0% found this document useful (0 votes)
2 views3 pages

Message 3

This document is an Arduino sketch that connects an ESP8266 microcontroller to Wi-Fi and a Firebase Realtime Database. It retrieves the status of various home devices and sends sensor data (MQ9, temperature, and humidity) to Firebase. The code includes error handling for both data retrieval and sending operations.

Uploaded by

hm42581
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Message 3

This document is an Arduino sketch that connects an ESP8266 microcontroller to Wi-Fi and a Firebase Realtime Database. It retrieves the status of various home devices and sends sensor data (MQ9, temperature, and humidity) to Firebase. The code includes error handling for both data retrieval and sending operations.

Uploaded by

hm42581
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <ESP8266WiFi.

h>
#include <FirebaseESP8266.h>

// Wi-Fi credentials
#define WIFI_SSID "D4C"
#define WIFI_PASSWORD "inkycrane123"

// Firebase project credentials


#define FIREBASE_HOST "https://ay7aga-11594-default-rtdb.firebaseio.com/" //
Firebase Realtime Database URL
#define FIREBASE_AUTH "gFEU9tzvDbUMMTUBlgs5PSCOjZ0e3uKhfplBomKU" //
Database Secret

// Create FirebaseConfig and FirebaseAuth objects


FirebaseConfig config;
FirebaseAuth auth;
FirebaseData firebaseData;

void setup() {
Serial.begin(115200);

// Connect to Wi-Fi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Connected to Wi-Fi");

// Configure Firebase
config.host = FIREBASE_HOST;
config.signer.tokens.legacy_token = FIREBASE_AUTH;

// Initialize Firebase
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);

// Check Firebase connection


if (Firebase.ready()) {
Serial.println("Connected to Firebase");
} else {
Serial.println("Failed to connect to Firebase");
}
}

void loop() {
float mq9 = 24.571;
float temp = 38.3;
float humidity = 10.2;

if (Firebase.getBool(firebaseData, "home_led/status")) {
Serial.println("Data recieved successfully");
bool home_led = firebaseData.boolData();
Serial.println(home_led);
} else {
Serial.print("Failed to receive data: ");
Serial.println(firebaseData.errorReason());
}
if (Firebase.getBool(firebaseData, "garage_led/status")) {
Serial.println("Data recieved successfully");
bool garage_led = firebaseData.boolData();
Serial.println(garage_led);
} else {
Serial.print("Failed to receive data: ");
Serial.println(firebaseData.errorReason());
}
if (Firebase.getBool(firebaseData, "home_door/status")) {
Serial.println("Data recieved successfully");
bool home_door = firebaseData.boolData();
Serial.println(home_door);
} else {
Serial.print("Failed to receive data: ");
Serial.println(firebaseData.errorReason());
}
if (Firebase.getBool(firebaseData, "garage_door/status")) {
Serial.println("Data recieved successfully");
bool garage_door = firebaseData.boolData();
Serial.println(garage_door);
} else {
Serial.print("Failed to receive data: ");
Serial.println(firebaseData.errorReason());
}
if (Firebase.getBool(firebaseData, "fan/status")) {
Serial.println("Data recieved successfully");
bool fan = firebaseData.boolData();
Serial.println(fan);
} else {
Serial.print("Failed to receive data: ");
Serial.println(firebaseData.errorReason());
}
if (Firebase.getBool(firebaseData, "hood/status")) {
Serial.println("Data recieved successfully");
bool hood = firebaseData.boolData();
Serial.println(hood);
} else {
Serial.print("Failed to receive data: ");
Serial.println(firebaseData.errorReason());
}
if (Firebase.getBool(firebaseData, "window_one/status")) {
Serial.println("Data recieved successfully");
bool window_one = firebaseData.boolData();
Serial.println(window_one);
} else {
Serial.print("Failed to receive data: ");
Serial.println(firebaseData.errorReason());
}
if (Firebase.getBool(firebaseData, "window_two/status")) {
Serial.println("Data recieved successfully");
bool window_two = firebaseData.boolData();
Serial.println(window_two);
} else {
Serial.print("Failed to receive data: ");
Serial.println(firebaseData.errorReason());
}
if (Firebase.getBool(firebaseData, "plug_in_one/status")) {
Serial.println("Data recieved successfully");
bool plug_in_one = firebaseData.boolData();
Serial.println(plug_in_one);
} else {
Serial.print("Failed to receive data: ");
Serial.println(firebaseData.errorReason());
}
if (Firebase.getBool(firebaseData, "plug_in_two/status")) {
Serial.println("Data recieved successfully");
bool plug_in_two = firebaseData.boolData();
Serial.println(plug_in_two);
} else {
Serial.print("Failed to receive data: ");
Serial.println(firebaseData.errorReason());
}

if (Firebase.setFloat(firebaseData, "mq9/value",mq9)) {
Serial.println("Data Sent successfully");

} else {
Serial.print("Failed to Send data: ");
Serial.println(firebaseData.errorReason());
}

if (Firebase.setFloat(firebaseData, "temp/value",temp)) {
Serial.println("Data Sent successfully");

} else {
Serial.print("Failed to Send data: ");
Serial.println(firebaseData.errorReason());
}

if (Firebase.setFloat(firebaseData, "humidity/value",humidity)) {
Serial.println("Data Sent successfully");

} else {
Serial.print("Failed to Send data: ");
Serial.println(firebaseData.errorReason());
}

You might also like