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 };
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);
}
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>");
delay(30*1000);
}
}
void receiveChannelUri() {
boolean isReceivingData = false;
String parameterName;
String parameterValue;
EthernetClient mobileClient = server.available();
if (mobileClient) {
while(mobileClient.available()) {
char c = mobileClient.read();
if(isReceivingData) {
if(parameterName.equals("channelUri=")) {
parameterValue += c;
} else {
parameterName += c;
}
} else {
if(c == '?') {
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:
private static string ARDUINO_ADDRESS = "http://192.168.0.15:80";
private HttpNotificationChannel pushChannel = null;
public MainPage()
{
InitializeComponent();
string channelName = "ToastSampleChannel";
pushChannel = HttpNotificationChannel.Find(channelName);
if (pushChannel == null)
{
pushChannel = new HttpNotificationChannel(channelName);
pushChannel.ChannelUriUpdated +=
new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred +=
new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
pushChannel.ShellToastNotificationReceived +=
new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
pushChannel.Open();
pushChannel.BindToShellToast();
}
else
{
pushChannel.ChannelUriUpdated +=
new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred +=
new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
pushChannel.ShellToastNotificationReceived +=
new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
}
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!