Day 19- Arduino Code for Esp8266 Firebase Home Automation
What we will Learn Today
1. Install relative Library for firebase master esp8266
2. Arduino Code configure with firebase URL and Secret key
3. 4ch IOT Home automation Circuit Diagram
How to install library
Go to Library manager in Arduino ide > Tools> Manage Libraries
install ' firebase arduino client library for esp8266 and esp32
//////////////////////////////////////////////////////////// Arduino Code //////////////////////////////////////
#include <Arduino.h>
#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <Firebase_ESP_Client.h>
//Provide the token generation process info.
#include "addons/TokenHelper.h"
//Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"
// Insert your network credentials
#define WIFI_SSID "------------------"
#define WIFI_PASSWORD "--------------"
#define API_KEY "--------------------"
#define DATABASE_URL "---------------"
//Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
//some importent variables
String sValue1, sValue2, sValue3, sValue4;
bool signupOK = false;
void setup() {
Serial.begin(115200);
pinMode(D2,OUTPUT);
pinMode(D1,OUTPUT);
pinMode(D6,OUTPUT);
pinMode(D5,OUTPUT);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
/* Assign the api key (required) */
config.api_key = API_KEY;
/* Assign the RTDB URL (required) */
config.database_url = DATABASE_URL;
/* Sign up */
if (Firebase.signUp(&config, &auth, "", "")) {
Serial.println("ok");
signupOK = true;
}
else {
Serial.printf("%s\n", config.signer.signupError.message.c_str());
}
/* Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
}
void loop() {
if (Firebase.ready() && signupOK ) {
/////////////////////////////////////////////////////
if (Firebase.RTDB.getString(&fbdo, "/L1")) {
if (fbdo.dataType() == "string") {
sValue1 = fbdo.stringData();
int a = sValue1.toInt();
Serial.println(a);
if (a == 1){digitalWrite(D2,HIGH);}
else{digitalWrite(D2,LOW);}}}
else {Serial.println(fbdo.errorReason());}
////////////////////////////////////////////////////
if (Firebase.RTDB.getString(&fbdo, "/L2")) {
if (fbdo.dataType() == "string") {
sValue2 = fbdo.stringData();
int b = sValue2.toInt();
Serial.println(b);
if (b == 1){digitalWrite(D1,HIGH);}
else{digitalWrite(D1,LOW);}}}
else {Serial.println(fbdo.errorReason());}
//////////////////////////////////////////////////
if (Firebase.RTDB.getString(&fbdo, "/L3")) {
if (fbdo.dataType() == "string") {
sValue3 = fbdo.stringData();
int c = sValue3.toInt();
Serial.println(c);
if (c == 1){digitalWrite(D6,HIGH);}
else{digitalWrite(D6,LOW);}}}
else {Serial.println(fbdo.errorReason());}
///////////////////////////////////////////////////////////
if (Firebase.RTDB.getString(&fbdo, "/L4")) {
if (fbdo.dataType() == "string") {
sValue4 = fbdo.stringData();
int d = sValue4.toInt();
Serial.println(d);
if (d == 1){digitalWrite(D5,HIGH);}
else{digitalWrite(D5,LOW);}}}
else {Serial.println(fbdo.errorReason());}
////////////////////////////////////////////////////////////
}
}
////////////////////////////////////END/////////////////////////Update the above code with your available ssid and password
copy paste database secret key in API key section
past database url without http// in DATABASE_URL"......"
------------------------------------------------------------------------------------
Upload the above code with mentioned corrections in your NodeMCU
and connect it as per following diagram
No comments:
Post a Comment