Below is the code for getting a value from Firebase and sending the received data to arduino for performing necessary action.
The code here here sends a string "o"
to arduino once
//uploaded to ESP8266
.
.
.
if (cu_st == "c")
{
sent = 0;
}
cu_st = Firebase.getString("feeder");
if (cu_st == "o")
{
Serial.println("\t\trecd req to feed");
if(sent == 0)
{
esp.print("o");
sent =1;
cu_st = "c";
Serial.println("setting feeder to close in firebase");
Firebase.setString("feeder" ,"c");
}
.
.
.
Below is the code that gets the value, carries out necessary actions.
//uploaded to arduino
.
.
.
void feed_cmd()
{
while(ArduinoUno.available())
{
char valve = ArduinoUno.read();
Serial.print("feeder cmd recd ");
Serial.println(valve);
if(valve=='o')
{
digitalWrite(led, HIGH);
//feeder.write(0); //open position
open_time = millis();
feeder = 1 ;
Serial.println("I am opening feeder");
}
}
}
void close_feed()
{
if(feeder == 1)
{
close_time = millis();
if(close_time - open_time > 5000)
{
feeder = 0;
digitalWrite(led , LOW);
Serial.println("I am closing feeder") ;
}
}
}
.
.
.
My expectation/understanding is that:
feed_cmd()
continually checks for any data
As soon as it receives data LED turns ON and after 5 sec close_feed()
turns OFF the LED.
But what happens is that the while()
in feed_cmd()
continually executes showing some random data and also all of what I said above is also being carried out! (pic attached)
Why is there random serial data, even after sending only once and even after reading the sent data?
Is it bad coding / hardware?
Thanks for reading through and would really appreciate it if you can help me sort it out!!
Edit I found this link smthg abt pin voltages when I was looking for possible explanation for my code's behaviour, If somebody can simplify it and explain it, it'd be better !!
1 Answer 1
The baud rates, voltages, and timing all have to match up for serial communication to work right.
So, set both of your devices to the same baudrate (you specify the number in Serial.begin()), and ensure they're connected properly. (voltages match, TX to RX, and ground is connected)
You can just read/print the serial input to the arduino with something like Serial.print(softSerial.read()) in your code if you think you don't have serial wired up right.
Also in an unrelated issue; the esp32 spits out data on its main serial line at 115200 when it boots up which you can not disable. you can use the serial port afterwards though.
Here's a copy of what gets printed out on one of mine;
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:8896
load:0x40080400,len:5816
entry 0x400806ac
-
misread the question as esp32. I think the 8266 spits data out on boot @ 74880 baud, but can't confirm if I did that to the board I was testing, or if it came like that. Other than different baudrate on bootup, the answer is the same.bobsburner– bobsburner2020年02月19日 16:52:35 +00:00Commented Feb 19, 2020 at 16:52
-
here's how i have wrote the code for Softserial : in Arduino
SoftwareSerial ArduinoUno(3,2) // rx | tx
in esp8266SoftwareSerial esp(D2 , D3) //rx | tx
While wiring, I have wired 3 -> D3 and 2 -> D2. As far as power supplies go, I have connected them both to PC's usb port . In both codes I have keptSerial.begin(9600)
andSoftwareSerial.begin(4800)
Somasundharam Sampath– Somasundharam Sampath2020年02月19日 18:19:15 +00:00Commented Feb 19, 2020 at 18:19 -
The baudrate (that number in parentheses after .begin() ) needs to be the same number on both devices. Also, if you have a 5V Uno (by which I mean the high-voltage on a pin is 5V), you could damage your esp8266 by connecting the two together.bobsburner– bobsburner2020年02月20日 09:21:01 +00:00Commented Feb 20, 2020 at 9:21
Explore related questions
See similar questions with these tags.
feed_cmd
gave out 23 bytes(i modified the above code and got 255 or 254 as output to softserial.read() ) then I removed those data lines and saw that while sending data, the while loop no longer got executed