How can i fix the value in Firebase Realtime Database always return 0?

I have an integrated circuit with Arduino UNO, ESP8266-01 Wifi Module, and HC-SR04 ultrasonic sensor. Since i'm coding this circuit, everything looks normal. Like the code can be compiled very well, the sensor value shows some number via serial monitor, and the database have my sensor request.
The problem is everytime i run the program, my sensor value in realtime database always return 0. I do some research because i think maybe it's my fault or something. But the result always same.
By the way, i have two code sketch. One in my Arduino and the other in my ESP8266-01.
This is code from my Arduino UNO sketch:

#include <SoftwareSerial.h> // library for software serial communication
SoftwareSerial mySerial(2, 3); // (RX, TX)
#define echoPin 5 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 4 // attach pin D3 Arduino to pin Trig of HC-SR04

// defines variables
float duration; // variable for the duration of sound wave travel
float distance; // variable for the distance measurement

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
  mySerial.begin(9600); // Software Serial is starting with 9600 of baudrate speed 
}
void loop() {
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
  // Displays the distance on the Serial Monitor
  Serial.print(distance);
  delay(1000);
}

And this is code from my ESP8266-01:

#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>

#define FIREBASE_HOST "projectsonarsensing-default-rtdb.firebaseio.com"
#define FIREBASE_AUTH "1PM8tlhXsznQ5UyTJlGp0CC08xAAZS0tYxkHAqWj"
#define WIFI_SSID "Faz_69"
#define WIFI_PASSWORD "password7"

void setup() {
  Serial.begin(9600);
  delay(1000);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  //Serial.print("Menyambungkan");
  while (WiFi.status() != WL_CONNECTED) {
    //Serial.print("-");
    delay(500);
  }
  //Serial.println();
  //Serial.print("Terhubung: ");
  //Serial.println(WiFi.localIP());

  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}

float incomingFloat = 0;

void loop() {
  
  boolean floatReady = false;
    while (Serial.available()){
      incomingFloat = Serial.parseFloat();
      floatReady = true;
    }
    delay(1000);
    if (floatReady = true){
      float ultrasonicValue = incomingFloat;
      Firebase.setFloat("ultrasonic_value",ultrasonicValue);
      delay(10);
      Firebase.pushFloat("previous_ultrasonic_value",ultrasonicValue);
      delay(1000);
      if (Firebase.failed()){
          return;
      }
    }
}

Really appreciate any help that this forum can offer for me.
Thanks and have a nice day everyone!

What is the purpose of defining mySerial on the Uno? You do not appear to use it.

Cause everytime i use a common RX,TX (not a software one), my code can't running.
Maybe you know the problem, sir.

Do you see output from this print statement on the serial monitor ?
Can you show a diagram of how the Arduino Uno and the ESP8266-01 are connected together or, if you are following a tutorial, a link to that.

Here is my circuit:


Here is the error code that's show when i use common RXTX:

Arduino: 1.8.19 (Windows 10), Board: "Arduino Uno"

C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\Raihan\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\Raihan\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\Raihan\Documents\Arduino\libraries -fqbn=arduino:avr:uno -vid-pid=1A86_7523 -ide-version=10819 -build-path C:\Users\Raihan\AppData\Local\Temp\arduino_build_578139 -warnings=none -build-cache C:\Users\Raihan\AppData\Local\Temp\arduino_cache_487254 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Users\Raihan\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Users\Raihan\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.avrdude.path=C:\Users\Raihan\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Users\Raihan\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avr-gcc.path=C:\Users\Raihan\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Users\Raihan\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -verbose C:\Users\Raihan\Documents\Arduino\01_ArduinoUNO_STE\01_ArduinoUNO_STE.ino

C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\Raihan\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\Raihan\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\Raihan\Documents\Arduino\libraries -fqbn=arduino:avr:uno -vid-pid=1A86_7523 -ide-version=10819 -build-path C:\Users\Raihan\AppData\Local\Temp\arduino_build_578139 -warnings=none -build-cache C:\Users\Raihan\AppData\Local\Temp\arduino_cache_487254 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Users\Raihan\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Users\Raihan\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.avrdude.path=C:\Users\Raihan\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Users\Raihan\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avr-gcc.path=C:\Users\Raihan\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Users\Raihan\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -verbose C:\Users\Raihan\Documents\Arduino\01_ArduinoUNO_STE\01_ArduinoUNO_STE.ino

Using board 'uno' from platform in folder: C:\Users\Raihan\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6

Using core 'arduino' from platform in folder: C:\Users\Raihan\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6

Detecting libraries used...

"C:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino" "-IC:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\standard" "C:\\Users\\Raihan\\AppData\\Local\\Temp\\arduino_build_578139\\sketch\\01_ArduinoUNO_STE.ino.cpp" -o nul

Alternatives for SoftwareSerial.h: [SoftwareSerial@1.0]

ResolveLibrary(SoftwareSerial.h)

  -> candidates: [SoftwareSerial@1.0]

"C:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino" "-IC:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\standard" "-IC:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\libraries\\SoftwareSerial\\src" "C:\\Users\\Raihan\\AppData\\Local\\Temp\\arduino_build_578139\\sketch\\01_ArduinoUNO_STE.ino.cpp" -o nul

Using cached library dependencies for file: C:\Users\Raihan\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\SoftwareSerial\src\SoftwareSerial.cpp

Generating function prototypes...

"C:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino" "-IC:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\standard" "-IC:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\libraries\\SoftwareSerial\\src" "C:\\Users\\Raihan\\AppData\\Local\\Temp\\arduino_build_578139\\sketch\\01_ArduinoUNO_STE.ino.cpp" -o "C:\\Users\\Raihan\\AppData\\Local\\Temp\\arduino_build_578139\\preproc\\ctags_target_for_gcc_minus_e.cpp"

"C:\\Program Files (x86)\\Arduino\\tools-builder\\ctags\\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\\Users\\Raihan\\AppData\\Local\\Temp\\arduino_build_578139\\preproc\\ctags_target_for_gcc_minus_e.cpp"

Compiling sketch...

"C:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino" "-IC:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\standard" "-IC:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\libraries\\SoftwareSerial\\src" "C:\\Users\\Raihan\\AppData\\Local\\Temp\\arduino_build_578139\\sketch\\01_ArduinoUNO_STE.ino.cpp" -o "C:\\Users\\Raihan\\AppData\\Local\\Temp\\arduino_build_578139\\sketch\\01_ArduinoUNO_STE.ino.cpp.o"

Compiling libraries...

Compiling library "SoftwareSerial"

Using previously compiled file: C:\Users\Raihan\AppData\Local\Temp\arduino_build_578139\libraries\SoftwareSerial\SoftwareSerial.cpp.o

Compiling core...

Using precompiled core: C:\Users\Raihan\AppData\Local\Temp\arduino_cache_487254\core\core_arduino_avr_uno_887041c46c9b1806775d877918dfd42b.a

Linking everything together...

"C:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-gcc" -w -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega328p -o "C:\\Users\\Raihan\\AppData\\Local\\Temp\\arduino_build_578139/01_ArduinoUNO_STE.ino.elf" "C:\\Users\\Raihan\\AppData\\Local\\Temp\\arduino_build_578139\\sketch\\01_ArduinoUNO_STE.ino.cpp.o" "C:\\Users\\Raihan\\AppData\\Local\\Temp\\arduino_build_578139\\libraries\\SoftwareSerial\\SoftwareSerial.cpp.o" "C:\\Users\\Raihan\\AppData\\Local\\Temp\\arduino_build_578139/..\\arduino_cache_487254\\core\\core_arduino_avr_uno_887041c46c9b1806775d877918dfd42b.a" "-LC:\\Users\\Raihan\\AppData\\Local\\Temp\\arduino_build_578139" -lm

"C:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-objcopy" -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 "C:\\Users\\Raihan\\AppData\\Local\\Temp\\arduino_build_578139/01_ArduinoUNO_STE.ino.elf" "C:\\Users\\Raihan\\AppData\\Local\\Temp\\arduino_build_578139/01_ArduinoUNO_STE.ino.eep"

"C:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-objcopy" -O ihex -R .eeprom "C:\\Users\\Raihan\\AppData\\Local\\Temp\\arduino_build_578139/01_ArduinoUNO_STE.ino.elf" "C:\\Users\\Raihan\\AppData\\Local\\Temp\\arduino_build_578139/01_ArduinoUNO_STE.ino.hex"

Using library SoftwareSerial at version 1.0 in folder: C:\Users\Raihan\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\SoftwareSerial 

"C:\\Users\\Raihan\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-size" -A "C:\\Users\\Raihan\\AppData\\Local\\Temp\\arduino_build_578139/01_ArduinoUNO_STE.ino.elf"

Sketch uses 3670 bytes (11%) of program storage space. Maximum is 32256 bytes.

Global variables use 198 bytes (9%) of dynamic memory, leaving 1850 bytes for local variables. Maximum is 2048 bytes.

An error occurred while uploading the sketch

avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00

avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x00

avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x00

avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x00

avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x00

avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x00

avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x00

avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x00

avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x00

avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00

If i use my Software Serial, ultrasonic value is showing in serial monitor like there's not a problem.

Yes. It is not a good idea to use pins 0 and pins 1 on a Uno, if you can avoid it, because of the problems with uploading software and debugging to the Serial console.

I don't know firebase, but it appears that you need to have a method of debugging the output of from the ESP-01 so you can see how far it has got. For example, how do you know that the ESP-01 has successfully established a connection to your Wireless LAN ?

You should probably start with the ESP-01 and only use the Uno when you have the connection with Firebase working. Use the programmer, that you use to load programs onto the ESP-01, to view its serial console and build debug messages into the code so you can see where it is going wrong.

Thanks for your support, sir. Really appreciate it.
Btw something that i know when ESP-01 establish connection with my wifi is in serial monitor.
Did you notice in my ESP-01 code have something like:

//Serial.println();
  //Serial.print("Terhubung: ");
  //Serial.println(WiFi.localIP());

When i un-comment that section, the output will represent the section i mentioned.

OK. It is good that you have a procedure for looking at the serial console of the ESP-01, if required.

In the Uno code you have this, which makes the results from the Ultrasonic sensor available to the ESP-01. SoftwareSerial mySerial(2, 3); // (RX, TX) but you never seem to write anything to mySerial.

But anyway, why not temporarily restructure your ESP-01 code so that it simply sends a test float, say 3.142, to Firebase. That instead of trying to send something from the Uno. View the results on the serial console.

1 Like

Oke, sir. I'll do your recommendation:)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.