-1

I have an Arduino Uno and an ESP8266, connected over serial through ESP8266 pin GPIO_2 (which is set as RX over espsoftwareserial) and 11 on Uno (which is set as TX over SoftwareSerial). I am trying to send data in this format:

<0.000000:0.000000:0.000000:0.000000:0.000000:0.000000:24.50:24.50:1.49>

It is < char then number with six floats, separator :, and again... 9 times

I am using:

Arduino UNO code:

SoftwareSerial ARD_ESP(10, 11); // RX, TX

totalVolume[0] = 0.000000;
totalVolume[1] = 0.000000;
totalVolume[2] = 0.000000;
flowRate[0] = 0.000000;
flowRate[1] = 0.000000;
flowRate[2] = 0.000000;
float temperature_cold_value = 24.50;
float temperature_hot_value = 24.50;
float propane_gas_value = 1.49;

void setup() 
{
   ARD_ESP.begin(57600);
   Serial.begin(9600);
}
void loop()
{
if (millis() - previousMillis_esp_post > 5000 && run_state_metering == 1)
     {          
          ARD_ESP.print("<");
          ARD_ESP.print(totalVolume[0],6);
          ARD_ESP.print(":");
          ARD_ESP.print(totalVolume[1],6);
          ARD_ESP.print(":");
          ARD_ESP.print(totalVolume[2],6);
          ARD_ESP.print(":");
          ARD_ESP.print(flowRate[0],6);
          ARD_ESP.print(":");
          ARD_ESP.print(flowRate[1],6);
          ARD_ESP.print(":");
          ARD_ESP.print(flowRate[2],6);
          ARD_ESP.print(":");
          ARD_ESP.print(temperature_cold_value,2);
          ARD_ESP.print(":");
          ARD_ESP.print(temperature_hot_value,2);
          ARD_ESP.print(":");
          ARD_ESP.print(propane_gas_value,2);       
          ARD_ESP.print(">");

          Serial.print("<");
          Serial.print(totalVolume[0],6);
          Serial.print(":");
          Serial.print(totalVolume[1],6);
          Serial.print(":");
          Serial.print(totalVolume[2],6);
          Serial.print(":");
          Serial.print(flowRate[0],6);
          Serial.print(":");
          Serial.print(flowRate[1],6);
          Serial.print(":");
          Serial.print(flowRate[2],6);
          Serial.print(":");
          Serial.print(temperature_cold_value,2);
          Serial.print(":");
          Serial.print(temperature_hot_value,2); 
          Serial.print(":");
          Serial.print(propane_gas_value,2);     
          Serial.print(">");
          Serial.print("");

          previousMillis_esp_post = millis();
     }  

ESP8266 code: (it's programmed with Arduino IDE)

#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
#include <string.h>

SoftwareSerial ARD_ESP(2, SW_SERIAL_UNUSED_PIN); 

const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;

String sensor_1_total_volume;
String sensor_2_total_volume;
String sensor_1_flow_rate;
String sensor_2_flow_rate;
String temperature_cold_value;
String temperature_hot_value;
String propane_butane_gas_value;
String mereni_token = "xxxxx";

void setup() {

  Serial.begin(115200);
  ARD_ESP.begin(57600);
  delay(10);
}

void loop() {

  recvWithStartEndMarkers();
  showNewData();

  sensor_1_total_volume = getValue(receivedChars, ':', 0);
  sensor_2_total_volume = getValue(receivedChars, ':', 1);
  sensor_1_flow_rate = getValue(receivedChars, ':', 2);
  sensor_2_flow_rate = getValue(receivedChars, ':', 3);
  temperature_cold_value = getValue(receivedChars, ':', 4);
  temperature_hot_value = getValue(receivedChars, ':', 5);
  propane_butane_gas_value = getValue(receivedChars, ':', 6);

}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (ARD_ESP.available() > 0 && newData == false) {
        rc = ARD_ESP.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                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 showNewData() {
    if (newData == true) {
        Serial.print("This arrived: (unparsed)");
        Serial.println(receivedChars);
        Serial.println("DATA sensors: (parsed)");
        Serial.println(sensor_1_total_volume);
        Serial.println(sensor_2_total_volume);
        Serial.println(sensor_1_flow_rate);
        Serial.println(sensor_2_flow_rate);
        Serial.println(temperature_cold_value);
        Serial.println(temperature_hot_value);
        Serial.println(propane_butane_gas_value);
        Serial.println(mereni_token);
        newData = false;
    }
}

String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;

    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
} 

It works fine, but ESP receive only three values, another 6 does not. I have tried to change bitrate to 57600, but thats not help. I think that there is a problem with speed or waiting for full data.

Can you help me ? Many Thanks.

Here is the output from ESP:

This arrived: (unparsed)0.000000:0.000000:0.000000:0.00
DATA sensors: (parsed)
0.000000
0.000000
0.000000
0.004



xxxx
  • 1
    Could you reduce these pages of code to a minimal, complete, verifiable example [stackoverflow.com/help/mcve] please, and edit your question to include it. You might even find that doing so fixes your problem. – Mark Smith Jan 21 '17 at 16:48
  • Its Done. Maybe now someone could help. – Vasekdvor Jan 21 '17 at 17:26
  • I meant get rid of all the many Serial.println()s and replace with a single test string. Send only that string. Write a minimal example for the receiving device to receive and print only that line. Show how the expected and actual received data differ. – Mark Smith Jan 21 '17 at 17:30
  • You should be able to write a little 10-line program for sender and another for receiver, and demonstrate the problem very concisely. It'll help us, and it'll help you :-) – Mark Smith Jan 21 '17 at 17:52
0

You are sending number+'.'+6 float +separator = 9 char at least.

You are sending 9x this, 81 char at least. Plus '<>' and maybe some more chars...

And you store it in 32 char long buffer. Somehow it does not fit all inside, who knows why? :(

As you can see, you got like 32 chars only of the full transmission. Try to set numChars to something like 100 and try again. See, if it is better.

(Also I would use pattern

      ARD_ESP.print("<");
      Serial.print("<");
      ARD_ESP.print(totalVolume[0],6);
      Serial.print(totalVolume[0],6);
      ARD_ESP.print(":");
      Serial.print(":");

to leave the ESP more time to process stored data, but it is probabelly not problem now)

  • Thank you so much. I am such an idiot. I wrote a program and forget on setting numChars to corresponding value. – Vasekdvor Jan 22 '17 at 16:14
  • I am trying to use this for my purposes with just two strings. I was hoping to use it with an ESP8266-01 with only eight pins. Do you think it would work what pins should I plug into on the ESP if I use TX (2) and RX(3) on the Arduino UNO. Thanks – user1114881 Apr 18 at 2:51
  • Sorry, I do not have ESP or much knowledge about it. I just counted characters and read the size of buffer and see, there is at least one big problem (regardless platform) which I can simply see. Happened this was all, what was needed to fix it. – gilhad Apr 18 at 5:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.