DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Five Java Books Beginners and Professionals Should Read
  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices
  • Hiding Data in Cassandra
  • Extending Java APIs: Add Missing Features Without the Hassle

Trending

  • Five Java Books Beginners and Professionals Should Read
  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices
  • Hiding Data in Cassandra
  • Extending Java APIs: Add Missing Features Without the Hassle
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. IoT Push Notifications: Arduino, Firebase, and Android

IoT Push Notifications: Arduino, Firebase, and Android

This quick and easy tutorial will show you how to implement push notifications for IoT apps.

Francesco Azzola user avatar by
Francesco Azzola
CORE ·
Sep. 27, 16 · Tutorial
Like (9)
Save
Tweet
Share
34.00K Views

Join the DZone community and get the full member experience.

Join For Free

This tutorial describes how to implement IoT push notifications. Using IoT push notifications, we can send notifications to other devices. In this case, we want to send IoT push notifications from a smart object (like an Arduino MKR1000) to a mobile smartphone (i.e. Android smartphone). This project integrates different eco-systems (IoT and Android) and can have several implementations. For example, we can send a push notification to our smartphone when an alert occurs:

  • Gas leakage.
  • Motion detection.
  • Higher or lower temperature.
  • General failure of systems.

As an 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 a Firebase server using the Firebase API. The smart object sends an IoT push notification to an Android smartphone. We can use the same 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 the Firebase API in a different way, mixing different worlds!

In the last article, we described how to use the Firebase API to create a push notification, and in this article, we want to expand it and integrate 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 reading the previous post so that you have a clear idea how things work.

This project uses the app we developed in the previous post. You can download it if you want to test the project.

Arduino MKR1000 PIR Project

The first step is connecting Arduino MKR1000 to a PIR sensor. The picture below shows how to wire the sensor to MKR1000.

Image title


The schema is very simple. To know if the PIR sensor detected a movement, we have to check if the signal is 1. The sketch is very simple:

int inputPin = 2;
int pirState = LOW;

void setup() {
    pinMode(inputPin, INPUT);
    Serial.begin(9600);
}

void loop() {
    int val = digitalRead(inputPin);
    if (val == HIGH) {
        if (pirState == LOW) {
            Serial.println("Motion detected");
            pirState = HIGH;
            // Motion detected
        }
    }
  else {
    if (pirState == HIGH) {
        Serial.println("Motion ended!");
        pirState = LOW;
        // Motion ended
        }
    }
    delay(5000);
}

We have to read the signal on the input pin and check if it is 1. If it is 1, then we have detected a motion and we can send the notification.

MKR1000 Wi-Fi Connection

Before sending a notification, we have to connect Arduino MKR1000 to the Internet using Wi-Fi. We can slightly modify the code above and add the Wi-Fi connection:

#include <SPI.h>
#include <WiFi101.h>

int inputPin = 2;
int pirState = LOW;

char ssid[] = "Vodafone-xxxxx"; // your network SSID (name)
char pass[] = "yyyyyyyy"; // your network password
int status = WL_IDLE_STATUS;

WiFiClient client;
void setup() {
    pinMode(inputPin, INPUT);
    Serial.begin(9600);

    // attempt to connect to Wifi network:
    while ( status != WL_CONNECTED) {
        Serial.print("Attempting to connect to WPA SSID: ");
        Serial.println(ssid);
        // Connect to WPA/WPA2 network:
        status = WiFi.begin(ssid, pass);
        // wait 10 seconds for connection:
        delay(10000);
    }
    // you're connected now, so print out the data:
    Serial.println("You're connected to the network");
}

All done! Now everything is ready and we can focus our attention on the most interesting part: sending IoT push notification to a smartphone.

Sending IoT Push Notifications

To send a push notification, we use the Firebase API. We assume you have already created a project in the Firebase console, if not go back to the previous article and read how to configure a Firebase project.
To send a push notification using Firebase, it is necessary to:

  1. Authenticate our client.
  2. Create the message body in JSON.
  3. Send the message to a smartphone.

It is necessary to send some header HTTP parameters and a JSON message. The first thing is to set the Firebase host:

Host: fcm.googleapis.com

then, to authenticate our client we use a secret key, you get from Firebase console:

Authorization: key=AIzaSyC1mHfa_G89CDoNoq2zWhh1iL9---------

then we have to set the content type:

Content-Type: application/json

and finally we specify the HTTP method and the service we want to invoke:

POST /fcm/send HTTP/1.1

The body is in JSON format and is very simple:

{
    "to":"your_phone_key",
    "notification": {
        "body": "test message",
        "title" : "Title"
    }
}

That’s all!! We have to make this request using Arduino HTTP library:

void sendDataToFirebase() {
    String data = "{" ;
    data = data + "\"to\": \"your_smartphone_id\"," ;
    data = data + "\"notification\": {" ;
    data = data + "\"body\": \"Motion detected\"," ;
    data = data + "\"title\" : \"Alarm\" " ;
    data = data + "} }" ;

    Serial.println("Send data...");
    if (client.connect(fcmServer, 80)) {
        Serial.println("Connected to the server..");
        client.println("POST /fcm/send HTTP/1.1");
        client.println("Authorization: key=auth_key");
        client.println("Content-Type: application/json");
        client.println("Host: fcm.googleapis.com");
        client.print("Content-Length: ");
        client.println(data.length());
        client.print("\n");
        client.print(data);
    }
    Serial.println("Data sent...Reading response..");
    while (client.available()) {
        char c = client.read();
        Serial.print(c);
    }
    Serial.println("Finished!");
    client.flush();
    client.stop();
}

So it is very simple to send IoT push notifications, and we have integrated two different ecosystems. What will you build using push notifications? Comment below and let me know your experience. At the end of this post, hopefully, you gained a new knowledge about sending IoT push notifications.

push Firebase IoT arduino Android (robot)

Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Five Java Books Beginners and Professionals Should Read
  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices
  • Hiding Data in Cassandra
  • Extending Java APIs: Add Missing Features Without the Hassle

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: