So I am using Arduino UNO R3 WiFi ATMega328P + ESP8266 for a project and I want to send data collected using ATMega328P to ESP8266 so that I can send the data to firebase. However, I am noticing that using Serial class functions, The data transfer between the two chips seems to be not working(I am using the correct DIP switch config). I'll paste the code here for any possible bugs.
edit 1: when using for firebase output for debugging, i realised that there's no data that the Serial.available() function could read.
ATMega328P:
int x = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
x++;
String message = "The number is: " + String(x);
Serial.write(message.c_str());
Serial.println("");
delay(1500);
}
ESP8266:
#include <ESP8266WiFi.h>
#include <ESP8266Firebase.h>
#include <SoftwareSerial.h>
Firebase firebase("https://isaac-e9f39-default-rtdb.firebaseio.com/");
void setup()
{
Serial.begin(9600);
Serial.setTimeout(10);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(1000);
Serial.println();
WiFi.begin("OnePlus 8", "kanishk23");
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
// ---------------
firebase.setString("Test/question", "Is it really working?");
firebase.setString("Test/answer", "No");
firebase.setString("Test/answer", "Yes!");
}
void loop() {
delay(5000);
//String message ="";
//Serial.write("abc");
if (Serial.available() > 0) { // Check if data is available before reading
String message = Serial.readStringUntil('\n'); // Read until newline
message.trim(); // Remove leading/trailing whitespace
Serial.println(message);
if (message.length() > 0) { // Only set if message is not empty
firebase.setString("Test/message", message);
Serial.println("Message received: " + message); // Acknowledge receipt
} else {
Serial.println("Empty message received.");
}
}
else {
Serial.println("No data available on serial port.");
//message = "Hello, sorry no data available on serial port right now.";
//firebase.setString("Test/message", message);
}
}
It could be the firebase connection that is not working.
First you should test the ESP sketch, by loading an empty sketch onto the UNO and leaving the dip switch settings the same as for an upload to the ESP. That way you should be able to use the Serial monitor to communicate with the ESP and you can verify that it is doing what you expect it to do.
The esp sketch is echoing back, but there is no way of knowing what response it is sending to the UNO since that does not have a an open channel to the Serial monitor anymore.
This will make it hard to debug. You could add a USB to TTL converter icw swSerial to the UNO just so you can see what is going on. Alternately you can use 'Serial1' on the ESP which can be used for TX only on the ESP and is connected to GPIO 2
I tried the ESP sketch as well. I tried to just read and write data using Serial Monitor on ESP alone. Then even Serial.available() wasn't able to read any data I sent using Serial.write() function i used in the code. However when sending a message to ESP32 on the Serial monitor, it was able to read that. and print it.
void loop() {
delay(5000);
Serial.write("abc");
if (Serial.available() > 0) { // Check if data is available before reading
String message = Serial.readStringUntil('\n'); // Read until newline
message.trim(); // Remove leading/trailing whitespace
Serial.println(message);
}
else {
Serial.println("No data available on serial port.");
}
}
If you expect that what you send using Serial.write() will result in Serial.available() giving you a value different from 0, your understanding is wrong.
What you send using Serial.write() will show in the serial monitor; what you type in serial monitor (and after pressing the send button in serial monitor) will go to the Arduino and Serial.available() will not be zero. But serial monitor will not echo back what you did send from the Arduino.
What I see in serial monitor with your code from post #3
10:59:54.742 -> abcNo data available on serial port.
10:59:59.753 -> abcNo data available on serial port.
11:00:04.731 -> abcNo data available on serial port.
11:00:09.740 -> abcNo data available on serial port.
11:00:14.717 -> abcNo data available on serial port.
11:00:19.727 -> abcNo data available on serial port.
11:00:24.704 -> abcxyz
11:00:30.703 -> abcNo data available on serial port.
I did type xyz in the serial monitor at 11:00:24:704 (or so).
To test the communication between the 328P and the ESP8266, you can use the below two sketches.
328P
const uint8_t ledPin = LED_BUILTIN;
void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop()
{
static uint8_t value = 0;
// send the value to the nESP8266
Serial.write(value);
// wait for reply from ESP8266
while (Serial.available() == 0);
// read received byte
uint8_t b = Serial.read();
if (b == 0)
{
digitalWrite(ledPin, LOW);
}
else
{
digitalWrite(ledPin, HIGH);
}
// toggle value between 0 and 1
if (value == 0)
{
value = 1;
}
else
{
value = 0;
}
// wait a bit
delay(1000);
}
ESP8266
void setup()
{
Serial.begin(115200);
}
void loop()
{
// if there is data from the 328P
if(Serial.available())
{
// echo it back
Serial.write(Serial.read());
}
}
If communication works as it should (your dip switches must be set correctly), the 328P will get the transmitted byte back from the ESP2866 and the 328P react on that by switching the LED on or off. The effect should basically be a blinking LED on the Uno side.