overview
In this section, I will show you how we control every device via the internet from anywhere, combining ESP8266 and Firebase.
Prepare
1. Firebase account and create database (you can google and do it yourself. It’s very simple), create a value as shown below.
2. Copy FIREBASE_HOST and FIREBASE_AUTH in the databasesecrets section.
3. Install Arduino for programming ESP8266. Details you refer here
4. Set the library to use firebase, you can download it here
5. Install the hardware as shown in the first part. With changing the light bulb with any other equipment such as television, fan, refrigerator, …
It’s done. Turn on the Arduino and code.
Program
1. Set parameters
1 2 3 4 5 6 | #define FIREBASE_HOST "your-project.firebaseio.com" // the project name address from firebase id<br> #define FIREBASE_AUTH " your-secret key generated" // the secret key generated from firebase<br> #define WIFI_SSID "xxxxxxxxxxxxx" // nhập tên wifi của bạn<br> #define WIFI_PASSWORD "xxxxxxxxxxxxxx" // mật khẩu của wifi ssid<br> #define PIN_CONTROL D3 // Chân dữ liệu nối ra modul relay<br> |
- Read data from Firebase
- Connect ESP8266 to your home wifi.
12 WiFi.begin (WIFI_SSID, WIFI_PASSWORD).
- Wait for the connection to succeed
12345 while (WiFi.status() != WL_CONNECTED) {Serial.print(".");delay(500);}
- Get data from firebase
12 status = Firebase.getString("status"); // lấy đầu vào trạng thái từ firebase
- Check if the sequence is “on” or “off” to decide whether to close or disconnect the relay to control the device
12345678 if(status== "ON" || status== "on") {Serial.println(" Turned ON");digitalWrite(PIN_CONTROL, HIGH);}else{digitalWrite(PIN_CONTROL,LOW);}
summary
So I have shown you how to connect ESP8266 with Firebase. From the following article, you only need to care how to change the “status” value on firebase to turn off the device, to become “smart”. No need to care about that rudimentary module, all rich ideas start here. Remember to follow the next article.