Rest of the code here
void updateLCD(float pv, float mains, float heater, float total)
{
char pvpower[10];
char mainspower[10];
char heaterpower[10];
char housepower[10];
dtostrf (pv, 4,1,pvpower);
dtostrf (mains, 4,1,mainspower);
dtostrf (heater,4,1,heaterpower);
dtostrf (total, 4,1,housepower);
lcdtext(8, 0, pvpower);
lcdtext(8, 1, mainspower);
lcdtext(8, 2, housepower);
lcdtext(8, 3, heaterpower);
}
void sendtothingspeak(float pv, float mains, float heater, float total)
{
char datastring[100] ; //holds text to send to thingspeak
char openstring[20]; //holds send coomand including length of data string
char lenstring[4];
char pvpower[10];
char mainspower[10];
char heaterpower[10];
char housepower[10];
int datalength=0;
int pvlen=3, mainslen=3, heaterlen=3, totallen=3;
// used for the dtostrf function to ensure no extra spaces are inserted as these break thingspeak
// will never need more than 4 characters as maximum supply from mains is 20.0KW
if (pv>9.49) pvlen=4;
if (mains>9.49) mainslen=4;
if (heater>9.49) heaterlen=4;
if (total>9.49) totallen=4;
// convert the floats from Energy monitor to char arrays with one decimal (eg 2.1)
dtostrf (pv,pvlen,1,pvpower);
dtostrf (mains, mainslen, 1, mainspower);
dtostrf (heater, heaterlen, 1, heaterpower);
dtostrf (total, totallen, 1, housepower);
// start TCP connection
strcpy(datastring,"AT+CIPSTART=\"TCP\",\"");
strcat(datastring,ip);
strcat(datastring,"\",80");
ESP8266command(datastring);
if(ESP8266response("OK",5000))
{
//we have an active collection to thingspeak
Serial.println(F("Connected to thingspeak"));
lcdtext(18,0,"*");
//build and send the GET request with the data in it
strcpy(datastring,GET);
strcat(datastring,"&field1="); strcat(datastring,pvpower);
strcat(datastring,"&field2="); strcat(datastring,mainspower);
strcat(datastring,"&field3="); strcat(datastring,heaterpower);
strcat(datastring,"\r\n");
//build the request to send AT+CIPSEND=xx - where xx is number of bytes in datastring
datalength=strlen(datastring);
strcpy(openstring,"AT+CIPSEND=");
dtostrf(datalength,2,0,lenstring);
strcat(openstring,lenstring);
ESP8266command(openstring);
if(ESP8266response(">",10000))
{
//arrow received
ESP8266command(datastring);
delay(200);
if(ESP8266response("CLOSED",10000))
{
//received by thingspeak
lcdtext(18,0," ");
}
else
{
//not received
}
}
else
{
//not received
}
}
else
{
//couldnt connect to thingspeak
Serial.println(F("Failed to connect to thingspeak"));
}
}
bool ESP8266response(const char response[], int timeout)
{
byte index=0;
char iochar=-1;
unsigned long oldmillis=millis();
//clear incoming string
incoming[0]=(char)0;
while((millis()-oldmillis)<timeout && !strstr(incoming,response))
{
if(wifiserial.available())
{
iochar=wifiserial.read();
incoming[index]=iochar;
index++;
incoming[index]='\0';
}
}
if ((millis()-oldmillis)>=timeout) {
return false;
}
else {
Serial.println(incoming);
Serial.print(response);
Serial.print(F(" received after "));Serial.print(millis()-oldmillis);Serial.println(F(" milliseconds"));
return true;
}
}
void connecttowifi()
{
char cmd[50] ="AT+CWJAP=\"";
Serial.println(F("Connecting to wifi"));
strcat (cmd,ssid);
strcat (cmd,"\",\"");
strcat (cmd,pwd);
strcat (cmd,"\"");
ESP8266command(cmd);
}
void ESP8266command (const char sendthis[])
{
Serial.print(F("Sending: "));
Serial.println(sendthis);
wifiserial.println(sendthis);
}
void scantext(char* tag, char* clientip)
{
char* label;
char* firstquote;
char* secondquote;
char lengthofvalue;
label=strstr(incoming,tag);
if (label)
{
firstquote=strstr(label+1,"\"");
secondquote=strstr(firstquote+1,"\"");
lengthofvalue=secondquote-firstquote-1;
strncpy(clientip,firstquote+1,lengthofvalue);
clientip[lengthofvalue]='\0';
}
else
{
strcpy(clientip,"NOT FOUND");
}
}
void lcdtext(int column, int row, const char text[] )
{
lcd.setCursor(column,row);
lcd.print(text);
}
void getenergyvalues()
{
//first the PV values
Serial.println(F("Getting PV power"));
emonpv.calcVI(halfwaves,timeout); // Calculate all. No.of half wavelengths (crossings), time-out
qpv = ((emonpv.realPower + pvoffset) / 1000);
//now the mains values
Serial.println(F("Getting mains power"));
emonmains.calcVI(halfwaves,timeout); // Calculate all. No.of half wavelengths (crossings), time-out
qmains = ((emonmains.realPower+mainsoffset) / 1000);
//now the heater values
Serial.println(F("Getting heater power"));
emonheater.calcVI(halfwaves,timeout); // Calculate all. No.of half wavelengths (crossings), time-out
qheater = ((emonheater.realPower + heateroffset) / 1000);
housetotal = (qpv + qmains) - qheater;
}