0% found this document useful (0 votes)
261 views

Firebase Arduino

The document describes sending temperature sensor data from an Arduino to a NodeMCU and then to Firebase. It includes: 1) Code to read temperature data from an LM35 sensor using an Arduino and send it via serial to a NodeMCU. 2) Instructions for setting up the NodeMCU to receive the data using WiFi and libraries. 3) Code for the NodeMCU to connect to Firebase, receive the temperature readings from the Arduino, and store the data in the Firebase realtime database. This allows monitoring of temperature sensor readings in real-time via the Internet of Things.

Uploaded by

drut
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
261 views

Firebase Arduino

The document describes sending temperature sensor data from an Arduino to a NodeMCU and then to Firebase. It includes: 1) Code to read temperature data from an LM35 sensor using an Arduino and send it via serial to a NodeMCU. 2) Instructions for setting up the NodeMCU to receive the data using WiFi and libraries. 3) Code for the NodeMCU to connect to Firebase, receive the temperature readings from the Arduino, and store the data in the Firebase realtime database. This allows monitoring of temperature sensor readings in real-time via the Internet of Things.

Uploaded by

drut
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Internet of Things

• Sending Data from Arduino to NodeMCU


• Sending Data from NodeMCU to Firebase
Temperature Sensor with Arduino
Code
//Initialization of variables

// Define the analog pin, the LM35's Vout pin is connected to


#define AsensorPin A0

String data1;
int temp;

void setup() {
Serial.begin(115200);
}
Code
void loop() {
int reading = analogRead(AsensorPin);

// Convert that reading into voltage


float voltage = reading * (5.0 / 1024.0);

// Convert the voltage into the temperature in Celsius


temp = voltage * 100;
String str = String(temp);
Serial.println(str);
delay(1000);
}
Sending Data from Arduino to NodeMCU
Physical Connections

NodeMCU to Arduino
NODEMCU ARDUINO
• RX ------------------------- 11 (TX)
• TX ------------------------- 10 (RX)
• GND -------------------------- GND
SETTING UP NODEMCU
GO TO FILE > PREFERENCES > ADDITIONAL BOARD MANAGER URL
and paste

https://github.com/esp8266/Arduino/releases/download/2.7.4/
package_esp8266com_index.json
TOOLS > BOARD MANAGER

TYPE ESP8266 AND INSTALL THE COMMUNITY


INSTALLING ESP8266 Community
INSTALLING SerialTransfer.h
Sending Data From Arduino To NodeMCU
//Arduino to NodeMCU Lib
#include <SoftwareSerial.h>
#include <ArduinoJson.h>

//Initialise Arduino to NodeMCU (10=Rx & 11=Tx)


SoftwareSerial nodemcu(10, 11);

//Initialization of variables
// Define the analog pin, the LM35's Vout pin is connected to
#define AsensorPin A0
Sending Data From Arduino To NodeMCU
String data1;
int temp;

void setup() {
Serial.begin(115200);
nodemcu.begin(115200);
//Serial.println("Program started");
}
Sending Data From Arduino To NodeMCU
void loop() {

StaticJsonDocument<1000> doc;
int reading = analogRead(AsensorPin);

// Convert that reading into voltage


float voltage = reading * (5.0 / 1024.0);

// Convert the voltage into the temperature in Celsius


temp = voltage * 100;
String str = String(temp);
Serial.println(str);
Sending Data From Arduino To NodeMCU

// Assign collected data to JSON Object


doc["temperature"] = str;

//Send data to NodeMCU


serializeJson(doc, nodemcu);
nodemcu.println(str);

delay(1000);
}
Errors You may face
StaticJsonBuffer is a class from ArduinoJson 5. Please see
https://arduinojson.org/upgrade to learn how to upgrade your program
to ArduinoJson version 6

To fix these upgrading issues regarding JSON Version


• https://arduinojson.org/v6/doc/upgrade/
• https://youtu.be/l8TYLFbo9ws
PART II
SETTING UP NODEMCU
Adding FirebaseESP8266.h
Goto library manager and type firebase and select firebase esp8266 client and click on
install
Adding FirebaseESP8266.h
Creating Firebase Realtime Database
Explore the following link to set up real-time database :
https://create.arduino.cc/projecthub/pibots555/iot-using-firebase-and-
nodemcu-48a1fd

• Step by Step Demonstration


• Create Firebase Project :  Go to 
“https://console.firebase.google.com/u/0/?pli=1”, 
• Click on Add Project and name your project
Creating Firebase Realtime Database
• Create Firebase Project :  Go to 
“https://console.firebase.google.com/u/0/?pli=1”, 
• Click on Add Project and name your project
Creating Firebase Realtime Database
• Enable Google analytics of your project and create project
Creating Firebase Realtime Database
Setting up • Goto Build>Authentication
• Get Started > Email/Password
Authentication
Setting up • Goto Build>Authentication
• Get Started > Email/Password
Authentication
Setting up • Enable Email/Password
• Click on “Save”
Authentication
Setting up Authentication
• Goto Users
• Click on add User (First Time there would be no identifier created so
you’ll see blank screen)
Setting up Authentication
• Type Email ad dress(with which you are signed into firebase project) and
password and click on add user.
• Remember these credentials as we would later use them in
programming.
Setting up Authentication
• Type Email ad dress(with which you are signed into firebase project) and
password and click on add user.
• Remember these credentials as we would later use them in
programming.
Creating Firebase Realtime Database
• Goto BUILD > Realtime Database
Creating Firebase Realtime Database
• Goto BUILD > Realtime Database
• Select “set in a test mode”
Ready to Go
Let’s Go to Programming again!!
Adding Firebase
Details in a
Code(NodeMcu)
•/* 1. Define the WiFi credentials
*/
•#define WIFI_SSID "Hotspot"
•#define WIFI_PASSWORD
"abcd1234“’

•/* 2. Define the API Key */


•#define API_KEY "AIzaSyA-
PJuQHVk1c7G4aYUZy89_mI-
z69EM0H4“
•** Goto Project Overview
>Project Settings
Adding Firebase Details in a Code(NodeMcu)

/* 3. Define the RTDB URL */


#define DATABASE_URL "arduino-temp-nodemcu-default-rtdb.firebaseio.com" //Your
Firebase Project URL goes here without "http:" , "\" and "/“
Adding Firebase
Details in a
Code(NodeMcu) •/* 4. Define the user Email and password that
alreadey registerd or added in your project */
Adding Firebase Details in a Code(NodeMcu)
/* 1. Define the WiFi credentials */
#define WIFI_SSID "Hotspot"
#define WIFI_PASSWORD "abcd1234“’

/* 2. Define the API Key */


#define API_KEY "AIzaSyA-PJuQHVk1c7G4aYUZy89_mI-z69EM0H4"

/* 3. Define the RTDB URL */


#define DATABASE_URL "arduino-temp-nodemcu-default-rtdb.firebaseio.com" //<databaseName>.firebaseio.com
or <databaseName>.<region>.firebasedatabase.app

/* 4. Define the user Email and password that alreadey registerd or added in your project */
#define USER_EMAIL "[email protected]"
#define USER_PASSWORD "arduino123"
Code Explanation
NodeMCU code
FirebaseData fbdo;

FirebaseAuth auth;
FirebaseConfig config;

unsigned long sendDataPrevMillis = 0;

unsigned long count = 0;


NodeMCU code
void setup()
{

Serial.begin(115200); // Select the same baud rate if you want to see the datas on
Serial Monitor

WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
NodeMCU code
void setup()
{
………………………….
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();

Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);


}
NodeMCU code
void loop()
{
//For Receiving Data
DynamicJsonDocument data(2048);
DeserializationError error = deserializeJson(data, Serial);

if (error)
{
return;
}
NodeMCU code
else{
Serial.println("JSON Object Recieved");
Serial.print ("Recieved Temperature: ");
float data1 = data["temperature"];
String temp = String(data1);
Serial.println(temp);
NodeMCU code
//SENDING THE RECEIVED DATA TO FIREBASE
if (Firebase.ready())
{
sendDataPrevMillis = millis();

Serial.printf ("Set bool... %s\n", Firebase.setBool(fbdo, F("/test/bool"), count


% 2 == 0) ? "ok" : fbdo.errorReason().c_str());

Serial.printf("Get bool... %s\n", Firebase.getBool(fbdo, F("/test/bool")) ?


fbdo.to<bool>() ? "true" : "false" : fbdo.errorReason().c_str());
NodeMCU code
Serial.printf("Set int... %s\n", Firebase.setInt(fbdo, F("/test/int"), count) ? "ok"
: fbdo.errorReason().c_str());

Serial.printf("Get int... %s\n", Firebase.getInt(fbdo, F("/test/int")) ?


String(fbdo.to<int>()).c_str() : fbdo.errorReason().c_str());

int iVal = 0;
Serial.printf("Set float... %s\n", Firebase.setFloat(fbdo, F("/test/float"),
count + 10.2) ? "ok" : fbdo.errorReason().c_str());

Serial.printf("Get float... %s\n", Firebase.getFloat(fbdo, F("/test/float")) ?


String(fbdo.to<float>()).c_str() : fbdo.errorReason().c_str());
NodeMCU code
Serial.printf("Set double... %s\n", Firebase.setDouble(fbdo, F("/test/double"),
count + 35.517549723765) ? "ok" : fbdo.errorReason().c_str());

Serial.printf("Get double... %s\n", Firebase.getDouble(fbdo, F("/test/double")) ?


String(fbdo.to<double>()).c_str() : fbdo.errorReason().c_str());

Serial.printf("Set string... %s\n", Firebase.setString(fbdo,


F("/test/Temperature"), temp) ? "ok" : fbdo.errorReason().c_str());

Serial.printf("Get string... %s\n", Firebase.getString(fbdo,


F("/test/Temperature")) ? fbdo.to<const char *>() : fbdo.errorReason().c_str());
NodeMCU code
// For the usage of FirebaseJson, see
examples/FirebaseJson/BasicUsage/Create_Parse_Edit.ino

FirebaseJson json;
if (count == 0)
{
json.set("value/round/" + String(count), F("cool!"));
json.set(F("vaue/ts/.sv"), F("timestamp"));
Serial.printf("Set json... %s\n", Firebase.set(fbdo, F("/test/json"), json) ?
"ok" : fbdo.errorReason().c_str());
}
NodeMCU code
else
{
json.add(String(count), "smart!");
Serial.printf("Update node... %s\n", Firebase.updateNode(fbdo,
F("/test/json/value/round"), json) ? "ok" : fbdo.errorReason().c_str());
}
Serial.println();
count++;
}
else {
Serial.println("Error in writing data to firebase"); } } }
Code Uploading on NodeMCU
Plug in NodeMCU Cable
• Tools > Boards > Generic ESP8266 Module
• Select Port
• Upload the code, make sure Arduino is not connected to any port
while uploading your code to nodeMCU
Final Look of the Real time Database

You might also like