1

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)

showing data received

showing action taken

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

5
  • 1
    SoftwareSerial at 115200 baud?
    – Juraj
    Commented Feb 19, 2020 at 14:18
  • 1
    @Juraj Serial monitor is set at 9600 baud , softwareserial i.e sending and receiving data from ESP is set at 4800 baud... so is this the issue? Commented Feb 19, 2020 at 14:20
  • are both SoftwareSerial on 4800 baud?
    – Juraj
    Commented Feb 19, 2020 at 17:09
  • @Juraj Yes, they are! Here's smthg I tried, pls let me know if you can make sense of this: i just ran arduino code(disconnecting power to esp8266 but not the data lines between them) , every time arduino sent data, the while loop in 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 Commented Feb 19, 2020 at 17:43
  • @Juraj I'm begining to think that it's a hardware problem, in my arduino , pins 0-7 are exposed(i.e without those black female connector heads, it just has 'u' shaped pins ) so i used female connectors to connect to them.. I made sure they weren't touching but still every time data was sent, i was getting additional bytes being read . So i thought maybe there's some short circuit and chose 2 pins that are farther away, but now I'm getting additional 16 bytes Commented Feb 19, 2020 at 19:51

1 Answer 1

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
3
  • 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
    Commented 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 esp8266 SoftwareSerial 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 kept Serial.begin(9600) and SoftwareSerial.begin(4800) Commented 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
    Commented Feb 20, 2020 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.