Using Firebase to Send Sensor Data From One Arduino to Another
Send Sensor Data from One Arduino to Another Using Firebase
In this tutorial, we will learn how to send sensor data from one Arduino to another using Firebase. The process of doing this involves two parts: configuring the Arduino to send data to Firebase and then receiving data from Firebase with the other Arduino.
Part 1: Setting up the Sending Arduino
The first step is to configure the sending Arduino. As an example, we will use an Arduino Uno with a DHT11 temperature and humidity sensor. First, connect your Arduino Uno to your computer via USB cable. Then, open the IDE and upload the code below. Make sure to change the WIFI SSID and password to match your home network.
// Include the Firebase library
#include <FirebaseArduino.h>
// Set up your network credentials
const char* ssid = "YourNetworkSSID";
const char* password = "YourNetworkPassword";
// Set up pins for DHT11
#define DHTPIN 2
#define DHTTYPE DHT11
// Create a DHT object
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Connect to WiFi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
// Print out IP address
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Begin Firebase setup
Firebase.begin("https://your-firebase-app.firebaseio.com/","YourFirebaseSecret");
Serial.println("Firebase began");
}
void loop() {
// Read data from the sensor
float h = dht.readHumidity();
float t = dht.readTemperature();
// Send the data to Firebase
Firebase.setFloat("temperature", t);
Firebase.setFloat("humidity", h);
Serial.println("Data sent to Firebase");
// Wait 10 seconds before sending more data
delay(10000);
}
Once you’ve uploaded the code, your Arduino will start sending data to Firebase. You can now move on to setting up the receiving Arduino.
Part 2: Setting up the Receiving Arduino
The second part is to set up the receiving Arduino. In this case, we will use an Arduino Uno with an LCD display. Connect your Arduino Uno to your computer via USB cable. Then, open the IDE and upload the following code. Remember to change the WIFI SSID and password to match your home network.
// Include the Firebase library
#include <FirebaseArduino.h>
// Set up your network credentials
const char* ssid = "YourNetworkSSID";
const char* password = "YourNetworkPassword";
void setup(){
// Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Connect to WiFi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
// Print out IP address
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Begin Firebase setup
Firebase.begin("https://your-firebase-app.firebaseio.com/","YourFirebaseSecret");
Serial.println("Firebase began");
}
void loop() {
// Read data from Firebase
float t = Firebase.getFloat("temperature");
float h = Firebase.getFloat("humidity");
// Display data on LCD
lcd.clear();
lcd.print("Temperature:");
lcd.setCursor(0,1);
lcd.print(t);
lcd.setCursor(10,1);
lcd.print("Humidity:");
lcd.setCursor(10,1);
lcd.print(h);
// Wait 10 seconds before reading more data
delay(10000);
}
Now your Arduino will read data from Firebase and display it on the LCD. Congratulations! You’ve now learned how to send sensor data from one Arduino to another using Firebase.