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
#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
}
}
#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);
}
#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);
}
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