Serial communication slight problem using Wemos D1 R2 and UNO + ThingSpeak

Hello, i am new to arduino and currently seeking guidance on how to proceed.

my project uses 3 ACS712 sensor connected to UNO to detect current flowing through and then send the data to ThingSpeak using Wemos D1 R2 as the internet.

so far i managed to send the sensor data to wemos via serial communication, the problem is i don't know whether the data sent to the wemos corresponds to the sensor it belongs to.

*all calculation is already made

this is sender code:

 float W1 = I1 * V1;
 float W2 = I2 * V2;
 float W3 = I3 * V3;

 Serial.println(W1); //sensor 1
 delay(1000);
 Serial.println(W2); //sensor 2
 delay(1000);
 Serial.println(W); //sensor 3
 delay(1000);

Receiver:

if(Serial.available()){
    sensor1 = Serial.parseFloat(); //receiver float data that SHOULD BE coming from SENSOR 1
    delay(1000);
    sensor2 = Serial.parseFloat(); //receiver float data that SHOULD BE coming from SENSOR 2
    delay(1000);
    sensor3 = Serial.parseFloat(); //receiver float data that SHOULD BE coming from SENSOR 3
    delay(1000);
 }

unfortunately i can't ascertain if its actually receiving from the correct sensor or actually overwriting each other. Since i need to upload all 3 data to thingspeak every 20sec interval, i don't want to get the wrong data.

how should i manage this problem? and how do i implement it in my code?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The way you are sending and receiving the data in the programs in your Original Post you have no means to know which item of data is which. Look at how it is done in the 3rd example in my tutorial. You can send data in a compatible format like this

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

...R

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The way you are sending and receiving the data in the programs in your Original Post you have no means to know which item of data is which. Look at how it is done in the 3rd example in my tutorial. You can send data in a compatible format like this

Serial.print('<'); // start marker

Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker




...R

Thanks for replying and sorry for not replying so long.
i kinda get how to receive it by using the marker example but the problem is the sensor data needed to upload to thingspeak needed to be converted into string. since i only know how to upload all three values instead of one each time, its hard to convert value of each array into string.

i used the code in Serial input basics in example 3 and
this is what i can think of:

recvWithStartEndMarkers();
String s1 = String(receivedString[0]);
String s2 = String(receivedString[2]);
String s3 = String(receivedString[4]);
uploadThingSepak();

and this is the recvWithStartEndMarkers code:

String receivedString[ndx];
String senser;
 
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                senser = String(receivedChars[ndx]);
                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;
        }
    }

but this code might get the wrong data to upload since i just guessing which index the data belong to also how to empty the receivedString array after i get all the data then uploaded it.

Thanks in advance

AndiAkF:
i kinda get how to receive it by using the marker example but the problem is the sensor data needed to upload to thingspeak needed to be converted into string.

Your problem seems to be in your function uploadThingSepak(); but you did not post that.

Please post your complete program.

...R

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.

Rather than construct the "body" with this line

String body = "api_key="+writeAPIKey+"&field1="+s1+"&field2="+s2+"&field3="+s3;

you can build it up line by line if you replace the line

client.print(body);

with this

    client.print("api_key=");
    client.print(writeAPIKey);
    client.print("&field1=");
    client.print(receivedString[0]);
    //etc

...R

Thanks sir, one problem solved :slight_smile:

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

but how do i empty "receivedString" array when serial received the end marker?, i tried using the same line with "receivedChar" but it doesn't work.

i think its fine not to empty it since it reset the index to 0, but just in case.

and how do i identify which data i want it "receivedString" array?, the code above is just a guess.

AndiAkF:
i think its fine not to empty it since it reset the index to 0,

Yes

and how do i identify which data i want it "receivedString" array?, the code above is just a guess.

If you print the complete contents of receivedString[] what do you see - post some examples.

...R

String receivedString[ndx];

Uh...

AndiAkF:
Hello, i am new to arduino and currently seeking guidance on how to proceed.
my project uses 3 ACS712 sensor connected to UNO to detect current flowing through and then send the data to ThingSpeak using Wemos D1 R2 as the internet.

Have you seen this link here ? ....

https://forum.arduino.cc/index.php?topic=143382.0