In this tutorial of IOT with Firebase we’ll create python script for sending automated IOT with Firebase Sensor Alert Automated Push Notification if fire sensor detects any fire which is connected to Adruino NodeMCU ESP8266. We’ll finish up the tutorial with demo of our project in which we will see working of smart led and sensor alert notification, on which we are working from last 4 tutorials.
IOT with Firebase Sensor Alert Automated Push Notification
I hope you enjoy this tutorial. All of the code and a transcript of the video follows the video below.

Transcript / Cheat-sheet :

Installing Required Libraries

Installing Pyrebase

Pyrebase is a Python interface to Firebase’s REST API. In layman’s terms, it allows you to use Python to manipulate your Firebase database. The documentation for Pyrebase can be found at https://github.com/thisbejim/Pyrebase
We will install the Pyrebase and its dependencies using pip
sudo pip3 install Pyrebase
 

Installing Pusher Push Notifications

Push Notifications makes it easy to send push notifications to iOS and Android apps. It takes the hassle out of managing device tokens and interacting with Apple and Google’s messaging services. WithInterestsubscriptions to notify segmented devices in each request and delivery SLAs offered on premium plans, it’s built to scale. The documentation for Pusher Push Notification can be found
at https://docs.pusher.com/push-notifications/reference/server-sdk-python
 
The Pusher Notifications Python server SDK is available on PyPi here.
We can install this SDK by using pip:
sudo pip3 install pusher_push_notifications

Python Script Code : 

 


from pusher_push_notifications import PushNotifications
import pyrebase
config = {
apiKey: AIzaSyAhw2k–your key –mhPCpw5mScEksDc8,
authDomain: esri-.firebaseapp.com,
databaseURL: https://eea46.firebaseio.com,
projectId: esri-eea51,
storageBucket: esri—.appspot.com,
messagingSenderId: 4573—08934
}
firebase = pyrebase.initialize_app(config)
db=firebase.database()
pn_client = PushNotifications(
instance_id=61d2753d——86d4-61e44fedab27,
secret_key=1B3432CB——-B2FFC1CA9204EAB,
)
def stream_handler(message):
print(message)
if(message[data] is 1):
response = pn_client.publish(
interests=[hello],
publish_body={
apns: {
aps: {
alert: Hello!,
},
},
fcm: {
notification: {
title: Hello,
body: Hello, world!,
},
},
},
)
print(response[publishId])
my_stream = db.child(fire_sensor_status).stream(stream_handler,None)

 

Arduino Code :























































#include <ESP8266WiFi.h>

#include <FirebaseArduino.h>

// Set these to run example.
#define FIREBASE_HOST esri-eea51.firebaseio.com
#define FIREBASE_AUTH qULRQbnuzJ3qkAdYunMasgcLlKtpXic1UQ1FxHvM
#define WIFI_SSID kavin
#define WIFI_PASSWORD 1123581321
float fire;
void setup() {
Serial.begin(9600);
pinMode(D1, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(A0, INPUT);
// 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);
Firebase.set(LED_STATUS, 0);
}
int n = 0;
void loop() {
// get value
fire = analogRead(A0);
Serial.println(fire);
if(fire<250){
digitalWrite(D5,HIGH);
Firebase.setInt(fire_sensor_status,1);
}
n = Firebase.getInt(LED_STATUS);
// handle error
if (n==1) {
Serial.println(LED ON);
digitalWrite(D1,HIGH);
return;
delay(10);
}
else {
Serial.println(LED OFF);
digitalWrite(D1,LOW);
digitalWrite(D5,LOW);
Firebase.setInt(fire_sensor_status,0);
return;
}
}

For Android App code visit Previous Tutorial : IOT with Firebase Part 4

So, guys that’s end up with great tutorial on IOT with Firebase for sending automated Push Notification for Sensor Alert using Pusher, FCM to Android App. I hope you guy’s enjoyed it 🙂

Categorized in: