Problem regarding sending multiple sensor data from Arduino to Nodemcu ESP8266

Hello,
I am a beginner in the field of Arduino and I need a help in the following.
I am designing a device to monitor air pollutants and the data sensed by the sensors are to be uploaded on the MySQL database.
Below, I have written a basic code for sending multiple static values from Arduino UNO to Nodemcu ESP8266 using JSON.
The sole problem here is that, in the code part for Nodemcu, only this "String incomingString = linkSerial.readString();" line gets executed and the code below this is not executed.
Can somebody help me with this. I have also attached the code snippets and the output.

I am open to any other solution which will help me out in sending sensor data from Arduino UNO to Nodemcu ESP8266 .

PS. In the following code I only want the solution for transferring values from Arduino to NOdemcu and not for uploading the values to MySQL database .

Please HELP!.

CODE FOR ARDUINO

#include <ArduinoJson.h>
#include <SoftwareSerial.h>

// Declare the "link" serial port
// Please see SoftwareSerial library for detail
SoftwareSerial linkSerial(4, 5); // RX, TX

void setup() {
// Initialize "debug" serial port
// The data rate must be much higher than the "link" serial port
Serial.begin(115200);
while (!Serial) continue;

// Initialize the "link" serial port
// Use the lowest possible data rate to reduce error ratio
linkSerial.begin(4800);
}

void loop() {
// Values we want to transmit
long timestamp = millis();
int value = random(100);
int value1 = random(500);

// Print the values on the "debug" serial port
Serial.print("timestamp = ");
Serial.println(timestamp);
Serial.print("value = ");
Serial.println(value);
Serial.print("value1 = ");
Serial.println(value1);
Serial.println("---");

// Create the JSON document
StaticJsonDocument<200> doc;
doc["timestamp"] = timestamp;
doc["value"] = value;
doc["value1"] = value1;

// Send the JSON document over the "link" serial port
serializeJson(doc, linkSerial);

// Wait
delay(5000);
}

CODE FOR NODEMCU

#include <ArduinoJson.h>
#include <SoftwareSerial.h>

// Declare the "link" serial port
// Please see SoftwareSerial library for detail
SoftwareSerial linkSerial(4, 5); // RX, TX

void setup() {
// Initialize "debug" serial port
// The data rate must be much higher than the "link" serial port
Serial.begin(115200);
while (!Serial) continue;

// Initialize the "link" serial port
// Use the lowest possible data rate to reduce error ratio
linkSerial.begin(4800);
}

void loop() {
// Check if the other Arduino is transmitting
if (linkSerial.available())
{
String incomingString = linkSerial.readString();
Serial.println(incomingString);

// Allocate the JSON document
// This one must be bigger than for the sender because it must store the strings
StaticJsonDocument<300> doc;

// Read the JSON document from the "link" serial port
DeserializationError err = deserializeJson(doc, incomingString );

if (err == DeserializationError::Ok)
{
// Print the values
// (we must use as() to resolve the ambiguity)
Serial.print("timestamp = ");
Serial.println(doc["timestamp"].as());
Serial.print("value = ");
Serial.println(doc["value"].as());
Serial.print("value1 = ");
Serial.println(doc["value1"].as());
}
else
{
// Print error to the "debug" serial port
Serial.print("deserializeJson() returned ");
Serial.println(err.c_str());

// Flush all bytes in the "link" serial port buffer
while (linkSerial.available() > 0)
linkSerial.read();
}
}
}

FYI I have had problems with software serial on several Arduinos, UNO and Nano, at bauds other then 9600. Since you have a 5 second delay, just keep sending the same message and watch for errors, if you get them it is in the serial link or software. That is how I found mine, I just kept sending the same message over and over.

Add a NL to your json output and try this code instead, to see what you are actually getting

String incomingString

bool readData() {
  if (linkSerial.available())    {
    char c = linkSerial.read();
    incomingString += c;
    Serial.print(c); Serial.print(" Ox"); Serial.println(c, HEX);
    if (c = '\n') {
      return true;
    } else {
      return false;
    }
  }
}

void loop() {
  if (readData()) {
    // Read the JSON document from the "link" serial port
    DeserializationError err = deserializeJson(doc, incomingString );

    if (err == DeserializationError::Ok) {
      // etc
      // . . .
      //
    }
     // clear for next data
    incomingString = ""; 
  }
}

Also your input flush code needs a timeout to allow for the slow serial.

Here is a non-blocking input flushing method

#include "millisDelay.h"
// install SafeString library from Arduino Library manager for millisDelay class

millisDelay flushingTimeout;
unsigned long FLUSHING_TIMEOUT_MS = 2000; // 2sec

bool flushInput(bool flushingIn) {
  if (!flushingIn) {
    return false; // nothing to do
  }
  // else flushingIn
  if (!flushingTimeout.isRunning()) {
    flushingTimeout.start(FLUSHING_TIMEOUT_MS);
  }
  while (Serial.available()) {
    Serial.read();
    flushingTimeout.restart();
  }
  if (flushingTimeout.justFinished()) {
    // had 2sec with no input
    return false;
  }
  // else still flushing
  return true;
}

bool flushing = false;

void setup() {
  Serial.begin(9600);
  for (int i = 10; i > 0; i--) {
    Serial.print(' '); Serial.print(i);
    delay(500);
  }
  Serial.println();

  flushing = true; // set to true to start flushing
}

void loop() {

  flushing = flushInput(flushing); // call this each loop
  if (flushing) {
    // still flushing
  } else {
    // finished
    Serial.println("Finished flushing");
    flushing = true; // start flushing again
    Serial.println("Start flushing again");
  }
}

also if you are using a lot of Arduino Strings check out my tutorial Taming Arduino Strings

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.