#include #include "EmonLib.h" #include "WiFi.h" #include #include // This is the device name as defined on AWS IOT #define DEVICE_NAME "xd-home-energy-monitor-1" // The GPIO pin were the CT sensor is connected to (should be an ADC input) #define ADC_INPUT 34 // The voltage in our apartment. Usually this is 230V for Europe, 110V for US. // Ours is higher because the building has its own high voltage cabin. #define HOME_VOLTAGE 110.0 // Force EmonLib to use 10bit ADC resolution #define ADC_BITS 10 #define ADC_COUNTS (1< 1000) { double amps = emon1.calcIrms(1480); // Calculate Irms only double watt = amps * HOME_VOLTAGE; if(watt > 45) { Serial.print("watt optimized: "); Serial.println(watt*1.35); } // Update the display Serial.print("watt: "); Serial.println(watt); Serial.print("amps: "); Serial.println(amps); String toSend = "WO: " + String(watt*1.35) + ", W: " + String(watt) + ", A: " + String(amps); toSend.toCharArray(message_buff, toSend.length()+1); client.publish("Energy", message_buff); lastMeasurement = millis(); // Readings are unstable the first 5 seconds when the device powers on // so ignore them until they stabilise. if (millis() - timeFinishedSetup < 10000) { Serial.println("Startup mode "); } else { Serial.println(WiFi.localIP()); measurements[measureIndex] = watt; measureIndex++; } } // When we have 30 measurements, send them to AWS! if (measureIndex == 30) { Serial.println("we have 30 measurements "); // Construct the JSON to send to AWS String msg = "{\"readings\": ["; for (short i = 0; i <= 28; i++) { msg += measurements[i]; msg += ","; } msg += measurements[29]; msg += "]}"; Serial.println("Message is ready : " + msg); measureIndex = 0; } }