Serial Communication Between Arduino Uno & Node MCU ESP8266

Hi all, I am doing an IoT project that wish to transfer real-time data of sensors to the IoT platform. I chose to perform serial communication between Arduino Uno & Node MCU ESP8266 due to the WiFi firmware from Node MCU ESP9266 is ready to use, no configuration is needed. The challenge I faced when doing serial communication is the transmission of data from Arduino to Node MCU is transferred in Byte and there's a need to transform it back to original value. I tried multiple coding but it seems not work, please kindly help.

The Sender Code is as below:

#include<SoftwareSerial.h>

SoftwareSerial s(0,1);

String sensor_data;

void setup() {
  //Serial S Begin at 9600 Baud
  s.begin(115200);
}

void writeString(String stringData) { // Used to serially push out a String with Serial.write()

  for (int i = 0; i < stringData.length(); i++)
  {
    s.write(stringData[i]);   // Push each char 1 by 1 on each loop pass
  }

}

void loop() {
  float sensorValue = analogRead(A1);
  float millivolt = (sensorValue/1023)*5;
  
  s.print("Sensor Value: ");
  s.println(sensorValue);
  s.print("Voltage: ");
  s.print(millivolt*1000);
  s.println(" mV");
  s.println("");
  delay(1);

  sensor_data = String(millivolt*1000);
  writeString(sensor_data);
  
  delay(1000);
}

Receiver Code:

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

SoftwareSerial s(3,1);

String  sensor_data_from_arduino;
WiFiClient client;

String MakerIFTTT_Key ;
String MakerIFTTT_Event;
char *append_str(char *here, String s) {  int i=0; while (*here++ = s[i]){i++;};return here-1;}
char *append_ul(char *here, unsigned long u) { char buf[20]; return append_str(here, ultoa(u, buf, 10));}
char post_rqst[256];char *p;char *content_length_here;char *json_start;int compi;

void setup()
{
  sensor_data_from_arduino = "";
  
  Serial.begin(115200);
  WiFi.disconnect();
  delay(3000);
  Serial.println("START");
  WiFi.begin("Aaron Lee","19980121");
  
  while ((!(WiFi.status() == WL_CONNECTED))){
    delay(300);
    Serial.print(".");
  }
  
  Serial.println("Connected");
  Serial.println("Your IP is");
  Serial.println((WiFi.localIP().toString()));

}


void loop()
{
  if (Serial.available()){
    sensor_data_from_arduino = Serial.read(); //string
    
    if (client.connect("maker.ifttt.com",80)) {
      MakerIFTTT_Key ="lhgdr4w3Vy25J6WKRLaIVZrLMCPb1B8uACWjpRdehYo";
      MakerIFTTT_Event ="EMG";
      p = post_rqst;
      p = append_str(p, "POST /trigger/");
      p = append_str(p, MakerIFTTT_Event);
      p = append_str(p, "/with/key/");
      p = append_str(p, MakerIFTTT_Key);
      p = append_str(p, " HTTP/1.1\r\n");
      p = append_str(p, "Host: maker.ifttt.com\r\n");
      p = append_str(p, "Content-Type: application/json\r\n");
      p = append_str(p, "Content-Length: ");
      content_length_here = p;
      p = append_str(p, "NN\r\n");
      p = append_str(p, "\r\n");
      json_start = p;
      p = append_str(p, "{\"value1\":\"");
      p = append_str(p, sensor_data_from_arduino);
      p = append_str(p, "\",\"value2\":\"");
      p = append_str(p, "");
      p = append_str(p, "\",\"value3\":\"");
      p = append_str(p, "");
      p = append_str(p, "\"}");
      
      compi= strlen(json_start);
      content_length_here[0] = '0' + (compi/10);
      content_length_here[1] = '0' + (compi%10);
      client.print(post_rqst);
      
      
      Serial.print("Value sent to Google Sheets is : ");
      Serial.println(sensor_data_from_arduino);
    }
    
    delay(10000);
  }
}

Reduce the baud rate to 9600.

There is a project that connects a Mega to an ESP8266.
Arduino to Arduino via Serial
A few point about your current code
i) SoftwareSerial s(0,1); is using the same pins as the Uno Hardware Uart.
Change that to say s(10,11)
ii) In the ESP you delay 10sec. while you send every 1sec. so time miss match
The link above has an example of a synchronized SerialComs where the ESP8266 will control how often data is sent

1 Like

Looks like you only need to send on number so just send it

#include<SoftwareSerial.h>

SoftwareSerial s(10,11);

void setup() {
  Serial.begin(115200); //for debug
    for (int i = 10; i > 0; i--) {
    delay(500);
    Serial.print(i); Serial.print(' ');
  }
  Serial.println("Uno started");

  s.begin(9600);
}

void loop() {
  float sensorValue = analogRead(A1);
  float millivolt = (sensorValue/1023)*5;
  // debug output
  Serial.print("Sensor Value: ");
  Serial.println(sensorValue);
  Serial.print("Voltage: ");
  Serial.print(millivolt*1000);
  Serial.println(" mV");

  // just send the mv and a newline
  s.println(millivolt,4); // 4 decimal places  
  
  delay(10000);  // to match the receiver sort of
}

If you need more than send them with commas between

Here is a reciever

#include <ESP8266WiFi.h>
#include<SoftwareSerial.h>
// download SafeString V4.1.5+ library from the Arduino Library manager or from
// https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html
#include "SafeString.h"

SoftwareSerial s(13, 12);

String  sensor_data_from_arduino;
WiFiClient client;

String MakerIFTTT_Key = "lhgdr4w3Vy25J6WKRLaIVZrLMCPb1B8uACWjpRdehYo";
String MakerIFTTT_Event = "EMG";

//char post_rqst[256];
createSafeString(post_rqst, 200); ;// was 100 but SafeString said more needed
//char *p;char *content_length_here;char *json_start;int compi;
createSafeString(json_data, 100);

void setup() {
  Serial.begin(115200); //for debug
  for (int i = 10; i > 0; i--) {
    delay(500);
    Serial.print(i); Serial.print(' ');
  }
  Serial.println("ESP8266 started");
  SafeString::setOutput(Serial); // for error msgs
  s.begin(9600);
  sensor_data_from_arduino.reserve(50); // allow some space for the data
  // prevents re-allocation

  // wifi stuff here
  //  Serial.begin(115200);
  //  WiFi.disconnect();
  //  delay(3000);
  //  Serial.println("START");
  //  WiFi.begin("Aaron Lee","19980121");
  //
  //  while ((!(WiFi.status() == WL_CONNECTED))){
  //    delay(300);
  //    Serial.print(".");
  //  }
  //
  //  Serial.println("Connected");
  //  Serial.println("Your IP is");
  //  Serial.println((WiFi.localIP().toString()));

  // do this just once
  post_rqst = ""; // clear last one
  post_rqst += "POST /trigger/";
  post_rqst += MakerIFTTT_Event.c_str();
  post_rqst += "/with/key/";
  post_rqst += MakerIFTTT_Key.c_str();
  post_rqst += " HTTP/1.1\r\n";
  post_rqst += "Host: maker.ifttt.com\r\n";
  post_rqst += "Content-Type: application/json\r\n";
  post_rqst += "Content-Length: ";

}


void loop() {
  if (s.available()) {
    sensor_data_from_arduino = s.readStringUntil('\n'); //read line
    Serial.println(sensor_data_from_arduino);
    //    if (client.connect("maker.ifttt.com",80)) {
    json_data = ""; // clear from last time
    json_data += "{\"value1\":\"";
    json_data += sensor_data_from_arduino.c_str();
    json_data += "\",\"value2\":\"";
    json_data += "";
    json_data += "\",\"value3\":\"";
    json_data += "";
    json_data += "\"}";

    // for testing
#define client Serial

    client.print(post_rqst);
    client.print(json_data.length());
    client.print("\r\n");
    client.print("\r\n");
    client.print(json_data);

    Serial.println();
    Serial.print("Value sent to Google Sheets is : ");
    Serial.println(sensor_data_from_arduino);
    //}
  }

  // delay(10000); remove this and just handle the data as it comes
}

I am using my SafeString library, but only because you had defined char[ ].
You could just as easily use Arduino Strings.

Or ditch the Uno entirely and have the esp8266 perform all tasks. It's plenty powerful and if the need for more pins arises external adc's and port multipliers can be added. In the end that would probably be the simplest approach IMO.

I agree with him. I've been working on a project that includes NodeMCU with esp8266 and a sensor, it's totally possible to just ditch your arduino. I do use my arduino to run my debug programs though because I find it's easier to upload to arduino than to NodeMCU.
If you wish to continue with your way, I suggest a mapping function, map your bytes to a value and just have a ratio equation.

Thanks, I will try it out

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