Categories
TUTORIALS

Sending Data To Firebase With The ESP8266 WiFi Module

Purpose:

This tutorial will show you how to use the Arduino to program an ESP8266 WiFi Module. This module can be used to connect your Arduino to the internet.

Parts:

  • Arduino Uno
  • Breadboard
  • ESP8266 WiFi Module
  • ESP8266 ESP-01 Breadboard Adaptor
  • 8 jumper wires

Notes:

  • This circuit requires 3.3V for power. However, it is ideal if you can use an external power source, rather than powering it directly through your Arduino. (For the purposes of this tutorial, I had to use my Arduino.)
  • You must have a Gmail account to create a Firebase account

Circuit:

Connect the ESP8266 WiFi Module to the breadboard adaptor, making sure they are facing away from one another, not overlapping.

Proper orientation of the ESP8266 WiFi module and adaptor

The ESP8266 breadboard adaptor to Arduino connections should be as follows:

On the Arduino itself, GND must be shorted to Reset. This is because if it is not, the Arduino will interfere with code being uploaded from the ESP8266 module. Make sure that this is done before you connect your Arduino to power.

Arduino IDE Setup:

Add Board Driver

Arduino > Preferences > In the “Additional Board Manager URLs” text field, copy the following URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json > OK

Make sure you are connected to the internet you are intending to connect the ESP8266 module to.

Tools > Board > Boards Manager > Scroll to the bottom to reach esp8266 > Choose version 2.5.0 > Install

Restart Arduino IDE once installation is done

Tools > Board > “Generic ESP8266 Module”

Port > Select your correct port

Software Setup:

Install Firebase Library

Download and install Firebase Arduino Library from this link: https://drive.google.com/file/d/0B4tww7CJlZPyUTd1enFxNklKOE0/view

Create Firebase Project

Go to https://console.firebase.google.com/u/0/ and create an account through your Gmail account. Click “Create a Project”

Find Database Host Name & Authentication Key for Arduino Sketch

Go to “Database”

Copy this host name. You’ll paste it into line 5 of the code, replacing “YOUR HOSTNAME HERE”

Go to Project Overview > Project Settings > Service Accounts > Database Secrets

Copy this database secret. You’ll paste it into line 6 of the code, replacing “DATABASE SECRET HERE”

Add WiFi Router Name & Password

In the code below, in lines 7 & 8 (respectively), you’ll replace “YOUR WIFI SSID HERE” and “YOUR WIFI PASSWORD HERE”with your WiFi SSID/name and password.

Arduino Code:

Copy/paste the following code into the Arduino IDE, making sure to perform the above substitutions in lines 5, 6, 7, & 8.

#include <"ESP8266WiFi.h">
#include <<span class="TextRun Highlight BCX0 SCXW174472232" lang="EN-US" xml:lang="EN-US" data-contrast="auto"><span class="SpellingError BCX0 SCXW174472232">FirebaseArduino.h></span></span>

//Replace these strings with your own
#define FIREBASE_HOST "YOUR HOSTNAME HERE" 
#define FIREBASE_AUTH "DATABASE SECRET HERE"
#define WIFI_SSID "YOUR WIFI SSID HERE" 
#define WIFI_PASSWORD "YOUR WIFI PASSWORD HERE" 


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 
 Firebase.setFloat("number", 42.0); 
 // handle error 
 if (Firebase.failed()) { 
     Serial.print("setting /number failed:"); 
     Serial.println(Firebase.error());   
     return; 
 } 
 delay(1000); 
 
 //Update value 
 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); 
} 

References:

Elements of the following tutorials and videos were referenced in creating this tutorial

One reply on “Sending Data To Firebase With The ESP8266 WiFi Module”

Leave a Reply

Your email address will not be published. Required fields are marked *