IoT project: Arduino sends push notification to Android using Temboo, Parse.com

This Internet of things project describes how Arduino sends a push notification to Android smartphone. As the example, we will build an alarm system based on Arduino and Android, this is an interesting example of Internet of things (IoT) and the aim of this project is building an alarm using an infrared sensor (PIR) connected to Arduino board that sends push messages to Android smartphone. This project mixes different technologies and platforms and we make them working together!

The alarm system uses two platforms that help to simplify the project:

  • Temboo 
  • Parse.com

 

Moreover, Google Android push notification is a service used by developers to send data to Android apps as soon as the data is available: in this way Android app doesn’t have to make requests to the server to know if new information is available.
Using Android push service, apps can save smartphone battery and reduce the network traffic: the user experience is also improved.
There are several different methods that can be used to implement Android push messages, the standard way is using GCM (Google Cloud Messaging) but there are some alternatives very interesting like Parse.com, that is easier to use.

Send push notification to android phone with parse.com

 

Android push notification example: Set up the project with Parse.com

The first thing is creating a Parse account and configure a new app. This is very easy. Once you have all things done, it is time to create a new project in Android Studio and modify build.grade to include the parse library:

[xml]dependencies {

compile ‘com.parse.bolts:bolts-android:1.2.1’
compile ‘com.parse:parse-android:1.10.1’
}
[/xml]

Now you can follow, the tutorial provided by Parse.com. In our case, we have created a ParseTutorialApplication that extends Application and it is used to configure the Parse connection:

[java]public class ParseTutorialApplication extends Application {

@Override
public void onCreate() {
super.onCreate();
System.out.println("Application");
Parse.initialize(this, "your key", "your key");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
[/java]

By now you use the default receiver provided by the library, in the next paragraph, you will see how to customize it.
If the project is configured correctly, you can try to send an android push notification using the web interface:

 

android push text message

In your Android emulator, you should get the push notification:

android push notification with text message

 

Please be sure that the emulator includes Google API.

Custom receiver example: Receive JSON data

Now it is time to customize the receiver so that we can support custom as android push notification message and not only text messages. Customising the receiver, it is possible to implement custom app logic like parsing the incoming message and show custom messages and so on. Looking back in the Manifest.xml, as broadcast receiver it was used the standard receiver com.parse.ParsePushBroadcastReceiver, now to customize its behaviour we can subclass it:

[java]public class JSONCustomReceiver extends ParsePushBroadcastReceiver {
@Override
protected void onPushReceive(Context context, Intent intent) {
…..
}
}
[/java]

and override the onPushReceiver so that it is possible to implement the app logic when the message is available.
Let us suppose that the message is in JSON format like that:

[xml]{"message":"hello phone"}[/xml]

In the onPushReceiver, the app parses the message:

[java]private String getData(String jsonData) {
// Parse JSON Data
try {
System.out.println("JSON Data ["+jsonData+"]");
JSONObject obj = new JSONObject(jsonData);
return obj.getString("message");
}
catch(JSONException jse) {
jse.printStackTrace();
}
return "";
}
[/java]

Once the message content is available and extracted from JSON message, the app notifies it to the user using NotificationCompat and NotificationManager.

[java]// Create custom notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_not_alert)
.setContentText(data)
.setContentTitle("Notification from Parse")
.setContentIntent(pendingIntent);

Notification notification = builder.build();
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
[/java]

where pendingIntent is the instance of the Intent that starts the Activity when a user touches the push notification:

[java]// Add custom intent
Intent cIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
cIntent, PendingIntent.FLAG_UPDATE_CURRENT);
[/java]

and finally the app notifies the message:

[java]nm.notify(1410, notification);[/java]

The final result is shown in the picture below:

Android push notification son message with custom receiver

Notice we have customized the notification icon too.

Temboo and parse to send push notification

 

Arduino sends push notification: IoT project overview

Before diving into the IoT project where Arduino sends push notification, it is useful to describe these two platforms.
Temboo is a platform that has a set of “connectors” that can be used to exchange data with other platforms or service providers (i.e eBay, Yahoo! Weather, Google and so on). The interesting part of Temboo is that it is compatible with Arduino board, so that these connectors can be exported on Arduino.
Parse.com is the platform we used in the last post to send android push messages.

The main overview of the internet of things project is shown below:

Arduino sends push notification

As it is clear, there are several parts that build the IoT project. The first part is Arduino board with PIR sensor that is used to detect movements. Arduino runs a Temboo agent that sends data to Parse platform. This agent is triggered when one of the Arduino digital input gets HIGH. The Temboo platforms is used to create the agent without writing too much code lines.
Using Temboo choreo, Arduino can send JSON data directly to Parse.com that in turns send a push message to our smart phone.

Arduino Sketch

The first step is setting up the Arduino sketch that uses PIR sensor and testing it. This step is very simple, we have to connect the sensor to Arduino board using three wires: Power, Ground and Signal.
The sensor is very simple, the output gets high when it detects movements. For this example, we can suppose it is connected to digital pin 8.
To check that our sensor works correctly and it is connected in the right way to Arduino board so that it detects movements,  try to load this code into arduino:
[cpp] int stato = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(8, INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
stato = digitalRead(8);
Serial.println(stato);
delay(500);
}
[/cpp]

Now runs the code and move your hand in front of the sensor and give a look at the serial monitor to check if it works!
Now the Arduino component is ready!

Temboo choreo

The next step is setting up the agent that connects Arduino board to Parse.com. In this case, we need an Ethernet shield to connect Arduino to internet. I’ve used Wiznet W5500. Once you have created your account, it is time to configure your Temboo chores. We want to connect Arduino to Parse so we check Parse -> Push Notification. Parse choreos requires some information before using it:

  • Application ID
  • RestAPI Key
These two parameters are used to connect the agent to Parse.com. You can find these info in Parse.com:
Parse.com connection api parameters

You have to copy the required key into Temboo:

Temboo choreos Parse.com config

We are ready. If you want you can try to send a notification from Temboo to Parse.com.
Now set the trigger that controls the agent:

Temboo agent trigger

At the end, Temboo will create the Arduino code ready to use!! Finally, copy and paste the code into your Arduino IDE.
The code generated by Temboo is shown below:

[cpp]#include <spi.h>
#include <dhcp.h>
#include <dns.h>
#include <ethernet.h>
#include <ethernetclient.h>
#include <temboo.h>
#include "TembooAccount.h" // Contains Temboo account information

byte ethernetMACAddress[] = ETHERNET_SHIELD_MAC;
EthernetClient client;

// The number of times to trigger the action if the condition is met
// We limit this so you won’t use all of your Temboo calls while testing
int maxCalls = 10;

// The number of times this Choreo has been run so far in this sketch
int calls = 0;

int inputPin = 8;

IPAddress ip(192, 168, 1, 130); // Arduino IP Add

void setup() {
Serial.begin(9600);
// For debugging, wait until the serial console is connected
delay(4000);
while(!Serial);
Ethernet.begin(ethernetMACAddress, ip) ;
Serial.println("OK");
delay(5000);

// Initialize pins
pinMode(inputPin, INPUT);
Serial.println("Setup complete.n");
}

void loop() {
int sensorValue = digitalRead(inputPin);
Serial.println("Sensor: " + String(sensorValue));

if (sensorValue == HIGH) {
if (calls < maxCalls) {
Serial.println("nTriggered! Calling SendNotification Choreo…");
runSendNotification(sensorValue);
calls++;
} else {
Serial.println("nTriggered! Skipping to save Temboo calls. Adjust maxCalls as required.");
}
}
delay(250);
}

void runSendNotification(int sensorValue) {
TembooChoreo SendNotificationChoreo(client);
// Set Temboo account credentials
SendNotificationChoreo.setAccountName(TEMBOO_ACCOUNT);
SendNotificationChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
SendNotificationChoreo.setAppKey(TEMBOO_APP_KEY);

// Set profile to use for execution
SendNotificationChoreo.setProfile("ParseAccount");
// Set Choreo inputs
String NotificationValue = "{"channel": "temboo", "type": "android", "data": {"message": "This is a test alert!"}}";
SendNotificationChoreo.addInput("Notification", NotificationValue);

// Identify the Choreo to run
SendNotificationChoreo.setChoreo("/Library/Parse/PushNotifications/SendNotification");

// Run the Choreo
unsigned int returnCode = SendNotificationChoreo.run();

// Read and print the error message
while (SendNotificationChoreo.available()) {
char c = SendNotificationChoreo.read();
Serial.print(c);
}
Serial.println();
SendNotificationChoreo.close();
}
[/cpp]

Configure Parse.com channel and build Android app

Temboo requires we use a Parse channel to send our notifications. We have then to modify our Android app to use a channel to listen to incoming notification. If you don’t know how to write an android app that handles push messages you can read my previous post describing how to send android push messages with Parse.com.
Modify slightly the ParseTutorialApplication.java in this way:
[java] @Override
public void onCreate() {
super.onCreate();
System.out.println("Application");
Parse.initialize(this, "sfFzOxotpsdOHWJE3nMmD0erLgFoecttKvC9CzIc", "nwbMEy7l4STpyNrABdrQxpEjdKIynSbuec56QbEz");
ParseInstallation.getCurrentInstallation().saveInBackground();

ParsePush.subscribeInBackground("temboo");
}
[/java]

where temboo is the channel.
We are ready!! Runs the app and move your hand near the sensor, Arduino will send a push message to the Android smartphone!!
The final result is shown here:

Android push message sent by Arduino with Temboo and Parse.com

At the end of this post, you know how to build an IoT project where Arduino sends push notification to Android and how to integrate these two ecosystems through Temboo and Parse.com.

  • Add Your Comment