Press ESC to close

148 12

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;
}
}
Android App Code : 
    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!”}}}
        that’s it for this tutorial in next Tutorial i will show you how to create Python script for sending automated push notification to android app if sensor detects any Fire Alert.


          12 Comments

          1. Unknown on April 16, 2018

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

          2. kavin sharma on April 17, 2018

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

          3. Burgess on May 7, 2018

            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

          4. judden jout on May 23, 2018

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

          5. BekarShongstha.com on December 8, 2018

            Bro can u please send me the app making code.specially the activity_main.xml

          6. kavin sharma on December 8, 2018

            i will suggest you to check previous part of this post which includes all source code

          7. Steven on February 1, 2019

            For additional wellbeing, you can even do your squats on a smith machine. So on the off chance that you are a learner, I propose crouching on a smith machine until you become acclimated to doing them legitimately. interval running

          8. Unknown on May 11, 2019

            Hello! I need help. I did everything as written by you and watched the video. BUT I do not receive notifications on the application. I add changes to FIreBase because there is no arduino. In the end, nothing comes

          9. Anonymous on May 18, 2019

            Sir, i want to know about the sensor used in this project

          10. tike mik on January 27, 2020

            Still it is the story of the human and the divine, not intended to be opposing forces, but certainly if not oddly playing out in the landscape of free will and providence…and eventually, the "Man of La Manga." was born.

            kiss manga

          11. Smith on March 21, 2020

            Transforming a relaxation time movement https://techcrue.com/ or enthusiasm into full time calling could be a fantasy occupation and Android Game Development could be named one.

          12. Unknown on May 2, 2020

            why didn't use firebase push notification???

          Leave a Reply