The arduino does not establish a serial communication with the NodeMCU

I am currently working with a CO2 sensor that communicates with an arduino uno by the UART protocol. I want to send the readings to the NodeMCU board, but when I enable a second object for another serial communication, the arduino does not send the information and also stops receiving data from the sensor. If I remove this second object, the arduino captures the information from the sensor, but if I initialize it again, it no longer captures information and does not send them to the NodeMCU. The way to verify that it receives or not the data from the sensor is through the serial monitor of the arduino

Do you know what causes this problem?

Cheers

Not without seeing your sketch

That sound like you are using SoftwareSerial and trying to use two instances of it

it is right

Are you saying that you are trying to use two instances of SoftwareSerial ?

If so then give up now

#include <SoftwareSerial.h>                           //we have to include the SoftwareSerial library, or else we can't use it
#define rx 2                                          //define what pin rx is going to be
#define tx 3                                          //define what pin tx is going to be

SoftwareSerial myserial(rx, tx);                      //define how the soft serial port is going to work
SoftwareSerial myserial(7, 8); 

String inputstring = "";                              //a string to hold incoming data from the PC
String sensorstring = "";                             //a string to hold the data from the Atlas Scientific product
boolean input_string_complete = false;                //have we received all the data from the PC
boolean sensor_string_complete = false;               //have we received all the data from the Atlas Scientific product
int Co2;                                              //used to hold a integer number that is the Co2



void setup() {                                        //set up the hardware
  Serial.begin(19200);                                 //set baud rate for the hardware serial port_0 to 9600
  myserial.begin(19200);                               //set baud rate for the software serial port to 9600
  NodeMCU.begin(19200
  inputstring.reserve(10);                            //set aside some bytes for receiving data from the PC
  sensorstring.reserve(30);                           //set aside some bytes for receiving data from Atlas Scientific product
}


void serialEvent() {                                  //if the hardware serial port_0 receives a char
  inputstring = Serial.readStringUntil(13);           //read the string until we see a <CR>
  input_string_complete = true;                       //set the flag used to tell if we have received a completed string from the PC
}


void loop() {                                         //here we go...

  if (input_string_complete == true) {                //if a string from the PC has been received in its entirety
    myserial.print(inputstring);                      //send that string to the Atlas Scientific product
    myserial.print('\r');                             //add a <CR> to the end of the string
    inputstring = "";                                 //clear the string
    input_string_complete = false;                    //reset the flag used to tell if we have received a completed string from the PC
  }

  if (myserial.available() > 0) {                     //if we see that the Atlas Scientific product has sent a character
    char inchar = (char)myserial.read();              //get the char we just received
    sensorstring += inchar;                           //add the char to the var called sensorstring
    if (inchar == '\r') {                             //if the incoming character is a <CR>
      sensor_string_complete = true;                  //set the flag
    }
  }


  if (sensor_string_complete == true) {               //if a string from the Atlas Scientific product has been received in its entirety
    Serial.println(sensorstring);                     //send that string to the PC's serial monitor
    NodeMCU.write(sensorstring);
    sensorstring = "";                                //clear the string
    sensor_string_complete = false;                   //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
  }
}

Can I2C be used to send the data to the NodeMCU?

Yes

How would I program the NodeMCU as the slave and the Arduino as the master?

I have already tried this protocol but when I send the character string to the NodeMCU, the latter does not read all the characters.

Please post the sketches that you tried

Could you post a wiring diagram and or some pictures of your project? Show your use of level shifters between the non 5V tolerant ESP and the 5V Uno.

The Uno does not do 2 software serials as a note.

Why can't the ESP8266 be directly connected to the CO2 sensor?

This is the code for the arduino:

#include <SoftwareSerial.h>                           //we have to include the SoftwareSerial library, or else we can't use it
#include <Wire.h>
#define rx 2                                          //define what pin rx is going to be
#define tx 3                                          //define what pin tx is going to be

SoftwareSerial myserial(rx, tx);                      //define how the soft serial port is going to work

String inputstring = "";                              //a string to hold incoming data from the PC
String sensorstring = "";                             //a string to hold the data from the Atlas Scientific product
boolean input_string_complete = false;                //have we received all the data from the PC
boolean sensor_string_complete = false;               //have we received all the data from the Atlas Scientific product
//int Co2;                                              //used to hold a integer number that is the Co2
char buffer[20];


void setup() {                                        //set up the hardware
  Wire.begin(1);
  Serial.begin(19200);                                 //set baud rate for the hardware serial port_0 to 9600
  myserial.begin(19200);                               //set baud rate for the software serial port to 9600
  Wire.onRequest(Request);
  inputstring.reserve(10);                            //set aside some bytes for receiving data from the PC
  sensorstring.reserve(30);                           //set aside some bytes for receiving data from Atlas Scientific product
}


void serialEvent() {                                  //if the hardware serial port_0 receives a char
  inputstring = Serial.readStringUntil(13);           //read the string until we see a <CR>
  input_string_complete = true;                       //set the flag used to tell if we have received a completed string from the PC
}


void loop() {                                         //here we go...

  if (input_string_complete == true) {                //if a string from the PC has been received in its entirety
    myserial.print(inputstring);                      //send that string to the Atlas Scientific product
    myserial.print('\r');                             //add a <CR> to the end of the string
    inputstring = "";                                 //clear the string
    input_string_complete = false;                    //reset the flag used to tell if we have received a completed string from the PC
  }

  if (myserial.available() > 0) {                     //if we see that the Atlas Scientific product has sent a character
    char inchar = (char)myserial.read();              //get the char we just received
    sensorstring += inchar;                           //add the char to the var called sensorstring
    if (inchar == '\r') {                             //if the incoming character is a <CR>
      sensor_string_complete = true;                  //set the flag
    }
  }


  if (sensor_string_complete == true) {               //if a string from the Atlas Scientific product has been received in its entirety
    Serial.println(sensorstring);                     //send that string to the PC's serial monitor
    sensorstring = "";                                //clear the string
    sensor_string_complete = false;                   //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
  }
}

void Request(){
  sensorstring.toCharArray(buffer, 20);
  Wire.write(buffer);
  delay(100);
  }

This is the code for the NodeMCU:

#include <Wire.h>
char c;
void setup() {
  Wire.begin(D1,D2); 
  Serial.begin(19200);

}

void loop() {
Wire.requestFrom(1,20);    // request 20 bytes from master device

  while (Wire.available()) {              //are there bytes to receive.
      c=Wire.read();
      delay(100);
  }
  Serial.println(c);               //print the data.
  delay(100);
}

Oi! Things changed quickly. Went from serial to I2C. Ok.

Show images of your project including the use of level shifters between the 2 MCU's

Hello, the circuit connections are as follows:

At the moment I am connecting both boards to the USB ports of my PC

I have not directly connected the sensor to the NodeMCU because I plan to connect other sensors

This is the code for the arduino:

#include <SoftwareSerial.h>                           //we have to include the SoftwareSerial library, or else we can't use it
#include <Wire.h>
#define rx 2                                          //define what pin rx is going to be
#define tx 3                                          //define what pin tx is going to be

SoftwareSerial myserial(rx, tx);                      //define how the soft serial port is going to work

String inputstring = "";                              //a string to hold incoming data from the PC
String sensorstring = "";                             //a string to hold the data from the Atlas Scientific product
boolean input_string_complete = false;                //have we received all the data from the PC
boolean sensor_string_complete = false;               //have we received all the data from the Atlas Scientific product
//int Co2;                                              //used to hold a integer number that is the Co2
char buffer[20];


void setup() {                                        //set up the hardware
  Wire.begin(1);
  Serial.begin(19200);                                 //set baud rate for the hardware serial port_0 to 9600
  myserial.begin(19200);                               //set baud rate for the software serial port to 9600
  Wire.onRequest(Request);
  inputstring.reserve(10);                            //set aside some bytes for receiving data from the PC
  sensorstring.reserve(30);                           //set aside some bytes for receiving data from Atlas Scientific product
}


void serialEvent() {                                  //if the hardware serial port_0 receives a char
  inputstring = Serial.readStringUntil(13);           //read the string until we see a <CR>
  input_string_complete = true;                       //set the flag used to tell if we have received a completed string from the PC
}


void loop() {                                         //here we go...

  if (input_string_complete == true) {                //if a string from the PC has been received in its entirety
    myserial.print(inputstring);                      //send that string to the Atlas Scientific product
    myserial.print('\r');                             //add a <CR> to the end of the string
    inputstring = "";                                 //clear the string
    input_string_complete = false;                    //reset the flag used to tell if we have received a completed string from the PC
  }

  if (myserial.available() > 0) {                     //if we see that the Atlas Scientific product has sent a character
    char inchar = (char)myserial.read();              //get the char we just received
    sensorstring += inchar;                           //add the char to the var called sensorstring
    if (inchar == '\r') {                             //if the incoming character is a <CR>
      sensor_string_complete = true;                  //set the flag
    }
  }


  if (sensor_string_complete == true) {               //if a string from the Atlas Scientific product has been received in its entirety
    Serial.println(sensorstring);                     //send that string to the PC's serial monitor
    sensorstring = "";                                //clear the string
    sensor_string_complete = false;                   //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
  }
}

void Request(){
  sensorstring.toCharArray(buffer, 20);
  Wire.write(buffer);
  delay(100);
  }

This is the code for the NodeMCU:

#include <Wire.h>
char c;
void setup() {
  Wire.begin(D1,D2); 
  Serial.begin(19200);

}

void loop() {
Wire.requestFrom(1,20);    // request 20 bytes from master device

  while (Wire.available()) {              //are there bytes to receive.
      c=Wire.read();
      delay(100);
  }
  Serial.println(c);               //print the data.
  delay(100);
}

Do you understand that you need level shifters between the Uno and the ESP?

How to Make a Level Shifter for Arduino Uno | Beginners Guide - OKdo

Anyways, good lick.

Ok, thank you very much, I did not know this part

It was mentioned several times. So, if you have hooked your Uno to the ESP directly then your ESP may be damaged.

Rather than using a UNO to communicate with the NodeMCU it may be simpler to replace the Uno with a 3.3V Arduino with several hardware serial ports, e.g. the Arduino Due
You no longer require level shifters and hardware serial ports replace Software Serial

Why two processors.
The NodeMCU is way more powerful than the Uno.
Can't you connect the CO2 sensor directly to the NodeMCU?
Leo..