Click here to Skip to main content
Click here to Skip to main content
Go to top

Push Notification using Arduino and Windows Phone 8

, 20 Nov 2013
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Hadrich Mohamed
Student GlobalNet
Tunisia Tunisia
Software Engineering student in the International Institute of technology of Sfax (IIT),Microsoft Student Partner , IEEE IIT SB member, IIT Microsoft Club President And a condidate at Appli Academy 2013-2014.
I like creating Windows store, Android and Windows Phone 8 applications.
Mail : hadrichmed@gmail.com
Phone : +216 27 61 61 40
Follow on   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 20 Nov 2013
Article Copyright 2013 by Hadrich Mohamed
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid