Windows Phone 7 瓦斯外洩通知

  • 11139
  • 0
  • 2010-12-06

Windows Phone 7 瓦斯外洩通知

相信每個人都有煮飯或是燒開水的經驗,可是突然去做別的事但忘了廚房還在煮東西,還有冬天到了不少人都會泡個熱水澡這些事件都會有可能發生瓦斯外洩的意外,新聞常常都播放好像很平凡,但我們可以怎麼來防止呢?今天我要跟大家分享一下使用簡單的瓦斯感測器來發報通知,所採用的是MQ-4可燃性氣體感測器一般都用在天燃氣甲烷洩漏檢測,檢測範圍為20ppm~10000ppm,反應速度快小於10秒,實際硬體描述請連結下載 規格

 

image

使用Windows Phone 7 Push Notificiation整個機制架構如下圖所示:

image

硬體建置作業如下圖所示,分別把紅色線接上板子上5v電壓插孔、黑線插在接地的GND孔、黃色線則插在Analog in 第0孔只要接上這三條線就可以來實作了。Arduino內建一組專門讀取analog訊號的腳位六組,使用功能大致和digital read相同。可以讓我們使用光敏電阻、溫感電組、感測器一隻腳位接在Arduino的5v孔,另一隻腳位接在pin角端,讀取電流通過的大小值所以當瓦斯通個感測器時所讀回來的值會越大。 使用以下AnalogInput的範例( http://www.arduino.cc/en/Tutorial/AnalogInput

image

接著我們來寫軟體吧首先來看Arduino的程式碼源吧

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,16,88 };
byte Service[] = { 94,245,116,126 };
byte gateway[] = { 192, 168, 16, 120 };
String readString = String(50);
String readMsg=String(100);
String URI=String(100);
Client client1(Service, 80);
Server server(80);
int sensorValue;
void setup()
{
    Ethernet.begin(mac, ip, gateway);
    server.begin();
}

//取得Http URI 參數

String find_key_val(String str,String key)
{
  int idx=str.indexOf(key)+key.length();
  int len=str.length()-11;
  return(str.substring(idx,len));
}

//傳送訊息給MPNS伺服器

void SendMsg()
{
  if (client1.connect())
  {
        String path=URI.substring(26,URI.length());
        client1.println("POST "+ path +" HTTP/1.1");
        client1.println("Host: db3.notify.live.net");
        client1.println("Content-Type:text/xml;charset=utf-8");
        client1.println("X-MessageID: 030ed569-d001-4681-9dd7-877e4826a3fc");
        client1.println("X-NotificationClass: 2");
        client1.println("X-WindowsPhone-Target: toast");
        client1.println("Content-Length: 169");
        client1.println();
        client1.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        client1.println("<wp:Notification xmlns:wp=\"WPNotification\">");
        client1.println("<wp:Toast><wp:Text1>瓦斯外洩</wp:Text1>");
        client1.println("<wp:Text2>sensorValue</wp:Text2></wp:Toast>");
        client1.println("</wp:Notification>");
        client1.println();
   }
}
void loop()
{
  Client client = server.available();

//判斷手機是否有傳送URI如果有傳送相對也回應所接收的URI給手機
  if (client){
    while (client.connected()){
   if (client.available()){
          char c = client.read();
    readString.concat(c);
   if (c == '\n'){
           URI=find_key_val( readString,"URI=");
         client.println("HTTP/1.1 200 OK");
       client.println("Content-Type: text/html");
      client.println();
           client.println(URI);
      readString="";
        client.stop();
        }
      }
    }
}

//判斷瓦斯回傳訊息是否大於800如果是的話就發送訊息
    sensorValue = analogRead(0);
if (sensorValue > 800){SendMsg();}

//結束MPNS伺服器溝通
if (client1)
{
  while (client1.connected()) { if (client1.available()){ client1.stop(); }}
}
}

 

在來我們寫手機的程式碼,開啟vs2010新增一個WindowsPhone7應用程式把以下程式碼貼上就行不用建立任何ui

HttpNotificationChannel c;

public MainPage()
        {
            InitializeComponent();

           //尋找MPNS所建立頻道URI
            c = HttpNotificationChannel.Find("kylin");
            if (c != null)
            {
                c.Close();
                c.Dispose();
            }
            c = new HttpNotificationChannel("kylin");
            c.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(c_ChannelUriUpdated);
            c.Open();
            c.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(c_ShellToastNotificationReceived);
            c.BindToShellToast();
        }

//取得MPNS所傳出Toast訊息

        void c_ShellToastNotificationReceived(object sender, NotificationEventArgs e)
        {
            Debug.WriteLine(e.Collection["wp:Text1"] + e.Collection["wp:Text2"]);
        }

        //把MPNS所取得的URI傳給Arduino,若Arduino也回傳跟MPNS一樣的URI就顯示ok

       void c_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
        {
            string ChannelUri=e.ChannelUri.ToString();
            try
            {
                System.Uri uri = new Uri("http://192.168.16.88/?URI=" + e.ChannelUri.ToString());
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
                request.Method = "GET";
                request.BeginGetResponse(
                    result =>
                    {
                        HttpWebResponse response = (HttpWebResponse)((HttpWebRequest)result.AsyncState).EndGetResponse(result);
                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            string responseString = reader.ReadToEnd();
                            Dispatcher.BeginInvoke(() =>
                            {
                                if (ChannelUri.Equals(responseString.Replace("\r\n","") ))
                                {
                                    MessageBox.Show("OK");
                                }

                            });
                        }
                    }, request);
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

接下來我們就來測一下我們系統首先將wp7模擬器打開送入程式,接著把瓦斯感測器上用打火機不用點火只要按著瓦斯鈕讓瓦斯噴出就好了,這時等到wp7的訊息出來吧就成功了哈哈。

image

以上的範例其實很簡單如果沒碰過硬體的人會稍會覺得有些困難,但是如果用心一下會發現不是很難還可把這個範例改成如果我們的抽屜、門、窗被打開了以及有人按門鈴也是可以回報。其實很多應用加上目前的最新手機都是很不錯居家應用。