Arduino uno and nodemcu software serial communication

Hello I have a project wherein I need to send data from uno to nodemcu and then the values will be sent to blynk. My issue is that when using software serial it worked the day before now the nodemcu is not receiving the data or showing anything at all at it's serial monitor. Help please I'm now confused as I have no idea why this keeps on happening.


arduino code:

  #include <Wire.h>
  #include <LiquidCrystal_I2C.h>
  #include <SoftwareSerial.h>

  // Constants for sensor pins
  const int AMMONIA_SENSOR_PIN = A0;
  const int CO2_SENSOR_PIN = A1;

  // Constants for relay pins
  const int RELAY1_PIN = 4;
  const int RELAY2_PIN = 5;
  const int RELAY3_PIN = 6;

  // Constants for calculation
  const float SENSOR_VOLTAGE_REF = 5.0;
  const float ADC_RESOLUTION = 1023.0;
  const float M = -0.263; // Slope
  const float B = 0.42;   // Y-Intercept
  const float R0 = 35;    // Sensor resistance in fresh air

  // SoftwareSerial setup
  SoftwareSerial mySerial(2, 3); // RX, TX pins on Arduino Uno

  // LCD setup
  LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD address and size

  void setup() {
    initializeSensorsAndRelays();
    initializeLCD();
    initializeSerial();
  }

  void loop() {
    double ammonia_ppm = readAmmoniaLevel();
    int co2Level = readCO2Level();

    controlRelays(ammonia_ppm);
    displaySensorReadings(ammonia_ppm, co2Level);
    sendReadingsToNodeMCU(ammonia_ppm, co2Level);

    // Print readings to Serial Monitor
    Serial.print("Ammonia: ");
    Serial.print(ammonia_ppm, 2);
    Serial.print(" ppm\t");

    Serial.print("CO2: ");
    Serial.print(co2Level);
    Serial.println(" ppm");

    delay(500); // Delay for stability - consider non-blocking alternatives
  }

  void initializeSensorsAndRelays() {
    pinMode(CO2_SENSOR_PIN, INPUT);
    pinMode(AMMONIA_SENSOR_PIN, INPUT);
    pinMode(RELAY1_PIN, OUTPUT);
    pinMode(RELAY2_PIN, OUTPUT);
    pinMode(RELAY3_PIN, OUTPUT);

    digitalWrite(RELAY1_PIN, LOW);
    digitalWrite(RELAY2_PIN, LOW);
    digitalWrite(RELAY3_PIN, LOW);
  }

  void initializeLCD() {
    lcd.init();
    lcd.backlight();
  }

  void initializeSerial() {
    Serial.begin(115200); // Changed baud rate to 115200
    mySerial.begin(115200);
  }

  double readAmmoniaLevel() {
    int sensorValue = analogRead(AMMONIA_SENSOR_PIN);
    double sensorVolt = sensorValue * (SENSOR_VOLTAGE_REF / ADC_RESOLUTION);
    double RS_gas = ((SENSOR_VOLTAGE_REF * 10.0) / sensorVolt) - 10;
    double ratio = RS_gas / R0;
    return pow(10, (log10(ratio) - B) / M);
  }

  int readCO2Level() {
    int gas = analogRead(CO2_SENSOR_PIN);
    return map(gas - 64, 0, 1024, 400, 5000);
  }

  void controlRelays(double ammoniaPpm) {
    if (ammoniaPpm >= 2 && ammoniaPpm <= 10) {
      digitalWrite(RELAY1_PIN, LOW);
    } else {
      digitalWrite(RELAY1_PIN, HIGH);
    }

    if (ammoniaPpm > 10 && ammoniaPpm <= 20) {
      digitalWrite(RELAY2_PIN, LOW);
    } else {
      digitalWrite(RELAY2_PIN, HIGH);
    }

    if (ammoniaPpm > 20 && ammoniaPpm <= 50) {
      digitalWrite(RELAY3_PIN, LOW);
    } else {
      digitalWrite(RELAY3_PIN, HIGH);
    }

    delay(500); // Delay outside the if statements to avoid unnecessary repetition
  }

  void displaySensorReadings(double ammonia_ppm, double co2Level) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Ammonia: ");
    lcd.print(ammonia_ppm, 2);
    lcd.setCursor(0, 1);
    lcd.print("CO2: ");
    lcd.print(co2Level);
  }

  void sendReadingsToNodeMCU(double ammonia_ppm, double co2Level) {
    mySerial.print(ammonia_ppm, 2);
    mySerial.print(",");
    mySerial.println(co2Level);
    delay(1000);
  }

nodemcu code:

#define BLYNK_TEMPLATE_ID "TMPL64qpH6bPb"
#define BLYNK_TEMPLATE_NAME "mq137"
#define BLYNK_AUTH_TOKEN "zeFfyjNsRBwxi2LXwTTR2ZQ2njAa9F8p"
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "zeFfyjNsRBwxi2LXwTTR2ZQ2njAa9F8p";  // Replace with your Blynk Auth Token
char ssid[] = "qwe";              // Replace with your Wi-Fi SSID
char pass[] = "nayeonnnie";          // Replace with your Wi-Fi Password

SoftwareSerial nodemcuSerial(D7, D8);  // RX, TX pins on NodeMCU

BlynkTimer timer;

void myTimer() {
  while (nodemcuSerial.available()) {
    String data = nodemcuSerial.readStringUntil('\n');
    // Split the received data into ammonia and CO2 values
    int commaIndex = data.indexOf(',');
    String ammoniaValue = data.substring(0, commaIndex);
    String co2Value = data.substring(commaIndex + 1);
    
    float ppmValueAmmonia = ammoniaValue.toFloat();
    float ppmValueCO2 = co2Value.toFloat();
    
    Blynk.virtualWrite(V0, ppmValueAmmonia); // Send the ammonia ppm value to V0
    Blynk.virtualWrite(V1, ppmValueCO2); // Send the CO2 ppm value to V1
    
    Serial.print("Received ppm values - Ammonia: ");
    Serial.print(ppmValueAmmonia);
    Serial.print(", CO2: ");
    Serial.println(ppmValueCO2); // Print ppm values to the serial monitor
    delay(1000);
  }
}

void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  nodemcuSerial.begin(115200);
  timer.setInterval(1000L, myTimer);
}

void loop() {
  Blynk.run();
  timer.run();
}

Maybe back off from that 115200 baud rate:

1 Like

will do sir

Great start with the schematic. On your schematic you need to label the lines such as Tx, Rx, at each processor. You could use CAN as an alternative communication method especially if you are going to add nodes. Any wires over ~10" will eventually cause you problems if this goes into an application.

the rx and tx are in the code sorry I didn't label it
UNO RX = 2, TX =3;
NODEMCU RX =7, and TX = 8

Why is there a pull down resistor on the NODEMCU TX to the UNO Rx?

1 Like

Voltage divider from what I've learned 5v signals are released from the digital pins of uno since nodemcu is only 3.3v tolerant I used voltage divider so that it wont be destroyed

If you input 3.3V TX into the middle of a voltage divider, it won't multiply to 5V at the upper end of the voltage divider for the Uno's RX.

Think of it as the Uno's input being 3.3V tolerant.

1. Practically, the IO pins of NodeMCU(ESP8266) are 5V tolerant; so, you can connect them directly without any level shifters.

2. The following sketches test the software serial link between UNO and NodeMCU at Bd = 9600 though the link works at Bd = 115200 (not recommended).

Sender-UNO Sketch:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX pins on Arduino Uno

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

void loop() 
{
  mySerial.println("Hello!"); //test signal
  delay(1000);
}

Receiver-NodeMCU Sketch:

#include <SoftwareSerial.h>
SoftwareSerial nodemcuSerial(D7, D8);  // RX, TX pins on NodeMCU

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

void loop()
{
  byte n = nodemcuSerial.available();
  if (n != 0)
  {
    char y = nodemcuSerial.read();
    Serial.print(y);
  }
}

Serial Monitor of NodeMCU:

Hello!
Hello!
Hello!

3. Upload the sketches into your UNO and NodeMCU and check that the soft serial link works. After that test your custom sketches which get compiled in my IDE without any errors.

I tried using the codes you've provided sir but the serial monitor of node is empty. I checked the i/o pins of both mcus both can send signals that's why I'm having a hard time determining the issue

Check the continuity of the jumper wires and connect the boards directly using soft serial ports. After connection, check the continuity of all three jumpers (STX, SRX, GND) from UNO's pins to NodemCU's pis. Are the sketches uploaded in the respective boards?

Yes sir I already did I even have the another spare mcus here to check if its working but still to no avail its still not showing

Have you uploade my sketches of post#9 or your sketches of post #1? Have you connected them directly or not?

I tried directly and using voltage divider still the same sir

Have the two boards got a common GND connection or just Rx to Tx and vice versa ?

Take a camera picture of your setup and post here.

Yes I did

image
image

1 Like

This is my working setup (Fig-1) using NANO and NodeMCU.
image
Figure-1:

1 Like

will try to change the nodemcu tomorrow it might be the problem.