Interfacing NodeMCU with Arduino Mega through Serial

Hello!

I am currently working on a project involving Arduino Mega and the NodeMCU board. The goal of this project is to control a small vehicle with a Camera and sensors via a web browser. I firstly programmed a websocket server to interact with a html file served through SPIFFS. The page is accessible by using my NodeMCU’s IP Address and data sent from Javascript is successfully read in the Serial Monitor.

The next step I took was to send this Serial info to the Arduino through the RX1/TX1 ports, and there was where the problem started. I want to send this info because it will give the commands to manage the motors and drive the car. However, when I write to Serial from NodeMCU and try to read on Arduino’s Serial1, nothing shows up.

Below it’s the schematics of the circuit (unnecessary things for this issue such as the L298N driver are ommited for clarity’s sake)

Code uploaded on NodeMCU:

#include "ESP8266WiFi.h" // connects Node MCU to a WiFi network
#include "ESPAsyncTCP.h" // dependency of ESPAsyncWebServer
#include "ESPAsyncWebServer.h" // makes possible to create an async web server to receive input from a browser
#include "FS.h" // FileSystem to handle .html files and such
 
const char* ssid = "net.1201"; // network name
const char* password =  "*********"; //network password
 
AsyncWebServer server(80);

AsyncWebSocket ws("/test");

void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){
 
  if(type == WS_EVT_CONNECT){
 
    Serial.println("Websocket client connected!");
 
  } else if (type == WS_EVT_DISCONNECT){
    Serial.println("Websocket client disconnected.");
 
  } else if(type == WS_EVT_DATA){
 
    Serial.print("Data received: ");
    char a = (char) data[0];
    Serial.println(a);
    Serial.write(a); // << Writing to Serial 
    Serial.println();
  }
}
 
void setup(){
  pinMode(D3, OUTPUT);
  Serial.begin(115200);
  digitalWrite(D3, LOW); // on
 
  if(!SPIFFS.begin()){
     Serial.println("SPIFFS failed");
     return;
  }
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
 
  Serial.println(WiFi.localIP());
 
  ws.onEvent(onWsEvent);
  server.addHandler(&ws);
 
  server.on("/html", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/control.html", "text/html");
  });
 
  server.begin();
}
 
void loop(){}

And Arduino’s code:

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);
  Serial1.begin(115200);  
  while (!Serial && !Serial1) {
    
  }
  Serial.println("Done");
}
 
void loop()
{
  if (Serial1.available() > 0) {
    int reading = Serial1.read();
    Serial.print("Msg received: ");
    Serial.println(reading);
  }
}

Do you guys see any hardware and/or software issues?
Voltage on NodeMCU’s RX and TX are measured at 2.4V, could be this the problem?

Thank you all in advance :slight_smile:

why do you have a voltage divider on RX of Mega?
I don't see a ground connection between Mega and NodeMcu

Juraj:
why do you have a voltage divider on RX of Mega?
I don't see a ground connection between Mega and NodeMcu

Hey Juraj! The black wire connects Arduino's GND to the protoboard. Then, there are these two voltage dividers for connecting RX_Ard/TX_MCU and vice versa. I used it because the Arduino operates with 5V whereas the NodeMCU only supports 3.3V

The TX pin on the NodeMCU is a 3.3 V output. The RX1 pin on the Mega is an input. Unless you do something wrong in your code, TX pin on the NodeMCU won’t be subjected to 5 V. So remove the voltage divider from the TX pin on the NodeMCU.

pert:
The TX pin on the NodeMCU is a 3.3 V output. The RX1 pin on the Mega is an input. Unless you do something wrong in your code, TX pin on the NodeMCU won't be subjected to 5 V. So remove the voltage divider from the TX pin on the NodeMCU.

Hi pert. I did it but I still cannot receive any data. I failed to mention that I am currently powering Arduino Mega via USB and the NodeMCU via another USB. The Motor Shield is powered with a 9V battery when on.

This is the new hardware layout:

victoroliveirab:
The black wire connects Arduino's GND to the protoboard.

Yes, but there's no connection between the NodeMCU's ground pin and the "protoboard"'s ground bus.

pert:
Yes, but there's no connection between the NodeMCU's ground pin and the "protoboard"'s ground bus.

Just wired both GNDs. Now the Serial Monitor keeps printing "Msg received: 0" and I don't know why is that.

Current setup:

And monitor print:

Thanks.

Anyone?? :confused: Up

Try running a simplified sketch on the NodeMCU that only prints to Serial to verify that serial communication is working correctly.

pert:
Try running a simplified sketch on the NodeMCU that only prints to Serial to verify that serial communication is working correctly.

Hi, pert. I believe the issue is not on NodeMCU. I can print stuff I write on the web out on the Serial Monitor. Then, I wired NodeMCU TX to Arduino RX1 and Arduino TX1 to NodeMCU RX and used Serial.write(0xFF) or anything. This won't show on Arduino's Serial. I tried swapping RX/TX to other GPIOs (as in Arduino/reference.rst at master · esp8266/Arduino · GitHub) and rewired everything.

Also tried setting pin 19 (RX1 on Mega) as a input, input_pullup, setting high and low, everything. Do you believe there is an issue with my board?

What happens if you run this sketch on the NodeMCU with the wiring shown in the Fritzing you posted?

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  Serial.print('a');
  delay(1000);
}

Use the sketch you posted here unmodified on the Mega.

pert:
What happens if you run this sketch on the NodeMCU with the wiring shown in the Fritzing you posted?

void setup()

{
  Serial.begin(115200);
}

void loop()
{
  Serial.print('a');
  delay(1000);
}




Use the sketch you posted here unmodified on the Mega.

I get 'a' s inline on NodeMCU's serial monitor, but Arduino's keeps printing "Msg received: 0" over and over

I now ran a modified version of Arduino's original sketch. I put it to print "Serial1.available()" content, and it seems like it goes from 0 to 63 (2^6 - 1, or am I trying to find a meaning to it?) and stays at 63. I tried flushing after a delay but once it reaches 63, it keeps on it.

Does it give you any clue? Thank you

victoroliveirab:
I now ran a modified version of Arduino's original sketch. I put it to print "Serial1.available()" content, and it seems like it goes from 0 to 63 (2^6 - 1, or am I trying to find a meaning to it?) and stays at 63. I tried flushing after a delay but once it reaches 63, it keeps on it.

Does it give you any clue? Thank you

the buffer fills up because it is read too slow.
but it is strange that it fills up with zeros even if you now send only 'a'

Juraj:
the buffer fills up because it is read too slow.
but it is strange that it fills up with zeros even if you now send only 'a'

What do you mean by strange? Do you think it is a board issue?

Update: a friend of mine was kind to lend me one NodeMCU and turns out the whole thing is now working.

Could anyone tell me how do I diagnose my board? Should I reflash the firmware? Or is it for sure a physical problem?