Header Ads

IOT with Firebase : Sensor Alert Push Notification to Android App Using FCM & PUSHER #PART4






I continue teaching IOT with Firebase through problem solving. In this video I'll show step by step guide to Setup Sensor Alert for Sending Push Notification to Android App by Using FCM (Firebase Cloud Messaging) & Pusher.In this tutorial we configure our android app and setup pusher notifications for sending manual Push Notifications in later tutorial we'll create a python script for sending automatic push notifications to android app if sensor detects any fire.

Video :


If you like videos like this consider donating $1, or simply turn off AdBlocker. Either helps me to continue making tutorials


Transcript / Cheat Sheet:

Arduino :


Circuit Diagram :




Above diagram connections:

Sensor A0 -> Node MCU Analog Pin
SENSOR alert LED -> Node MCU D5 Pin
Home Automation LED -> Node MCU D1 Pin

Arduino Code :


#include <FirebaseArduino.h>
#include <ESP8266WiFi.h>
// Set these to run example.
#define FIREBASE_HOST "hack-node.firebaseio.com"
#define FIREBASE_AUTH "qULRQbnuzJ3qk---------Xic1UQ1FxHvM"
#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;
}
}
Firstly we need to add FCM to our app for that Navigate to Tools -> Firebase, under Cloud Messaging click Setup Firebase Cloud Messaging. Now follow Step 1 & Step 2 which will help in Connecting your app with Firebase & add FCM dependency to your app.




Navigate to App/build.gradle, add dependency for pusher-notification API .

App/Build.gradle
apply plugin: 'com.android.application'
android
{
compileSdkVersion 26
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "com.example.root.ledcontrol"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.firebase:firebase-database:10.0.1'
compile 'com.pusher:push-notifications-android:0.9.12'
compile 'com.google.firebase:firebase-messaging:10.0.1'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
 Now sync your gradle file and Add Pusher Code to Main Activity.java which we get from pusher quick start guide

MainActivity.java



import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.pusher.pushnotifications.PushNotifications;
public class MainActivity extends AppCompatActivity {
Button on;
Button off;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PushNotifications.start(getApplicationContext(), "61d2753d-9e78-4bc5-86d4-61e44fedab27");
PushNotifications.subscribe("hello");
on = (Button) findViewById(R.id.on);
off = (Button) findViewById(R.id.off);
on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("LED_STATUS");
myRef.setValue(1);
}
});
off.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("LED_STATUS");
myRef.setValue(0);
}
});
}
}

Now build your gradle and Run app device & once compilation done you will get Continue option in pusher quick start guide after which we get code for sending push-notifications manually to our App.

Code Sample (run in terminal ) :



curl -H "Content-Type: application/json" \
-H "Authorization: Bearer 01CCFEA41DC49B7BE0782BC9BE6F064" \
-X POST "https://0f8e1764-5be5-4046-9fd9-80581ac4ea87.pushnotifications.pusher.com/publish_api/v1/instances/0f8e1764-5be5-4046-9fd9-80581ac4ea87/publishes" \
-d '{"interests":["hello"],"fcm":{"notification":{"title":"Hello","body":"Hello, world!"}}}'

4 comments:

  1. I have a doubt, you are using only curl or what else you use to apply "curl -H" Content-Type: application / json "\"

    ReplyDelete
    Replies
    1. we can create a python script which will automatically send push notification when some value changes in database
      Check out : https://www.hackanons.com/2018/04/iot-with-firebase-sensor-alert.html?m=1

      Delete
  2. While tablets should be conveyed in packs or in their unique cases, cell phones can without much of a stretch be put inside pockets. fallout 4 endurance bobbleheads locations

    ReplyDelete
  3. A portion of the prominent offering stock incorporates. periscope apk download

    ReplyDelete

Powered by Blogger.