#include #include #include #include #include #define SERIAL 0 // set to 1 to also report readings on the serial port #define DEBUG 0 // set to 1 to display each loop() run and PIR trigger #define BLINK_CYCLES 5 // report every N blink cycles #define RADIO_SYNC_MODE 2 static byte myNodeID; // node ID used for this unit static unsigned long last; static int nBlinks = 0; Port inputPort(4); struct { int data:16; } payload; static void doReport() { while (!rf12_canSend()) rf12_recvDone(); rf12_sendStart(0, &payload, sizeof payload, RADIO_SYNC_MODE); #if SERIAL Serial.print("POWER USAGE "); Serial.print(payload.data); Serial.print(" WATTS"); Serial.println(); #endif } void ledBlink() { #if SERIAL #endif unsigned long time = millis(); unsigned long interval = time - last; //Serial.println(interval); ;; nBlinks++; if (nBlinks == BLINK_CYCLES) { // Blinks are 1000 per kWh, or 0.1 Wh each // One hour has 3.6M milliseconds //long watts = nBlinks * 0.1 * 3.6E7 / interval; long watts = 0.1 * 3.6E7 * nBlinks/ interval; payload.data = watts; nBlinks = 0; last = time; doReport(); } } void setup () { #if SERIAL || DEBUG Serial.begin(57600); Serial.println("\n[meter reading]"); myNodeID = rf12_config(); #else myNodeID = rf12_config(0); // don't report info on the serial port #endif inputPort.mode2(INPUT); // Set AIO mode as input inputPort.digiWrite2(1); // Activate pull-up resistor for AIO last = millis(); doReport(); } void loop () { static boolean ledOn = false; int brightness = inputPort.anaRead(); //Serial.println(brightness); if (!ledOn && brightness < 60) { //goes low for about 30 loop cycles when led flashes on ledOn = true; } else if (ledOn && brightness > 200) { ledOn = false; ledBlink(); } }