Robin2:
Your problem seems to be in your function uploadThingSepak(); but you did not post that.Please post your complete program.
...R
The sender code is exactly the same as your previous reply.
Serial.print('<'); // start marker
Serial.print(watts1);
Serial.print(',');
Serial.print(watts2);
Serial.print(',');
Serial.print(watts3);
Serial.println('>');
this is receiver:
*some thing's are removed for clarity sake but won't affect the program
#include <ThingSpeak.h>
#include <ESP8266WiFi.h>
const byte numChars = 32;
char receivedChars[numChars];
String receivedString[numChars];
boolean newData = false;
const char* ssid = "****"; //SSID WIFI
const char* password = "****"; //PASS WIFI
WiFiClient client;
const int channelID = *****; //channel ID thingspeak
String writeAPIKey = "***************"; //write API key
const char* server = "api.thingspeak.com" ;
const int postingInterval = 20 * 1000;
void setup(){
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED){
delay(500);
}
}
void loop() {
recvWithStartEndMarkers();
String s1 = receivedString[0]; // this line should get the sensor 1 value but idk which index
String s2 = receivedString[2]; // this line should get the sensor 2 value but idk which index
String s3 = receivedString[4]; // this line should get the sensor 3 value but idk which index
uploadThingSpeak();
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
String senser;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
senser = String(receivedChars[ndx]); //Casting char to string so it can be saved in receivedString array
receivedString[ndx] = senser;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void uploadThingSpeak(){
if (client.connect("api.thingspeak.com", 80)){
// API BODY
String body = "api_key="+writeAPIKey+"&field1="+s1+"&field2="+s2+"&field3="+s3;
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(body.length());
client.print("\n\n");
client.print(body);
client.print("\n\n");
}
client.flush();
client.stop();
// wait and then post again
delay(postingInterval);
}
Sorry for the trouble.