- How to send sensor data to ESP8266 using serial communication.
- How to store sensor data in Firebase real-time database.
For demonstration purposes, I use two IR sensors and one ultrasonic sensor but you can use whatever you want sensors.
This is one method to send sensor data to Firebase real-time database using ESP8266. (There are more methods to send sensor data to Firebase real-time database using ESP8266.)
Step 1: Go to this link and connect ESP8266 to Firebase real-time database.All necessary steps include this link.
https://www.hackster.io/pulasthi-Narada/connecting-esp8266-to-firebase-to-send-receive-data-4adf66
Step 2: ESP8266 Code explanation.Set your FIREBASE_HOSTS, FIREBASE_AUTH, WIFI_SSID and WIFI_PASSWORD.
#define FIREBASE_AUTH "Your secret"
#define FIREBASE_HOST "your FIREBASE HOST"
#define WIFI_SSID " your WIFI SSID"
#define WIFI_PASSWORD " your WIFI PASSWORD"
When your copy theFirebase host, remove " https:// "and"/" from the Firebase host.
for example
#define FIREBASE_HOST "werr-f374b.firebaseio.com"
Connect to WIFI code.
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Initializes the serial connection at 9600 to get sensor data from arduino.
Serial.begin(9600);
Get sensor data from Arduino and put it into sensor_data variable.
while(Serial.available()){
//get sensor data from serial
sensor_data=Serial.readString();
Sr=true;
}
Get commas indexes from values variable.
//get comma indexes from values variable
int fristCommaIndex = values.indexOf(',');
int secondCommaIndex = values.indexOf(',', fristCommaIndex+1);
int thirdCommaIndex = values.indexOf(',', secondCommaIndex + 1);
Get sensors data from values variable by splitting by commas and put into variables
//get sensors data from values variable by spliting by commas and put in to variables
String ultrasonic_value = values.substring(0, fristCommaIndex);
String IR_sensor1_value = values.substring(fristCommaIndex+1, secondCommaIndex);
String IR_sensor2_value = values.substring(secondCommaIndex+1);
Store sensor data as string in thefirebase real-time database.
//store ultrasonic sensor data as string in firebase
Firebase.setString("ultrasonic_value",ultrasonic_value);
delay(10);
//store IR sensor 1 data as string in firebase
Firebase.setString("IR_sensor1_value",IR_sensor1_value);
delay(10);
//store IR sensor 2 data as string in firebase
Firebase.setString("IR_sensor2_value",IR_sensor2_value);
Store previous sensors' data as strings in the firebase real-time database.
//store previous sensors data as string in firebase
Firebase.pushString("previous_ultrasonic_value",ultrasonic_value);
delay(10);
Firebase.pushString("previous_IR_sensor1_value",IR_sensor1_value);
delay(10);
Firebase.pushString("previous_IR_sensor2_value",IR_sensor2_value);
Step 3: Connect ESP8266 to Arduino Uno.Connect the pins.
The ESP8266 works with 3.3V and not 5V.
ESP8266:-------------- >Arduino:
GND -------------------------- GND
GPIO-2 (IO2) -------------------- Not connected (open)
GPIO-0 (IO0)---------------------GND
RX --------------------------------RX
TX ---------------------------------TX
CHPD (EN) ------------------------ 3.3V
RST -------------------------------- Not connected (open)
VCC (3v3) -------------------------- 3.3V
connect Arduino Uno RESET Pin to Uno's Ground/GND Pin.
Before you upload ESP8266 code check following instructions.
- Select ESP8266 board from Tools > Board menu.
- Select right port from Tools > Port menu.
- Select 115200 as a upload speed from Tools > upload speed menu.
- Re check your pins.
If you fail to upload code, remove the serial Arduino cable and plug it again and retry to upload code. (retry more than three times)
If you want to upload new code, remove the serial Arduino cable and plug it again and upload the code.
For errors, please check out this video.
Step 5 : Connect your sensors to Arduino.After uploading the ESP8266 code, remove the serial Arduino cable and take out GPIO-0 (IO0) from GND and Arduino Uno RESET Pin from Uno's Ground/GND Pin.
remove ESP8266 RX pin from uno's RX pin and ESP8266 TX pin from uno's TX pin.
For demonstration purposes I use two IR sensors and one ultrasonic sensor but you can use whatever you want sensors.
Connect the pins.
ESP8266:-------------- >Arduino:
GND -------------------------- GND
GPIO-0 (IO0)---------------------Not connected (open)
GPIO-2 (IO2) -------------------- Not connected (open)
RX -------------------------------- Not connected (open)
TX --------------------------------- Not connected (open)
CHPD (EN) ------------------------ 3.3V
RST -------------------------------- Not connected (open)
VCC (3v3) -------------------------- 3.3V
Sensors:-------------- >Arduino:
ultrasonic sensor VCC -------------------------- 5V
ultrasonic sensor GND --------------------------GND
ultrasonic sensor Trig-------------------------- 2
ultrasonic sensor Echo -------------------------- 3
IR sensor1 VCC -------------------------- 5V
IR sensor1 GND --------------------------GND
IR sensor1 AO-------------------------- A1
IR sensor2 VCC -------------------------- 5V
IR sensor2 GND --------------------------GND
IR sensor2 AO-------------------------- A2
Initializes the serial connection at 9600 to sent sensor data to ESP8266.
Serial.begin(9600);
Get sensors data and put in to values variables as a string.
values= (get_distance_Value()+','+get_IR_sensor1_Value()+','+get_IR_sensor2_Value());
Removed any buffered previous serial data.
Serial.flush();
Sent sensors data to serial. (sent sensors data to ESP8266)
Serial.print(values);
Before you upload Arduino Uno code check following instructions.
- Select Arduino Uno board from Tools > Board menu.
- Select right port from Tools > Port menu.
- Take out GPIO-0 (IO0) from GND and Arduino Uno RESET Pin from Uno's Ground/GND Pin.
- Remove ESP8266 RX pin from uno's RX pin and ESP8266 TX pin from uno's TX pin.
- Re check your pins.
After uploading the code, remove the serial Arduino cable and connect ESP8266 RX pin to uno's TX pin and ESP8266 TX pin to uno's RX pin.
After that plug serial Arduino cable.
That's it, you are done!!!check your database.
Also you can use Software serial, if you want.
If your sensors data is scramble in firebase increase delay in code.
example
delay(2500);
https://github.com/FirebaseExtended/firebase-arduino
https://firebase-arduino.readthedocs.io/en/latest/
https://room-15.github.io/blog/2015/03/26/esp8266-at-command-reference/#AT+CWJAP
https://create.arduino.cc/projecthub/ahmedibrrahim/iot-using-esp8266-01-and-arduino-afa35e
Comments