[Arduino101] 開放資料平台之智慧家電控制

 

 

隨著最近的天氣溫度上升,對住家的環境也會有所影響,所以才想利用政府開放資料平台得到目前台北市氣溫溫度和透過arduino 101 接上 LM35線性溫度感測器得知目前的室內溫度,再與台北市目前溫度和室內溫度比較後,透過藍牙控制電風扇,若室內溫度高於台北市溫度則風扇開啟,反之則風扇關閉。

作者/攝影  許鈺莨
時間   約一小時
成本  NTD 2,000
難度   *  *  *
材料表
  • Arduino 101
  • 繼電器
  • DFROBOT Arduino 擴充板
  • DFROBOT 的 LM 35線性溫度
  • arduino IDE 1.8.1
  • Appinventor 2

 

首先,硬體設備先建置:

  1. 將DFROBOT擴充板接到Arduino101
  2. LM35線性溫度感測器接到A4腳位
  3. 將繼電器接到13號腳位

 

所有硬體接好後會是以下這張圖

再來就是程式撰寫的部分,分別為arduino IDE 和Appinventor 2,先以arduino IDE 環境說明為例:

 

第5行:LM35接DFROBOT 擴充板A4腳位

第6~11行: BLE UUID設定

第12行:設定繼電器腳位為13號腳位

第17~36行:BLE連線開啟

第40~83行:BLE傳輸LM35線性溫度感測器數值至手機畫面,並且Arduino101接收到字元O時則繼電器開啟,收到C字元則繼電器關閉。

完整程式:

#include <CurieBLE.h>

#include <LM35.h>

LM35 temp(A4);
BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)
BLEService RelayService("19B10010-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEUnsignedCharCharacteristic switchCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEUnsignedIntCharacteristic LM35Data( "19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
const int RelayPin = 13; // pin to use for the Relay
char control = '0';
int old_data ;

void setup() {
Serial.begin(9600);

// set Light pin to output mode
pinMode( RelayPin, OUTPUT);

// set advertised local name and service UUID:
blePeripheral.setLocalName("LM35");
blePeripheral.setAdvertisedServiceUuid( RelayService.uuid());

// add service and characteristic:
blePeripheral.addAttribute(RelayService);
blePeripheral.addAttribute(switchCharacteristic);
blePeripheral.addAttribute(LM35Data);
// set the initial value for the characeristic:
switchCharacteristic.setValue(0);

// begin advertising BLE Relay service:
blePeripheral.begin();

Serial.println("BLE Relay service.");
}

void loop() {
// listen for BLE peripherals to connect:
BLECentral central = blePeripheral.central();

// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());

// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the Light:
if (switchCharacteristic.written()) {

control = switchCharacteristic.value();
Serial.print("value : ");
Serial.println(control);

if (control == 'O') {
Serial.println("Relay on");
Serial.println(switchCharacteristic.value());
digitalWrite(13, HIGH); // Open relay
} else if(control == 'C'){

Serial.println(F("Relay off"));
Serial.println(switchCharacteristic.value());
digitalWrite(13, LOW); // Close Relay
}

}
int new_data = temp.cel();
if(old_data != new_data)
{
LM35Data.setValue(new_data);
Serial.println(new_data);
old_data = new_data;
delay(1000);
}
}

// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}

 

接下來是Appinventor2程式說明:

Arduino 101 BLE藍牙連線需要MAC序號才可連線成功,每片MAC都不相同,在Arduino101背後QR code下方即可找到,我使用Arduino101的 BLE 藍牙MAC為98:4F:EE:0F:42,每個Arduino101的藍牙MAC都不同。當程式執行時螢幕便會初始化,手機藍牙開始等待連線,當連上Arduino101時,螢幕標題會寫已連線,開始顯示室內溫度。

當手機與arduino101藍牙連線時,Arduino101會開始送出LM35線性溫度感測器的值。

手機利用OPEN DATA 開放資料平台每10秒鐘抓取台北市溫度的json網頁,資料來源:https://goo.gl/Vz49bi
json網頁: https://works.ioa.tw/weather/api/weathers/1.json
而台北市溫度是使用中央氣象局網站公告的氣象預報資訊作為資料來源,每 20 分鐘更新最新天氣概況。

而抓取json網頁的資料有個技巧就是先將所有的json網頁匯到Appinventor 的ListPicker元件裡,ListPicker元件可以自動將json網頁資料排列好,再利用陣列抓取我們所需資料,台北市溫度在第11行第2列中。

 

ListPicker 元件把json網頁排列好,如下圖所示:

比較台北市溫度和室內溫度,一開始抓取台北市溫度時會出現()為開始要抓取第一筆json資料之符號。而台北市溫度大於室內溫度,則風扇打開;反之, 台市溫度小於室內溫度,則風扇關閉。

 

手機端畫面如下圖所示:

 

結論:
本篇實現出藉由抓取open data 之json網頁得知目前台北氣溫數值和LM35線性溫度感測器測量出室內溫度並作比較,更進一步控制家電,未來可應用於溫室或打造智慧家電,有利於植物栽種,或調節房屋溫度,打造人類更舒適的環境。

 

 

相關文章:

發佈留言

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