getting delay in serial communication between arduino nano and nodemcu

hello i making project of transformer health monitoring in which i am taking sensor data from arduino nano to nodemcu and then uploading to ubidots server problem i am facing is that i am getting delay data in nodemcu i want to eliminate this delay.

for example in arduino nano: so in nodemcu i am getting like:
a=50 a= 5
a=30 a=3
a=60 a= 50 this is the delay i am getting
a=90 a=30
a=60
a=90

Arduino nano code

#include <SoftwareSerial.h>
#include "DHT.h" 
SoftwareSerial nodemcu(8,9);

float sdata1 = 0; // sensor1 data
float sdata2 = 0; // sensor2 data
float sdata3 = 0; // sensor3 data
float sdata4 = 0; // sensor3 data
float sdata5 = 0; // sensor3 data

String cdata; // complete data, consisting of sensors values
//---------------------------------- Oil level --------------------------
const int trigPin = 5;
const int echoPin = 6;
long duration,hp = 0;
int distance;

//--------------------------------- Current measurement -----------------
const int currentPin = A0;
int sensitivity = 66;
int adcValue= 0;
int offsetVoltage = 2500;
double adcVoltage = 0;
double currentValue = 0;

//----------------------------------- Voltage measurement ---------------
const int voltageSensor = A1;

float vOUT = 0.0;
float vIN = 0.0;
float R1 = 30000.0;
float R2 = 7500.0;
int value = 0;
//------------------------------ Temperature and Humidity ----------------------

#define DHTPIN 2     // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600); // Starts the serial communication
nodemcu.begin(115200);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input

dht.begin();
}

void loop() {
//------------------------------- Oil level ----------------------------------
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance= duration*0.034/2;
  distance = distance -6;
  distance = 50 - distance;
  

//-------------------------------- Current measurement -------------------------

  adcValue = analogRead(currentPin);
  adcVoltage = (adcValue / 1024.0) * 5000;
  currentValue = ((adcVoltage - offsetVoltage) / sensitivity);
//------------------------------- Voltage Measurement --------------------------

  value = analogRead(voltageSensor);
  vOUT = (value * 5.0) / 1024.0;
  vIN = vOUT / (R2/(R1+R2));
//------------------------------ Temperature and Humidity ----------------------

  delay(2000);
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

   sdata1 = (float)distance;
   sdata2 = (float)currentValue;
   sdata3 = vIN;
   sdata4 = h;
   sdata5 = t;

   cdata = cdata + sdata1+","+sdata2+","+sdata3+","+sdata4+","+sdata5; // comma will be used a delimeter
   Serial.println(cdata); 
   nodemcu.println(cdata);
   delay(6000); // 100 milli seconds
   cdata = ""; 
}

nodemcu code

#include <SoftwareSerial.h>
#include "Ubidots.h"

const char* UBIDOTS_TOKEN = "";  // Put here your Ubidots TOKEN
const char* WIFI_SSID = "";      // Put here your Wi-Fi SSID
const char* WIFI_PASS = "";      // Put here your Wi-Fi password
Ubidots ubidots(UBIDOTS_TOKEN, UBI_HTTP);

String myString; // complete message from arduino, which consistors of snesors data
char rdata; // received charactors

float firstVal, secondVal,thirdVal,fourthVal,fifthVal; // sensors 

void setup()
{
  // Debug console
 Serial.begin(115200);
 ubidots.wifiConnect(WIFI_SSID, WIFI_PASS);

}

void loop()
{
   if (Serial.available() > 0 ) 
  {
    rdata = Serial.read(); 
    myString = myString+ rdata; 
   // Serial.print(rdata);
    if( rdata == '\n')
    {
       // new code
       String l = getValue(myString, ',', 0);
       String m = getValue(myString, ',', 1);
       String n = getValue(myString, ',', 2); 
       String o = getValue(myString, ',', 3);
       String p = getValue(myString, ',', 4);
 
       firstVal = l.toFloat();
       secondVal = m.toFloat();
       thirdVal = n.toFloat();
       fourthVal = o.toFloat();
       fifthVal = p.toFloat();
       Serial.print(firstVal);
       Serial.print(",");
       Serial.print(secondVal);
       Serial.print(",");
       Serial.print(thirdVal);
       Serial.print(",");
       Serial.print(fourthVal);
       Serial.print(",");
       Serial.print(fifthVal);
       
       ubidots.add("value1", firstVal);  // Change for your variable name
       ubidots.add("value2", secondVal);
       ubidots.add("value3", thirdVal);
       ubidots.add("value4", fourthVal);
       ubidots.add("value5", fifthVal);
       bool bufferSent = false;
       bufferSent = ubidots.send();  // Will send data to a device label that matches the device Id

       if (bufferSent) {
              // Do something if values were sent properly
             Serial.println("Values sent by the device");
       }

       delay(5000);
       myString = "";
      // end new code
    }
  }
}

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]) : "";
}

Can anyone please help me out

Hi khalifa,

you should not use global defined variables of type String. The variable-type String eats up all RAM over time
because each new assignemt needs new RAM. In a RAM-limited environmanet lieke a microcontroller this is a problem.
Over time all RAM is taken by the string-variables and then starts to even overwrite other variables. This will make your program act strange and is very hard to debug. So you should use alternatives like PString, or SafeString. both available through the library-manager in the Arduino-IDE.

For more information why read here
The Evils of Arduino Strings

connecting to a WiFi-router needs some time. So your setup-function should wait until the connection is established
I have never worked with the ubidots.library. There should be a documentaion with which command you can check if connection has established.

Then in your code there is a

delay(5000);

this command makes your nodeMCU busy for 5 seconds. Completely busy while the delay(5000) is executed not even communication over wifi will work.

you have to find a way to completely avoid the command delay().

best regards Stefan

delay(6000); // 100 milli seconds

Which is correct, the code or the comment ?

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

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

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

...R