This tutorial describes how to implement IoT Push notifications using Arduino and Firebase., we can send a push notification to our smartphone when an alert occurs:
- gas leakage
- motion detection
- higher or lower temperature
- general failure systems
As an real example to show how to use IoT notifications, we will use a motion detection sensor. In other words, there is an Arduino MKR1000 connected to a sensor (PIR sensor). When the sensor triggers an event, then MKR1000 makes a call to Firebase server using Firebase API. Through the Firebase API, the smart object sends IoT push notifications to an Android smartphone. We can use the same way, to send notifications to other devices like iOS and so on.
The idea, that stands behind this project, is shown below:
As you can see, this project uses Firebase API in a different way mixing different worlds!
In the last article, we described how to use Firebase API to create a push notification, in this article we want to expand it and integrate Firebase with Arduino.
Moreover, we have already seen in the last post, how to create an Android app that handles push notification and how to configure it in the Firebase console. If you are new to Firebase, I suggest you read the previous post so that you have a clear idea how things work.
As Android app, this project uses the app we developed in the previous post. You can download it if you want to test the project.
Triggering events to send notification to Firebase in the corresponding PIN. The sketch is very simple:
Code language: JavaScript (javascript)Code language: JavaScript (javascript)); }
As you can notice, the sketch is very simple. We have to read the signal on the input pin and check if it is 1. If it is 1, then we have detected motion and we can send the notification.
MKR1000 WiFi connection
Before sending a notification, we have to connect Arduino MKR1000 to the internet using WiFi. We can slightly modify the code above and add the WiFi connection:
Code language: PHP (php)Code language: PHP (php)
#include <SPI.h> #include <WiFi101.h> int inputPin = 2; int pirState = LOW; char ssid[] = "zzzzz-xxxxx"; // your network SSID (name) char pass[] = "yyyyyyyy"; // your network password int status = WL_IDLE_STATUS; WiFiClient client; void setup() { Serial.begin(9600); pinMode(inputPin, INPUT); //: how to send IoT push notifications to a smartphone. There are other ways to send notifications to a smartphone. You can learn how to use PushingBox and PushBullet to send notifcation messages.
Learn More
Internet of Things with Android and Arduino: Control remote Led
How to integrate Android Things with Firebase to develop real-time IoT projects
How to use Ubidots to build IoT projects and integrate it with Android
How to sending IoT push notifications using Arduino and Firebase:
- Authenticate our client
- Create the message body in JSON
- Send the message to a smartphone
To do it, it is necessary to send some header HTTP parameters and a JSON message. The first thing is set the Firebase host:
Code language: CSS (css)Code language: CSS (css)
Host: fcm.googleapis.com
then, to authenticate our client we use a secret key, you get from Firebase console:
Code language: HTTP (http)Code language: HTTP (http)
Authorization: key=AIzaSyC1mHfa_G89CDoNoq2zWhh1iL9---------
then we have to set the content type:
Code language: HTTP (http)Code language: HTTP (http)
Content-Type: application/json
and finally we specify the HTTP method and the service we want to invoke:
Code language: HTTP (http)Code language: HTTP (http)
POST /fcm/send HTTP/1.1
The body of the IoT push notificiation is in JSON format and is very simple:
Code language: JSON / JSON with Comments (json)Code language: JSON / JSON with Comments (json)
{ "to":"your_phone_key", "notification": { "body": "test message", "title" : "Title" } }
That’s all!! We will make this request using Arduino HTTP library:
Code language: PHP (php)Code language: PHP (php)(); }
Wrapping up….
Hi,
I am using Arduino/ESP8266 to receive notification from android device. Can you tell me how I can generate device token on Arduino/ESP8266 board? If not, is there any other way I can receive notification on Arduino/ESP8266 from FCM/Android device?
Thanks in advance.
Regards.
Vicky.
I think it is not so easy to get a token for Arduino because you should run a full webserver that supports javascript on Arduino. In this case, you could add it to Firebase console.
You could give a look at aRest library that helps you to call sketch function from a remote application. If you want to have more information give a look at my post
Instead of aRest, you could give a try to Firmata. What kind of project are you building?
Hi!
I am using an Ultrasonic Sensor, Arduino UNO and ESP8266 to send notification to an android phone when the sensor detects and object. Could you tell me the steps in which I should take in order to receive the notifications from my android phone whenever the sensor detects an object? I am also using Firebase but I do not know how to link it with Arduino UNO.
Thanks in advanced!
Friend you congratulations. I’m developing this same, but I can´t integrate the arduino code for send the notification “push” to my android, you can share the source code.
Sir, I want to do the same(PIR sensor, push notification) but using raspberry pi 3. can u please share the source code.
Very well. It’s so good but I will be so thankful if you tell me or make an article about how to on and off the lights using firebase notification, raspberry pi 3, and Android thing app as well as Android client app.
Thank you, it is a good idea. I will plan a post covering this topic
If you mean passing the ssid and password from your device…..i do not know….i guess it is not possible.
You should find a way to connect to internet
Yes i guess it should work even if i did not try it…anyway they should be compatible
Hi, Where is ‘your_smartphone_id’ ? because in my Firebase setting don’t exist. I don’t know where search this data.
Thanks you
You get the smartphone_key when you register your smartphone in Firebase at the beginning in the app.
Give a look at you will get there how to know the smartphone_id.
Let me know if you need more information
Dear Francesco, the only thing I see in Firebase is the ‘Sender ID’ and the API number, but not ‘smartphone_key’ and I don’t see how to configure it.
In addition, it is not clear to follow the tutorial in a way that I do not understand because, for example:
The body is in JSON format and is very simple:
{
“to”: “your_phone_key”,
“notification”: {
“body”: “test message”,
“title”: “Title”
}
}
Where does this apply? The arduino code to understand it is rare, and I have done many projects with arduino, esp32 and nodemcu, but I still don’t understand how to send push notifications via arduino, firebase so that android / ios can take it.