Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Push Notifications from Arduino to Windows Phone 8

From Wiki
Jump to: navigation, search

This article explains how to push notifications from an Arduino board to an Windows Phone 8 application through the network.

WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code ExampleTested with
Devices(s): Lumia 820
Compatibility
Platform(s): Windows Phone
Windows Phone 8
Windows Phone 7.5
Article
Keywords: Arduino, Push Notification
Created: mfabiop (10 Jun 2013)
Last edited: hamishwillee (04 Jul 2013)

Contents

Introduction

This is another post about integration between Arduino and Windows Phone. This article will explains how to send push notifications from Arduino to the Windows Phone through the network. It makes possible to alert you anywhere when some sensor state is updated.

This post will show the following:

  • How to connect to the Internet using an Arduino board;
  • How to create push notification requests in Arduino;
  • How to receive these push notifications on Windows Phone 8 application.
  • It will be proved with a simple alarm application that alerts you when someone is detected by the infrared sensor.

Sample application (Simple alarm)

An arduino board that listen to the infrared motion sensor and send a push notification to the mobile client application when some motion is detected.

Architecture

A push notification service offers a channel to send data to a mobile client application in a power-efficiente manner, just as described in Push notifications for Windows Phone page. This feature enables the mobile client application to receive notifications from a server without do a request to the server.

The below image shows how this architecture works togheter with the Arduino board.

PushNotificationArduinoArch.png

Mobile application

  • (1) Request a channel URI from the Push Notification service
  • (2) Send the channel URI to the Arduino board

Arduino

  • Save the received channel URI
  • Listen to the motion detection sensor
  • (3) If a motion is detected, send a push notification to the Push Notification service using the channel URI

Mobile application

  • (4) Receives the push notification and notify the user

Hardware used

Schematic

The schematic is shown below. It's simple to put the parts together:

  1. Connect the Ethernet shield on your Arduino board
  2. Connect the infrared proximity sensor to the Ethernet shield
  3. Finally, connect the Ethernet shield to the wired network.

PushNotificationArduinoSchematic.png

Code

Below are the Arduino code and a link to download the Windows Phone 8 code.

Arduino

The Arduino code is shown below. It's able to push notifications to the Windows Phone 8 application through the ethernet shield.

#include <SPI.h>
#include <Ethernet.h>
 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Customize the below line to your IP address.
byte ip[] = { 192, 168, 0, 15 };
String pushNotificationServer;
String pushNotificationPath;
 
char* pushNotificationServerBuf;
char* pushNotificationPathBuf;
 
EthernetClient client;
EthernetServer server(80);
 
void setup()
{
Ethernet.begin(mac,ip);
server.begin();
pinMode(7, INPUT);
}
 
/*
Function used to push a notification to the mobile application.
*/

void pushNotification(char* notificationMessage) {
if(pushNotificationServer.length() == 0 || pushNotificationPath.length() == 0) {
return;
}
 
if (client.connect(pushNotificationServerBuf, 80)) {
client.print("POST ");
client.print(pushNotificationPathBuf);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(pushNotificationServerBuf);
client.print("Content-Length: ");
client.println(strlen(notificationMessage));
client.println("Content-Type: text/xml");
client.println("X-WindowsPhone-Target: toast");
client.println("X-NotificationClass: 2");
client.println();
client.println(notificationMessage);
client.println();
client.stop();
}
}
 
void loop()
{
receiveChannelUri();
int val = digitalRead(7);
if(val == HIGH) {
pushNotification("<?xml version='1.0' encoding='utf-8'?><wp:Notification xmlns:wp='WPNotification'><wp:Toast><wp:Text1>Intruder detected</wp:Text1><wp:Text2>Intruder detected</wp:Text2><wp:Param>/Page2.xaml?NavigatedFrom=ToastNotification</wp:Param></wp:Toast></wp:Notification>");
//Wait 30 seconds for the next notification
delay(30*1000);
}
 
}
 
/*
Function that receives a new channel URI from the mobile application.
*/

void receiveChannelUri() {
boolean isReceivingData = false;
String parameterName;
String parameterValue;
EthernetClient mobileClient = server.available();
if (mobileClient) {
while(mobileClient.available()) {
char c = mobileClient.read();
if(isReceivingData) {
//Is receiving data
if(parameterName.equals("channelUri=")) {
parameterValue += c;
} else {
parameterName += c;
}
} else {
if(c == '?') {
//The data is comming...
isReceivingData = true;
}
}
}
if(parameterName.equals("channelUri=")) {
int indexOfSlash = parameterValue.indexOf('/',10);
pushNotificationServer = parameterValue.substring(7,indexOfSlash);
pushNotificationPath = parameterValue.substring(indexOfSlash,parameterValue.length());
 
free(pushNotificationServerBuf);
free(pushNotificationPathBuf);
 
pushNotificationServerBuf = (char*)malloc((pushNotificationServer.length() + 1) * sizeof(char));
pushNotificationServer.toCharArray(pushNotificationServerBuf, pushNotificationServer.length() + 1);
 
pushNotificationPathBuf = (char*)malloc((pushNotificationPath.length() + 1) * sizeof(char));
pushNotificationPath.toCharArray(pushNotificationPathBuf, pushNotificationPath.length() + 1);
}
}
}


Windows Phone 8

The Windows Phone 8 code is the same as shown in this post, the only difference is the way that the mobile application sends the channel URI to the Arduino board. Only this piece of code is shown below. But you can download the entire source code here.

//Put the address of your arduino here.
private static string ARDUINO_ADDRESS = "http://192.168.0.15:80";
 
//Push channel
private HttpNotificationChannel pushChannel = null;
 
// Constructor
public MainPage()
{
InitializeComponent();
 
// The name of our push channel.
string channelName = "ToastSampleChannel";
 
// Try to find the push channel.
pushChannel = HttpNotificationChannel.Find(channelName);
 
// If the channel was not found, then create a new connection to the push service.
if (pushChannel == null)
{
pushChannel = new HttpNotificationChannel(channelName);
 
// Register for all the events before attempting to open the channel.
pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
 
// Register for this notification only if you need to receive the notifications while your application is running.
pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
 
pushChannel.Open();
 
// Bind this new channel for toast events.
pushChannel.BindToShellToast();
 
}
else
{
// The channel was already open, so just register for all the events.
pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
 
// Register for this notification only if you need to receive the notifications while your application is running.
pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
}
 
// Send the channel URI to the Arduino.
string postData = "?channelUri=" + pushChannel.ChannelUri.ToString();
 
WebClient wc = new WebClient();
wc.Headers["Content-Length"] = postData.Length.ToString();
wc.UploadStringAsync(new Uri(ARDUINO_ADDRESS), "POST", postData, wc);
}

Summary

This article is simpler than the other ones published about Arduino and Bluetooth. But it shows a more useful way to connect with an Arduino board, if you want to access the Arduino from everywhere through the Internet.

It is also possible to push notification to the new Nokia Asha devices using the new Nokia Notifications API. I didn't show this in the article just because the Nokia Notifications API doesn't work with the emulator and it couldn't be tested properly.

This page was last modified on 4 July 2013, at 02:02.
380 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×