Arduino UNO to NodeMCU serial communication

I'm trying to send information from an Arduino to a NodeMCU board via the software serial library so i can then send that data to a local server i have running via post request, the data does seem to arrive to the NodeMCU but all it does is take over the the serial monitor and print itself over and over while i can't access it through serial.read( ) or serial.readString( ), the post request is being sent but with an empty string and if i hard code it to send over a string to the server it does send it succesfully so it looks like it's a problem with the serial transmission that i'm not able to get that data over to the string i'm sending over to the server, i can't figure out what i'm doing wrong.

Arduino UNO code:

#include <SoftwareSerial.h>
SoftwareSerial espSerial(6, 7); //6 to RX, 7 to TX
String str;

void setup(){
Serial.begin(115200);
espSerial.begin(9600);
delay(2000);
}

void loop(){
delay(5000);
float chairNum = 1, data = 34; //placeholder data

//begin byte
espSerial.write(0x01);

str = "";
str += ("chairNum=");
str += String(chairNum, 1);
str += ("&data=");
str += String(data, 1);
//string data being sent
espSerial.print(str);

//end byte
espSerial.write(0x02);
}

ESP-12E code:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
//wifi credentials
const char* ssid = "WiFi__EXT";
const char* password = "sdfdfse";
//Your Domain name with URL path or IP address with path
const char* serverName = "http://192.168.0.12:3000/chairTest";
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;
//variables for the byte transfer logic
int i = 0;
char dataArray[50] = "";
bool flag1 = false;

void setup() {
  Serial.begin(9600);

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
  //Send an HTTP POST request every x seconds
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      WiFiClient client;
      HTTPClient http;
      // Your Domain name with URL path or IP address with path
      http.begin(client, serverName);
      // Specify content-type header
      http.addHeader("Content-Type", "application/x-www-form-urlencoded");
      // Data to send with HTTP POST
      String message = "";
      byte n = Serial.available();
      if (n != 0){
        if (flag1 == false){
          byte x = Serial.read();
          if (x == 0x01){
            Serial.print("Received start.");
            flag1 = true;
          }
        }else{
          byte x1 = Serial.read();
          if (x1 != 0x02){
            dataArray[i] = x1;
            i++;
          }else{
            dataArray[i] = 0x00;  //nul-byte
            Serial.println(dataArray);
            message = dataArray;
            i = 0;
            flag1 = false;
          }
        }
      }
      String httpRequestData = message;
      Serial.println(httpRequestData);
      // Send HTTP POST request
      int httpResponseCode = http.POST(httpRequestData);
     
      Serial.print("HTTP Response code: ");
      Serial.println(String(httpResponseCode));
        
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

I've copied the byte receiving logic from another topic to see if it would fix my problem but it's the same result even if i just try to get the data using a single serial.readString( ) and i'm using the latest version to date on IDE and all the libraries.

Have you accounted for the fact that the Uno is a 5V device whilst NodeMCU is a 3.3V device ?

Is there any reason why you are using both a Uno and a NodeMCU in the project ?

Yes the pin 6 of the Arduino going to the RX pin of the NodeMCU has a voltage divider that makes the 5v voltage from the Arduino into 3.3v and no particular reason for the choice of board other than i need the Arduino to be reading sensor data from load cells through an HX711 amplifier module and sending over that data to the NodeMCU to send over to the server, i've got the sensor working with the Arduino already so that code isn't included, the problem being that i can't get any sort of data going to the NodeMCU and sending it over to the server, i am seeing the data from the Arduino in the NodeMCU serial monitor i'm just not able to work with it, also not able to check any of the variables through printing it with the monitor being taken over by the transfered strings but the code still works since i can see the requests coming in the server.

That doesn't really explain. The question was really, why not let the NodeMCU read the load cells?

Well i didn't think of that haha, i'm not aware of what the NodeMCU can do so i'll see if i can give that go, originally i was trying this with an ESP01 only after not getting that to work i bought the NodeMCU but i was thinking of the same method of sending the data over to then send it to the server.

x
Two microcontrollers: one problem.
One Microcontroller: no problem.

Why do you need two microcontrollers?
Just use the NodeMCU for your program.

1 Like

Where does the Arduino Uno program read data from the ESP? I only see writes.

It doesn't, only writes and prints to the NodeMCU via software serial, it only goes one way.

Your NodeMCU serial reads are nested inside your timer delay.

I've tried moving the whole if logic outside the timer and it gave same results.

09:53:27.227 -> chairNum=1.0&data=34.0chairNum=1.0&data=34.0

This is what shows on the monitor over and over.

please show us that one. it has a better chance of working.

I did got it to work without the arduino just like it would in the arduino even though it doesnt solve the problem i was having it solves my problem so ill mark it as solved, thank you to everyone.

1 Like

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