Created
March 27, 2020 07:57
-
-
Save protongt/fad70e5d65bbc340e30d3fd94f33eac0 to your computer and use it in GitHub Desktop.
IOT Thingspeak Energy Monitoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include "EmonLib.h" // Include Emon Library | |
EnergyMonitor emon1; // Create an instance | |
#define SSID "SSID" // "WiFi Name" | |
#define PASS "PASSWORD" // "Password" | |
#define IP "184.106.153.149"// thingspeak.com ip | |
String msg = "GET /update?key=APIKEY"; //change it with your key... | |
int error; | |
float Irms; | |
float power; | |
void setup() | |
{ | |
Serial.begin(9600); // use default 9600. | |
Serial.println("AT"); | |
delay(5000); | |
if(Serial.find("OK")){ | |
connectWiFi(); | |
} | |
emon1.current(1, 10.1); // Current: input pin, calibration. | |
} | |
void loop() | |
{ | |
start: | |
error=0; | |
Irms = emon1.calcIrms(1480); // Calculate Irms only (A) | |
power = Irms*230.0; // Calculate power only (W) | |
char buffer[10]; | |
updateData(); | |
if (error==1) | |
{ | |
goto start; | |
} | |
delay(60000); | |
} | |
void updateData() | |
{ | |
String cmd = "AT+CIPSTART=\"TCP\",\""; | |
cmd += IP; | |
cmd += "\",80"; | |
Serial.println(cmd); | |
delay(2000); | |
if(Serial.find("Error")){ | |
return; | |
} | |
cmd = msg ; | |
cmd += "&field1="; | |
cmd += String(Irms); | |
cmd += "&field2="; | |
cmd += String(power); | |
cmd += "\r\n"; | |
Serial.print("AT+CIPSEND="); | |
Serial.println(cmd.length()); | |
if(Serial.find(">")) | |
{ | |
Serial.print(cmd); | |
} | |
else | |
{ | |
Serial.println("AT+CIPCLOSE"); | |
//Resend... | |
error=1; | |
} | |
} | |
boolean connectWiFi(){ | |
Serial.println("AT+CWMODE=1"); | |
delay(2000); | |
String cmd="AT+CWJAP=\""; | |
cmd+=SSID; | |
cmd+="\",\""; | |
cmd+=PASS; | |
cmd+="\""; | |
Serial.println(cmd); | |
delay(5000); | |
if(Serial.find("OK")) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment