LinkIt ONE 結合 IBM BlueMix 雲服務 – 即時檢測 DHT11 溫濕度感測器資料

本文是參考 IBM 開發者論壇上 IoTGeek 網友的教學,使用聯發科 LinkIt ONE 開發板經由 Wi-Fi 連上 IBM Bluemix 雲服務。

前置作業:

  1. 註冊 IBM Bluemix ID (可免費試用 30 天)
  2. 作業系統:Microsoft Windows XP/Vista/7/8 或 Mac OS X 10.9 以上。請確認您的電腦已經安裝上述版本的 Arduino IDE 與 LinkIt ONE SDK:教學請點我
  3. Arduino IDE:版本需為 1.5.6-r2 BETA 與 1.5.7 BETA,這是為了配合 LinkIt ONE SDK 的緣故。
  4. 請確認您知道如何將 LinkIt ONE 連上 WiFi:教學請點我

下載 LinkItONE-IBMBluemix 範例程式並修改對應參數

  1. 請由此下載範例草稿碼:https://github.com/iotgeek/LinkItONE-IBMBluemix
  2. 請根據本網頁說明來完成 https://github.com/iotgeek/LinkItONE-IBMBluemix/blob/master/README.md
  3. 如果您有 Grove 擴充板的話,請將其裝上 LinkIt ONE,接著在 D2 腳位上裝一個溫度感測器。

4-15a

  1. 在 Arduino IDE 中開啟 quickstart.ino (在剛剛下載的 .zip 檔中),請將其中的 WiFi 相關資訊(帳號、密碼與 WPA/WEP 加密方式等)修改為您的環境參數。
  2. 在草稿碼中修改 servername,格式:”666666.messaging.internetofthings.ibmcloud.com“, “666666” 是您的組織 (organization) 名稱
  3. 在草稿碼中修改 clientName,格式:”LinkItONE: aabbccddeeff “,”aabbccddeeff ” 是 MAC Address,以 LinkIt ONE 來說就是 2CXXXXXXXXXX (寫在 LinkIt ONE 背面,如下圖);”666666″ 是您的組織 (organization) 名稱 ,裝置種類則是 LinkItONE。

1-07

#define WIFI_AP “your_ap_ssid”                  

// WiFi AP 名稱

#define WIFI_PASSWORD “your_password”      
// 輸入 WiFi AP 密碼

#define WIFI_AUTH LWIFI_WPA
// choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP, depending on security type of your WiFi access point.

char macstr[] = “aabbccddeeff”;
// Enter the device mac address here. You can see the WiFi MAC address printed on the backside                                                                                of the LinkIt ONE board.

上傳並執行

編譯並上傳這份草稿碼到 LinkIt ONE,上傳好之後板子就會連上 WiFi AP (一切順利的話),接著板子就會把溫濕度資料以 MQTT 訊息格式送到 IBM Bluemix。

請用網頁瀏覽器開啟:https://quickstart.internetofthings.ibmcloud.com/#/ ,請在畫面左側輸入您剛剛在 quickstart.ino 中所修改的同一組 MAC Address,再按下 “Go” 就可以看到來自 LinkIt ONE 的即時溫溼度資料:

ibmInternetofthingsfoundation_quickstart

執行畫面如下圖。您也可以從同一個帳號下管理所有連到您的 IBM Bluemix 下的各種裝置。

QuickStart_graph2

 

quickstart.ino

quickstart.ino
/*

MQTT IOT Example - LinkIt One board to IBM Bluemix Connection quick start
- continuously obtains values from the Virtuabotix DHT11 temperature/humidity sensor
- formats the results as a JSON string for the IBM IOT example
- connects to an MQTT server (either local or at the IBM IOT Cloud)
- and publishes the JSON String to the topic named quickstart:MY_MAC_ADDRESS
*/



#include <SPI.h>


#include <LWiFi.h>
#include <LWiFiClient.h>
#include "DHT.h"

/* PubSubClient has been taken from https://github.com/knolleary/pubsubclient/tree/master/PubSubClient */
#include <PubSubClient.h>

#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)

/* Modification Needed :
Enter your WIFI Access Credentials.
*/
#define WIFI_AP "your_ap_ssid"
#define WIFI_PASSWORD "your_password"
#define WIFI_AUTH LWIFI_WEP // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.



char macstr[] = "aabbccddeeff"; // Enter the device mac address here



// Note this next value is only used if you intend to test against a local MQTT server
byte localserver[] = {192,168,1,196};

char servername[] = "quickstart.messaging.internetofthings.ibmcloud.com";
String clientName = String("d:quickstart:arduino:") + macstr;
String topicName = String("iot-2/evt/status/fmt/json");

float tempF = 0.0;
float tempC = 0.0;
float humidity = 0.0;

DHT dht(DHTPIN, DHTTYPE);
LWiFiClient wifiClient;
PubSubClient client(servername, 1883, 0, wifiClient);

// Uncomment this next line and comment out the line after it to test against a local MQTT server
//PubSubClient client(localserver, 1883, 0, ethClient);


void InitLWiFi()
{
LWiFi.begin();
// Keep retrying until connected to AP
Serial.println("Connecting to AP");
while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
{
delay(1000);
}


Serial.println("Connected to AP");
}

void setup()
{
Serial.begin(9600);
dht.begin();
InitLWiFi();
}

void loop()
{
IPAddress server(localserver);

char deviceName[] = "Linkitone";
char clientStr[34];
clientName.toCharArray(clientStr,34);
char topicStr[26];
topicName.toCharArray(topicStr,26);


getData();




if (!client.connected()) {
Serial.print("Trying to connect to: ");
Serial.println(clientStr);

Serial.println("Connecting to server ");
while (! client.connect(clientStr))
{
Serial.println("Re-Connecting to SERVER");
delay(3000);
}
Serial.println("Connected to server");

}

if (client.connected() ) {

Serial.println("Connected to server : Building jason");

String json = buildJson();
char jsonStr[200];
json.toCharArray(jsonStr,200);
Serial.println("Connected to server : publishing");
boolean pubresult = client.publish(topicStr,jsonStr);
Serial.print("attempt to send ");
Serial.println(jsonStr);
Serial.print("to ");
Serial.println(topicStr);
if (pubresult)
Serial.println("successfully sent");
else
Serial.println("unsuccessfully sent");
}
delay(5000);
}

String buildJson() {
String data = "{";
data+="\n";
data+= "\"d\": {";
data+="\n";
data+="\"myName\": \"LinkIt ONE DHT11\",";
data+="\n";
data+="\"temperature (F)\": ";
data+=(int)tempF;
data+= ",";
data+="\n";
data+="\"temperature (C)\": ";
data+=(int)tempC;
data+= ",";
data+="\n";
data+="\"humidity\": ";
data+=(int)humidity;
data+="\n";
data+="}";
data+="\n";
data+="}";
return data;
}

void getData() {

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
if(dht.readHT(&tempC, &humidity))
{
Serial.println("------------------------------");
Serial.print("temperature = ");
Serial.println(tempC);
tempF = (tempC * 2 ) + 30;

Serial.print("humidity = ");
Serial.println(humidity);
}

delay(2000);

}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *