Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Push Notification using Arduino and Windows Phone 8

0.00/5 (No votes)
20 Nov 2013 2  
This article shows you how to push Notification from Arduino to Windows Phone 8 application.

Introduction

Almost of us wants to use the Arduino features and Push Notification from Arduino Board to a Mobile Application. We can do that using Android and Windows Phone 8 application.So, here we will see together how to send push notification from Arduino to the Windows Phone Device Through the Network.

Background

We will make a simple alarm Application where an arduino board listen to the infrared motion sensor and send a push notification to the mobile client application when some motion is detected.So, The Mobile Application request a channel URI from the Push Notification service and send the channel URI to the Arduino board.Then, The Arduino save the received channel URI , listen to the motion detection sensor.If a motion is detected, send a push notification to the Push Notification service using the channel URI. Finally, the Mobile application is goinig to receive the push notification and notify the user.

Using the code

In order to push notifications to the Windows Phone 8 application through the ethernet shield.we must add the Arduino code shown below:

#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);
    }
  }  
}

There aren't a big difference between the Arduino Code and the Mobile Application code. So the only difference is the way that the mobile application sends the channel URI to the Arduino board as shown on the code below:

//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);
}  

Great, now we are done!

Any comments are welcome!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here