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 !!
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