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>
|
|
// 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!”}}}‘ |
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.
I have a doubt, you are using only curl or what else you use to apply "curl -H" Content-Type: application / json ""
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
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
A portion of the prominent offering stock incorporates. periscope apk download
Bro can u please send me the app making code.specially the activity_main.xml
i will suggest you to check previous part of this post which includes all source code
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
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
Sir, i want to know about the sensor used in this project
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
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.
why didn't use firebase push notification???