Sending Data from Arduino to nodemcu via serial communication and then to database(firebase)

because of this lockdown situation I can't buy a Arduino that has WIFI inbuild. So I had to use the things that I have at home to complete my project. So I decided to connect my Nodemcu esp8266 to arduino via serial communication and send that data to the firebase. The data comes all good to the Nodemcu and can see that on the serial monitor until I start to send that data to the firebase. when I put the code line that needs to send data to firebase, incoming data from the arduino uno goes crazy. And on firebase realtime database it shows some random numbers. Im a noob at this. I searched every where for a solution but there was non. I think way im saving the serialread data to the variable is also wrong. Can someone please help me? down here is the code I used.

arduino code

#include <dht.h>
#include <SoftwareSerial.h>
dht DHT;

#define DHT11_PIN 4
SoftwareSerial espSerial(5, 6);
String str;
float h, t;
void setup(){
  
  Serial.begin(115200);
  espSerial.begin(115200);
  delay(2000);
}

void loop(){
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  h = DHT.humidity;
  t = DHT.temperature;
  str =String("coming from arduino: ")+String("H= ")+String(h)+String("T= ")+String(t);
  espSerial.println(str);

  delay(2000);
}

nodemcu esp8266 code

#include <ESP8266WiFi.h>                                                    // esp8266 library
#include <FirebaseArduino.h>                                                // firebase librar                                                            // dht11 temperature and humidity sensor library
#define FIREBASE_HOST "######################################"
#define FIREBASE_AUTH "#####################################"
#define WIFI_SSID "###########"
#define WIFI_PASSWORD "##########"
String alldata;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
  delay(1000);                
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);                                     
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}

void loop() { // run over and over
if (Serial.available()) {
Serial.write(Serial.read());
alldata = Serial.read();
}
 Firebase.pushString("/smart pot/data", alldata);
 delay(500);
}

Try these two sketches (you will need to add the Firebase stuff)
UnoSoftSerialCSV.ino (1.9 KB)
ESP8266CSV_SoftSerial.ino (1.8 KB)

1 Like

thank you soo much for your time and effort. I get this error in the serial monitor of my nodemcu esp8266 "Prompt other side to connect". and if the values are changed in the Arduino Uno its needs to be updated. But " if (coms.textToSend.isEmpty()) { " using this line will stop the updating right? And in my nodemcu, their is no pin 13 and 14 pin. you have mentioned in the code that i need to connect them to D7 and D6. Do I need to change that in the code too if I'm connecting those pins to D7 and D6. thank you for you time.

That is normal the ESP is the 'controller' and is prompting the other side to send something or to just say it is there and running ready to receive.
for nodemcu

static const uint8_t D6   = 12;
static const uint8_t D7   = 13;

so just connect RX to D7 and TX to D6 (remember to swap tx-rx, rx-tx between the boards) see this circuit below.

To send just put text into coms.textToSend each time it is sent it is emptied so coms.textToSend.isEmpty() becomes true. BUT you can just keep putting text in every loop and latest text will be sent when the UNO can send.


To test open two (2) instances of the IDE and connect one Monitor to the UNO and one monitor to the ESP then you can see both ends at the same time.

This is the output I get from a slightly modified UNO sketch (not DHT sensor)
UnoSoftSerialCSV.ino (1.9 KB)

Sending 'coming from arduino: H= 60.60T= 26.70'
coming from arduino: H= 60.60T= 26.70

and on the ESP side

Received 'coming from arduino: H= 60.60T= 26.70'
coming from arduino: H= 60.60T= 26.70

Here is a brief write up I am working on

To send data you put in the SafeString coms.textToSend usually by using the print() methods but you can also use the = and += operators to add text. The coms.sendAndRecieve() method will send the text after the other side as finished sending its text. Once the textToSend has been sent it is cleared and coms.textToSend isEmpty() becomes true. So your code can test this to determine when to load the next message to be sent.

The coms.textRecieved is filled with text received from the other side. Send and receive both have a check sum added. If the received check sum is not correct the entire message is discarded. Each time coms.sendAndReceive() is called, it starts by clearing the coms.textRecieved SafeString. If a message has been received it will be returned in coms.textRecieved. So you should test !coms.textReceived.isEmpty() to see if there is a new message. If there is a new message, your code needs to process it or save it then because coms.textRecieved will be cleared when coms.sendAndReceive() is called next loop().

This code is non-blocking, so the loop() will run as fast at it can. If the textToSend is longer the the serial TX buffer size, then the loop will pause in coms.sendAndRecive() until the entire textToSend is output to the serial connecton. For receive, the Serial software will fill the Serial RX buffer with the incoming message and each time coms.sendAndReceive() is called, that RX buffer will be quickly emptied into an internal SerialComs buffer and the loop allowed to continue. Once the last of the incoming message arrives and is read into the internal buffer the check sum is verified and the message (less the check sum and terminating XON) is transferred to coms.textReceived and !coms.textReceived.isEmpty() become true and the message should be processed by the loop() code.

1 Like

This tutorial SerialComs, covers various errors you might encounter and how to fix them

1 Like

omg thank you so much <3. you just saved my life. I cant be thankful enough. <3 <3 <3