SHARE
TWEET

Energy Monitor Arduino Sketch

techwithshae Jan 28th, 2020 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Dhcp.h>
  2. #include <Dns.h>
  3. #include <Ethernet2.h>
  4. #include <EthernetClient.h>
  5. #include <EthernetServer.h>
  6. #include <EthernetUdp2.h>
  7. #include <Twitter.h>
  8. #include <util.h>
  9. #include <millisDelay.h>
  10. #include <PubSubClient.h>
  11. #define CLIENT_ID       "Power Meter I/O"
  12. millisDelay publishDelay;
  13.  
  14. #define ct1 A0
  15. #define ct2 A1
  16.  
  17. uint8_t mac[6] = {0xDE, 0xAD, 0x5E, 0xEF, 0x8E, 0xED};
  18. byte ip[] = { 192, 168, 1, 50 }; //SET STATIC IP ADDRESS OF MODULE - Change to something in your ip range
  19.  
  20. EthernetClient ethClient;
  21. PubSubClient mqttClient;
  22.  
  23. //CT Fine Tuning - Value used for math equation
  24. float ctdivision = 10.23;
  25.  
  26. void setup() {
  27.   pinMode(A0, INPUT);
  28.   pinMode(A1, INPUT);
  29.  
  30.   // setup serial communication
  31.  
  32.   Serial.begin(9600);
  33.   while (!Serial) {};
  34.   Serial.println(F("Energy Monitor I/O"));
  35.   Serial.println();
  36.  
  37.   Ethernet.begin(mac,ip);
  38.  
  39.   Serial.print("IP address: ");
  40.   Serial.println(Ethernet.localIP());
  41.   Serial.println();
  42.  
  43.   mqttClient.setClient(ethClient);
  44.   mqttClient.setServer( "192.168.1.10", 1883); //SET TO YOUR MQTT SERVER IP
  45.   mqttClient.setCallback(callback);
  46.  
  47.  
  48. publishDelay.start(1000);
  49.  
  50. }
  51.  
  52. void reconnect() {
  53.   // Loop until we're reconnected
  54.   while (!mqttClient.connected()) {
  55.     Serial.print("Attempting MQTT connection...");
  56.        if (mqttClient.connect(CLIENT_ID)) {
  57.       Serial.println("connected");
  58.       mqttClient.publish("sensor/power/connect", "true");
  59.     } else {
  60.       Serial.print("failed, rc=");
  61.       Serial.print(mqttClient.state());
  62.       Serial.println(" try again in 5 seconds");            
  63.       // Wait 5 seconds before retrying
  64.       delay(5000);
  65.     }
  66.   }
  67. }
  68.  
  69.  
  70. void loop() {
  71.  
  72. if (!mqttClient.connected()) {
  73.     reconnect();
  74.   }
  75.  
  76.   mqttClient.loop();
  77.  
  78.   if (publishDelay.justFinished()) {
  79.       sendData();
  80.       publishDelay.repeat();
  81.   }
  82.  
  83. }
  84.  
  85.  
  86. void sendData() {
  87. if (!mqttClient.connected()) {
  88.     reconnect();
  89.  }
  90.  
  91.   int ct1Reading = analogRead(ct1);
  92.   ct1Reading = ct1Reading / ctdivision;
  93.   char ct1Amp[8];
  94.   dtostrf(ct1Reading, 1, 2, ct1Amp);
  95.  
  96.   int ct2Reading = analogRead(ct2);
  97.   ct2Reading = ct2Reading / ctdivision;
  98.   char ct2Amp[8];
  99.   dtostrf(ct2Reading, 1, 2, ct2Amp);  
  100.  
  101.  
  102.  
  103.  
  104.   Serial.println("------------------------------------");
  105.   Serial.println("Current Data - 1 Second Refresh");
  106.   Serial.println();
  107.  
  108.   Serial.print("L1:");
  109.   Serial.println(ct1Amp);
  110.   Serial.println(ct1Reading);
  111.   Serial.print("L2:");
  112.   Serial.println(ct2Amp);  
  113.   Serial.println(ct2Reading);
  114.   Serial.println("------------------------------------");
  115.  
  116.   if (mqttClient.connect(CLIENT_ID)) {
  117.     mqttClient.publish("sensor/power/L1", ct1Amp);
  118.     mqttClient.publish("sensor/power/L2", ct2Amp);
  119.   }
  120. }
  121.  
  122.  
  123. void callback(char* topic, byte* payload, unsigned int length) {
  124.  
  125.  
  126. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top