Overview:
You’ll program the ESP8266-01 board to send data from your serial monitor to a Firebase Realtime data base.
The highlevel steps are:
- Create a Firebase Realtime Database
- Wire your circuit
- Program your ESP8266
- Connect your data stream to a web or mobile app
Why Firebase?
Sending sensed data to Firebase allows you to connect with web and mobile apps, and gives you access to Google Analytics. It also offers a lot of flexibility in terms of what you can do with the data—exporting to BigQuery, running machine learning analyses, etc.
Why ESP8266-01?
It’s cheap! A better option for more elaborate sensing projects is the ESP8266-12E. In fact, this kind of Wifi development board would work as a standalone (you wouldn’t need a separate Arduino board).
Parts needed:
- Elegoo Uno R3
- ESP8266-01
- F-M and M-M wires
- Breadboard
- USB cable
- 100k Ohm resistor (optional)
Step 1: Create the Firebase Realtime database
This is where the data you send from the ESP8266 will live, and can be pulled from to populate a web or mobile app.
- Create a Firebase Account
- Click Add project and create a Realtime Database
2. Use test mode as a Security rule
3. Go to Project Overview, Project Settings, Service Accounts, Database secrets, and copy your Database and Secret key. Save these to use in your sketch later on.
Step 2: Wire your Circuit
- Watch this tutorial on how to correctly wire the ESP8266. Below is the diagram as well as pictures of the boards.
2. Double check that the Arduino ground is wired to reset. This make sure you’re programming the ESP8266 and not the Arduino. NOTE: the ESP8266 uses the 3.3V pin, not the 5V pin.
Step 3: Program your ESP8266
- Download Arduino IDE if you haven’t already.
- Install the ESP8266 Board to Arduino IDE (reminder, you’re programming the ESP, not the Arduino board).
- File -> Preferences
- Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into the Additional Board Manager URLs field.
- After that open Boards Manager from Tools > Board menu and install esp8266 platform.
- Select ESP8266 board from Tools > Board menu after installation.
3. Add the Firebase library.
- Download it from this link.
- Open Sketch > include library > add.ZIP library and add ZIP library to Arduino IDE.
- After that go to Sketch > Include Library Verify that the library you just added available in the list.
4. Add the ArduinoJson Library.
- Go to Tools -> Manage Libraries.
- Install ArduinoJson version 5.13.5 library.
5. Add the code below into a sketch. Replace the Firebase and Wifi credentials with your own.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
// // Copyright 2015 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // FirebaseDemo_ESP8266 is a sample that demo the different functions // of the FirebaseArduino API. #include <ESP8266WiFi.h> #include <FirebaseArduino.h> //Replace these strings with your own #define FIREBASE_HOST "YOURFIREBASEHOST" #define FIREBASE_AUTH "YOURSECRETKEY" #define WIFI_SSID "your WIFI SSID" #define WIFI_PASSWORD "your WIFI PASSWORD" void setup() { Serial.begin(9600); // connect to wifi. WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.print("connecting"); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println(); Serial.print("connected: "); Serial.println(WiFi.localIP()); Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); } int n = 0; void loop() { //Set value to send to Firebase database Firebase.setFloat("number", 42.0); // handle error if (Firebase.failed()) { Serial.print("setting /number failed:"); Serial.println(Firebase.error()); return; } delay(1000); //Update value sent to Firebase database Firebase.setFloat("number", 43.0); //Handle error if (Firebase.failed()) { Serial.print("setting /number failed:"); Serial.println(Firebase.error()); return; } delay(1000); //Get value Serial.print("number: "); Serial.println(Firebase.getFloat("number")); delay(1000); //Remove value Firebase.remove("number"); delay(1000); //Set string value Firebase.setString("message", "hello world"); // handle error if (Firebase.failed()) { Serial.print("setting /message failed:"); Serial.println(Firebase.error()); return; } delay(1000); //Set bool value Firebase.setBool("truth", false); //Jandle error if (Firebase.failed()) { Serial.print("setting /truth failed:"); Serial.println(Firebase.error()); return; } delay(1000); //Append a new value to /logs String name = Firebase.pushInt("logs", n++); //Handle error if (Firebase.failed()) { Serial.print("pushing /logs failed:"); Serial.println(Firebase.error()); return; } Serial.print("pushed: /logs/"); Serial.println(name); delay(1000); } |
6. Double check that you’ve:
- Selected the ESP8266 board from Tools > Board
- Selected the correct port from Tools > Port Menu
7. Upload your code to the ESP8266 and inevitably troubleshoot.
- If you’re getting something like “error: espcomm_upload_mem failed”, watch this video.
- If you’re getting gibberish in your serial monitor, check that the baud rate is 9600.
- If you’re seeing “setting /number failed:” in your serial monitor, watch this video.
You should see this at the bottom of your sketch when it uploads:
And you should see this in your Serial Monitor (Tools > Serial Monitor):
8. Check out your data in your Firebase database!
Step 4: Connect your data to a web or mobile app
References:
Google Firebase & ESP8266 Complete Guide – Sending/Receiving Data from ESP8266 & Firebase
Connecting ESP8266 to Firebase to Send & Receive Data
ESP8266 DHT11/DHT22 Temperature and Humidity Web Server with Arduino IDE
Easy Way to Program an ESP8266
How to fix esp8266 error upload message arduino
[SOLVED]Setting/number failed problem || Google firebase || firebase