2016年5月9日 星期一

如何使用Azure Mobile Service結合Arduino及DHT11做出網路温度 / 溼度計 ?

在目前Raspberry Pi , Arduino及其它大廠 (聯發科LinkIt One / 瑞昱Ameba.....)推出相容開發板容易

取得及價格低廉的情況下 ,  要自行架個單的IOT環境 , 已不是難事 , 本次小編使用Azure

Mobile Service結合Arduino Uno3  + DHT11來製作一個簡單的網路温度/溼度計 , 做法如以下: 

1.首先到Azure Manage Portal建立Mobile Service
 

2.填入Server Name及DB管理者帳號及密碼
 


3.新增一個Table來存放資料


4.在Table Name輸入名字 , 並且設定任何人只要知道Application Key就可以新增/修改/刪除/讀取   資料


5.在Mobile Service中 , 確定Table已建立好 , 並且可正常運作



6. 在Azure Mobile Service點選管理金鑰 , 並記下二組金鑰 , 待會程式會用到



7.確認在Azure Mobile Service下的資料可以看到一個資料表名稱為tabledht11 , Mobile Service準備工作到此結束


8.先準備好Arduino Uno3  , Ethernet Shield , 3條杜邦線及DHT11 (温度/溼度感測器) ,如下圖 :




9.將Arudino Uno 3及Ethernet Shield組合起來 , Ethernet Shield在上面 , 如下圖 :

10~11.接下來必須將 DHT11的三支腳接到Ethernet Shield , 接線電路圖如下 :  DHT11的Vcc接Ethernet Shield 5V , DHT11的GND接Ethernet Shield的GND , DHT11的Data接Ethernet Shield D4




12.開啟Arduino IDE , 將以下程式碼貼上 , 只需填入Server Name , Table Name及步驟6的key即可

#include <SPI.h>
#include <Ethernet.h>
#include <dht11.h>
// Ethernet shield MAC address (sticker in the back)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
/*You can find this values in your Mobile Service dashboard
   server = namemobileservice.azure-mobile.net
   table_name = nametable
   ams_key = your keyvalue, find in home page mobile service, with a click on button MANAGE KEYS*/
const char* server = "sensorfarm.azure-mobile.net";
const char* table_name = "tabsensor";
const char* ams_key = "you can find it on mobile service with manage keys button";
EthernetClient client;
char buffer[64];
dht11 DHT;
#define  DHT11_PIN 4
/*This method use Mobile service REST API fot to write value*/
void write_sensorvalue(double tempvalue, double humidityvalue)
{
   if (client.connect(server, 80)) {
      
     /*Table name in Mobile service*/
     sprintf(buffer, "POST /tables/%s HTTP/1.1", table_name);
     client.println(buffer);
    /*Sever name in Mobile Service*/
     sprintf(buffer, "Host: %s", server);
     client.println(buffer);
    /*Mobile Services application key*/
     sprintf(buffer, "X-ZUMO-APPLICATION: %s", ams_key);
     client.println(buffer);
    // JSON content type for the information
     client.println("Content-Type: application/json");
    /*Write values of temperature and humidity from DHT11 sensor*/
     sprintf(buffer, "{\"tempvalue\":\"%f\",\"humidityvalue\":\"%f\"}", tempvalue, humidityvalue);
    // Content length
     client.print("Content-Length: ");
     client.println(strlen(buffer));
    // End of headers
     client.println();
    // Request body
     client.println(buffer);
  }
    
   else
   {
     Serial.println("no connection available!");
   }
     client.stop();
}
/*-------------------------------------------------------*/
/*Setup method*/
/*-------------------------------------------------------*/
void setup()
{
   Serial.begin(9600);
  if (Ethernet.begin(mac) == 0)
   {
     Serial.println("ethernet shield failed");
     for (;;);
   }
    
   // Ethernet shield initialization:
   delay(1000);
}
/*-------------------------------------------------------*/
/*Loop method*/
/*-------------------------------------------------------*/
void loop()
{
   /*Read value from sensor input*/
   int chk = DHT.read(DHT11_PIN);
  /*Call method writ_sensorvalue and pass sensor parameter for to save on mobile service*/
   write_sensorvalue((DHT.temperature), (DHT.humidity));
  /*Time wait for new input data*/
   delay(10000);
}

13.在Arduino IDE執行程式結果如下 :



14. 即可從Azure Mobile Service中的Table中看到温度及溼度被寫入Table



















15.使用SQL Server Management Studio開啟資料庫中的Table , 檢視温度及溼度值是否寫入

 16.若要移轉到Azure App Service ,  執行以下步驟


17.輸入要移轉的Mobile Service名稱


18.移轉到Azure App Service , 可看到Easy Tables , 温度及溼度值會寫到這裡


19.Easy Tables看到的温度及溼度值 , 如下圖 :
資料來源 : http://social.technet.microsoft.com/wiki/contents/articles/31660.arduino-due-and-mobile-service-it-it.aspx

4 則留言:

  1. 作者已經移除這則留言。

    回覆刪除
  2. 您好,想請問新版的Azure中app service似乎於須再使用金鑰,也沒有金鑰的樣子,那在程式碼的部分,是直接將有金鑰的部分直接刪去即可?或者還要再做修改?謝謝

    回覆刪除
    回覆
    1. 漏掉你的留言,我在Arudino的Code改成以下例子,供你參考

      /*You can find this values in your Mobile Service dashboard
      server = namemobileservice.azure-mobile.net
      table_name = nametable
      ams_key = your keyvalue, find in home page mobile service, with a click on button MANAGE KEYS*/

      const char* server = "mvcdht2017.azure-mobile.net";
      const char* table_name = "tabledht11";
      const char* ams_key = "jOwRdsrmqEbChbadRkAldBNkuwtgwrws";

      刪除
    2. 很感謝您回覆,但是我在Mobile App Service裡建立簡易表後,權限是設為"允許匿名存取",在現行的服務上沒有金鑰可以用?

      刪除