簡(jiǎn)介
本文檔介紹了基于樹莓派+百度天工(物接入、TSDB、物可視)快速搭建一個(gè)環(huán)境溫濕度監(jiān)測(cè)告警系統(tǒng)只祠。幫助用戶熟悉天工物接入的接入方式、物影子概念以及使用方式等內(nèi)容扰肌,快速搭建自己物聯(lián)網(wǎng)硬件和應(yīng)用抛寝。
閱讀本示例前建議先熟悉快速入門文檔。
場(chǎng)景描述
該場(chǎng)景演示物聯(lián)網(wǎng)應(yīng)用場(chǎng)景中常用的功能曙旭,包括設(shè)備接入盗舰、數(shù)據(jù)上報(bào)、數(shù)據(jù)存儲(chǔ)桂躏、數(shù)據(jù)展示钻趋、遠(yuǎn)程控制、配置下發(fā)等剂习。
硬件設(shè)計(jì)
首先我們?cè)O(shè)計(jì)了一款智能溫濕度監(jiān)測(cè)儀蛮位,監(jiān)測(cè)儀運(yùn)行時(shí)會(huì)實(shí)時(shí)監(jiān)測(cè)當(dāng)前所處環(huán)境的溫濕度,同時(shí)可以設(shè)置溫濕度的告警值鳞绕,當(dāng)溫濕度達(dá)到設(shè)定值時(shí)失仁,相應(yīng)的告警燈亮起——這是設(shè)備硬件部分具備的功能,還未涉及到物聯(lián)網(wǎng)設(shè)備接入
為了能夠?qū)崟r(shí)查看到環(huán)境溫信息们何,進(jìn)而需要將設(shè)備接入物聯(lián)網(wǎng)平臺(tái)陶因,如果當(dāng)前環(huán)境溫濕度達(dá)到設(shè)置的閾值,觸發(fā)設(shè)備告警燈亮起垂蜗,并上報(bào)設(shè)備當(dāng)前狀態(tài)和溫濕度信息楷扬,正常情況下則每30秒上報(bào)一次當(dāng)前溫濕度信息到平臺(tái)配置下發(fā)
通過遠(yuǎn)程向監(jiān)測(cè)儀下發(fā)配置信息,修改溫濕度的告警值遠(yuǎn)程控制
硬件部分具備告警指示燈功能贴见,溫度烘苹、濕度值分別達(dá)到告警值時(shí),對(duì)應(yīng)的指示燈會(huì)亮起并保持常亮片部,通過遠(yuǎn)程指令消除告警狀態(tài)熄滅指示燈數(shù)據(jù)存儲(chǔ)(拓展)
平臺(tái)接收到信息后可以將溫濕度數(shù)據(jù)存儲(chǔ)镣衡,以便進(jìn)行數(shù)據(jù)展示、分析數(shù)據(jù)展示(拓展)
設(shè)備接入平臺(tái)后,我們通過大屏的對(duì)實(shí)時(shí)和歷史數(shù)據(jù)進(jìn)行儀表盤廊鸥、圖表等展示監(jiān)控望浩,同時(shí)可展示告警狀態(tài)
硬件清單
- 樹莓派3B+ 1臺(tái)
- DHT11溫濕度傳感器 1個(gè)
- LED指示燈(普通發(fā)光二極管) 2個(gè)
實(shí)現(xiàn)方案
- 使用樹莓派+node-red實(shí)現(xiàn)硬件控制,預(yù)裝Raspian系統(tǒng)的樹莓派自帶node-red惰说,不需要安裝磨德,使用其他操作系統(tǒng)的樹莓派可能需要單獨(dú)安裝一下,關(guān)于node-red的介紹和安裝本教程不做詳細(xì)介紹了吆视,不了解的同學(xué)可以自行百度學(xué)習(xí)
- 在平臺(tái)創(chuàng)建相應(yīng)的物模型和物影子典挑,獲取設(shè)備接入平臺(tái)需要的MQTT連接信息
- 使用樹莓派外接溫濕度傳感器以30s周期實(shí)時(shí)采集環(huán)境溫濕度,并上報(bào)到平臺(tái)
- 設(shè)置報(bào)警閾值啦吧,當(dāng)溫濕度達(dá)到設(shè)定值后您觉,樹莓派外接報(bào)警指示燈亮起,可通過微信小程序或控制臺(tái)遠(yuǎn)程消除報(bào)警狀態(tài)熄滅告警燈
- 設(shè)置數(shù)據(jù)自動(dòng)存儲(chǔ)到TSDB
- 配置物可視大屏實(shí)時(shí)顯示當(dāng)前溫濕度信息以及歷史數(shù)據(jù)曲線授滓、溫濕度超限報(bào)警信息
操作步驟
創(chuàng)建物模型和設(shè)備影子
物模型配置
首先按如下配置信息在控制臺(tái)創(chuàng)建一個(gè)物模型琳水,物模型中設(shè)置了6個(gè)屬性,分別代表溫濕度當(dāng)前值般堆、溫濕度告警指示燈狀態(tài)在孝、溫濕度告警閾值,詳細(xì)配置如下:
名稱:hygrothermoscope
描述:用于天工平臺(tái)能力演示的通用設(shè)備
序號(hào) | 顯示名稱 | 屬性名稱 | 數(shù)據(jù)類型 | 默認(rèn)值 | 單位 |
---|---|---|---|---|---|
1 | 溫度 | temperature | number | 0.00 | ℃ |
2 | 濕度 | humidity | number | 0.00 | %rh |
3 | 溫度告警 | temperature_alarm | number | 0 | |
4 | 濕度告警 | humidity_alarm | number | 0 | |
5 | 溫度閾值 | temperature_alarm_value | number | 25 | ℃ |
6 | 濕度閾值 | humidity_alarm_value | number | 40 | %rh |
配置頁(yè)面如圖:
物影子配置
創(chuàng)建完物模型后郁妈,為設(shè)備創(chuàng)建一個(gè)物影子浑玛,來獲得設(shè)備接入天工平臺(tái)需要的MQTT連接用戶名和密碼,創(chuàng)建成功后注意保存影子的連接信息噩咪,配置信息如下:
名稱:demo01
描述:——
物模型:hygrothermoscope
配置頁(yè)面如圖:
硬件開發(fā)和配置
- 硬件連接
按下表連接樹莓派和傳感器顾彰、告警指示燈
DHT11電氣參數(shù)
電源電壓:3~5.5V(典型值:5V);
溫度量程:0~50℃胃碾,誤差 ±2℃涨享;
濕度量程:20~90%RH,誤差 ±5%RH仆百;
采樣周期:大于等于1秒/次厕隧。
元器件 | 引腳 | 樹莓派GPIO |
---|---|---|
DHT11 | VDD | 5V Power(pin2) |
DHT11 | DATA | GPIO24(pin18) |
DHT11 | GND | Ground(pin14) |
溫度告警指示燈 | 正極 | GPIO23(pin16) |
溫度告警指示燈 | 負(fù)極 | Ground(pin6) |
濕度告警指示燈 | 正極 | GPIO25(pin22) |
濕度告警指示燈 | 負(fù)極 | Ground(pin9) |
連接后的實(shí)物圖:
-
程序編寫
為了讓整個(gè)程序更加便于理解和維護(hù),我們將程序的各個(gè)模塊寫成獨(dú)立node-red流程俄周,每個(gè)流程完成獨(dú)立的功能吁讨,node-red的啟動(dòng)可以參考 node-red安裝和啟動(dòng)教程
本實(shí)踐編寫完成的node-red流程 下載保存 后,可直接導(dǎo)入自己的設(shè)備中體驗(yàn)峦朗,只需修改任意MQTT節(jié)點(diǎn)中服務(wù)端地址建丧、用戶名和密碼即可,具體方法后邊教程有提示波势。流程模塊 描述 獲取傳感器數(shù)據(jù)流程 1s周期采集傳感器數(shù)據(jù)并保存在全局變量中 獲取配置信息流程 從云端拉取設(shè)備影子作為配置信息翎朱,初始化設(shè)備 上報(bào)溫濕度數(shù)據(jù)流程 30s周期上報(bào)溫濕度數(shù)據(jù) 告警觸發(fā)與上報(bào)流程 實(shí)時(shí)監(jiān)測(cè)溫濕度變化橄维,達(dá)到閾值時(shí)觸發(fā)告警指示燈點(diǎn)亮,并上報(bào)當(dāng)前設(shè)備狀態(tài) 接收云端指令流程 接收云端下發(fā)的溫濕度告警閾值配置拴曲、熄滅告警指示燈指令 -
獲取傳感器數(shù)據(jù)流程
-
莓派實(shí)時(shí)讀取DHT11數(shù)據(jù)争舞,并存儲(chǔ)在全局變量中,后續(xù)上報(bào)澈灼、告警邏輯均讀取全局變量中存儲(chǔ)的溫濕度數(shù)據(jù)
此處特別注意的一點(diǎn)竞川,rpi-dht11節(jié)點(diǎn)不是node-red內(nèi)置的節(jié)點(diǎn),需要單獨(dú)安裝蕉汪,具體安裝方法可參考 DTH11安裝方法
設(shè)置觸發(fā)器流译,樹莓派每1s對(duì)溫濕度進(jìn)行采樣更新逞怨,并進(jìn)行實(shí)時(shí)告警者疤,上報(bào)程序每30s上報(bào)一次,這樣就實(shí)現(xiàn)了數(shù)據(jù)采集和上報(bào)分離(采集和上報(bào)異步)
-
設(shè)置當(dāng)前溫濕度狀態(tài)function節(jié)點(diǎn)代碼
global.set('humidity',Number(msg.humidity).toFixed(2)) global.set('temperature',Number(msg.payload).toFixed(2)) return {};
-
-
獲取配置信息流程
-
樹莓派開機(jī)聯(lián)網(wǎng)后叠赦,主動(dòng)從云端下載配置信息驹马,初始化溫濕度告警閾值、初始化告警燈狀態(tài)
- 先檢測(cè)一下消息發(fā)布節(jié)點(diǎn)是否就緒除秀,使用get_shadow組裝payload糯累,向$baidu/iot/shadow/demo01/get主題發(fā)布消息,請(qǐng)求獲得設(shè)備影子册踩,get_shadow function節(jié)點(diǎn)代碼
var res={} var parmars={ "requestId": new Date().getTime(), } res.payload=parmars return res;
-
對(duì)$baidu/iot/shadow/demo01/get主題的訂閱泳姐,對(duì)應(yīng)node-red中的MQTT輸出節(jié)點(diǎn),第一次使用時(shí)添加該節(jié)點(diǎn)到界面內(nèi)暂吉,雙擊配置信息胖秒,會(huì)要求先選擇mqtt連接配置
-
點(diǎn)擊服務(wù)端右側(cè)的 編輯 按鈕新建mqtt客戶端,填寫連接信息慕的,服務(wù)端地址創(chuàng)建設(shè)備時(shí)獲得的接入地址
-
切換到安全tab下阎肝,設(shè)置MQTT連接用戶名和密碼,用戶名和密碼為創(chuàng)建設(shè)備時(shí)獲得的用戶名和密碼(特別提醒肮街,下載使用本教程中提供的完整流程時(shí)风题,請(qǐng)務(wù)必將此處用戶名和密碼修改為自己設(shè)備的用戶名密碼)
-
返回頁(yè)面選中剛剛新建的服務(wù)端,設(shè)置主題為$baidu/iot/shadow/demo01/get嫉父,點(diǎn)擊完成即可完成MQTT客戶端配置沛硅,此后再需要使用MQTT節(jié)點(diǎn)時(shí),不需要再次配置連接信息绕辖,只需要設(shè)置相應(yīng)的發(fā)布或訂閱主題即可
訂閱$baidu/iot/shadow/demo01/get/accepted主題摇肌,接收天工返回的設(shè)備影子信息,并使用初始化設(shè)備狀態(tài)引镊,function節(jié)點(diǎn)代碼
global.set('temperature_alarm_value',msg.payload.reported.temperature_alarm_value) global.set('humidity_alarm_value',msg.payload.reported.humidity_alarm_value) global.set('humidity_alarm',0) global.set('temperature_alarm',0) global.set('humidity',0) global.set('temperature',0) global.set('humidity_alarm_has_reported',false) global.set('temperature_alarm_has_reported',false) return {};
- 可同時(shí)訂閱$baidu/iot/shadow/demo01/update/rejected主題朦蕴,調(diào)試過程中篮条,如設(shè)備影子請(qǐng)求失敗,會(huì)在此接收到消息
-
-
上報(bào)溫濕度數(shù)據(jù)
-
設(shè)備初始化完成之后吩抓,我們來實(shí)現(xiàn)30s周期上報(bào)溫濕度信息到云端
-
設(shè)置觸發(fā)器涉茧,每30s觸發(fā)formate函數(shù)執(zhí)行一次,formate函數(shù)會(huì)從全局變量中獲得當(dāng)前設(shè)備記錄的溫濕度數(shù)值,組裝設(shè)備影子上報(bào)消息,向$baidu/iot/shadow/demo01/update主題發(fā)布更新設(shè)備影子信息是钥,formate function代碼
var res={} var parmars={ "requestId": new Date().getTime(), "reported": {} } parmars.reported.randomnum=Math.random(100)*1000 parmars.reported.temperature=global.get('temperature') parmars.reported.humidity=global.get('humidity') res.payload=parmars return res;
- 進(jìn)一步可以訂閱$baidu/iot/shadow/demo01/update/accepted來接收影子更新的結(jié)果
-
-
告警觸發(fā)與上報(bào)
-
告警是物聯(lián)網(wǎng)場(chǎng)景中常見的功能阴幌,我們使用本流程實(shí)現(xiàn)溫濕度達(dá)到閾值時(shí),設(shè)備告警指示燈點(diǎn)亮吭练,同時(shí),設(shè)備向云端發(fā)送設(shè)備當(dāng)前狀態(tài)信息
- 設(shè)置兩個(gè)觸發(fā)器分別監(jiān)測(cè)溫濕度值是否達(dá)到設(shè)置的閾值,觸發(fā)器以1s周期進(jìn)行檢測(cè)饺窿,與設(shè)備采集傳感器數(shù)據(jù)的周期保持一致
- 以濕度監(jiān)測(cè)為例,觸發(fā)器以全局變量中的humidity(濕度)值為輸入移斩,通過h_alarm_on函數(shù)來計(jì)算當(dāng)前值是否達(dá)到閾值肚医,通過switch進(jìn)行判斷達(dá)到閾值即點(diǎn)亮告警燈,同時(shí)通過向$baidu/iot/shadow/demo01/update發(fā)布消息向瓷,更新設(shè)備影子
- h_alarm_on代碼
-
var res={}
var turn_on=0
if(global.get('humidity')>global.get('humidity_alarm_value')){
global.set('humidity_alarm',1)
if(global.get('humidity_alarm_has_reported')){}else{ global.set('humidity_alarm_has_reported',true) turn_on=1 }
}
res.payload=turn_on
return res;
- formate代碼
var res={}
var parmars={
"requestId": new Date().getTime(),
"reported": {}
}
parmars.reported.randomnum=Math.random(100)*1000
parmars.reported.temperature=global.get('temperature')
parmars.reported.humidity=global.get('humidity')
parmars.reported.temperature_alarm=global.get('temperature_alarm')
parmars.reported.humidity_alarm=global.get('humidity_alarm')
res.payload=parmars
return res;
```- 接收云端指令
-
前邊兩個(gè)流程我們實(shí)現(xiàn)了數(shù)據(jù)上行肠套,接著我們編寫一個(gè)流程來接收云端下發(fā)給設(shè)備的指令,實(shí)現(xiàn)遠(yuǎn)程控制設(shè)備
-
通過天工控制臺(tái)物影子詳情頁(yè)或調(diào)用api接口可以更新設(shè)備影子中的‘desired’字段猖任,達(dá)到反控設(shè)備的目的你稚。物影子在接受到‘desired’字段更新后,會(huì)比較'reported'和‘desired’之間的差異朱躺,并將diff結(jié)果發(fā)送到主題'$baidu/iot/shadow/demo01/delta刁赖,設(shè)備通過訂閱該主題即可實(shí)時(shí)獲取云端下發(fā)的指令,進(jìn)行相應(yīng)的邏輯操作
- 云端可以下發(fā)指令設(shè)置溫濕度閾值室琢、熄滅告警指示燈乾闰,使用相應(yīng)的函數(shù)判斷云端指令類型進(jìn)行相應(yīng)的操作
- 關(guān)于設(shè)備影子的機(jī)制可參考 物影子操作 相關(guān)文檔
-
-
部署和運(yùn)行
經(jīng)過上邊的流程編寫之后,我們將程序部署到樹莓派中去盈滴,只需要點(diǎn)擊頁(yè)面右上角的部署涯肩,node-red會(huì)把我們編寫的程序自動(dòng)部署并運(yùn)行
接著我們就可以通過觀察控制臺(tái)設(shè)備影子數(shù)據(jù)變化驗(yàn)證我們的成果了。
step1:觀察控制臺(tái)設(shè)備影子數(shù)據(jù)變化巢钓,初始時(shí)像這樣病苗,設(shè)備開始上報(bào)數(shù)據(jù)了
step2:簡(jiǎn)單粗暴的方式,向傳感器哈氣症汹,當(dāng)然是用加濕器或電吹風(fēng)效果更突出硫朦,效果是這樣,溫濕度數(shù)值增高背镇,報(bào)警燈亮起
[圖片上傳失敗...(image-bb71af-1592968266047)]
遠(yuǎn)程下發(fā)指令
最后我們演示一下如何向設(shè)備下發(fā)指令咬展,下發(fā)指令操作我們使用控制臺(tái)提供的下發(fā)功能泽裳,當(dāng)然你也可以通過調(diào)用天工提供的api實(shí)現(xiàn),在我們的demo中設(shè)備可以響應(yīng)的指令有兩種破婆,一種是控制告警指示燈熄滅涮总,另一種遠(yuǎn)程設(shè)置告警閾值。
-
step1:設(shè)置告警閾值祷舀,打開設(shè)備影子詳情頁(yè)瀑梗,點(diǎn)擊編輯按鈕,設(shè)置濕度告警閾值為40
-
step2:點(diǎn)擊確定后裳扯,刷新影子狀態(tài)抛丽,可查看到濕度告警閾值當(dāng)前值已更新為40(設(shè)備接收到設(shè)置指令并設(shè)置成功后會(huì)上報(bào)更新后設(shè)備的當(dāng)前值)
-
step3:熄滅告警指示燈,首先我們通過對(duì)DHT11哈氣饰豺,觸發(fā)告警指示燈亮起
-
step4:下發(fā)指令熄滅告警燈亿鲜,我們將告警指示燈狀態(tài)值設(shè)置為0下發(fā)給設(shè)置點(diǎn)擊確定后,指示燈熄滅
設(shè)置數(shù)據(jù)自動(dòng)存儲(chǔ)到TSDB
打開demo01物影子詳情頁(yè)哟忍,切換到物詳情tab狡门,將6個(gè)屬性全部設(shè)置為“上報(bào)即存儲(chǔ)”陷寝,設(shè)置之后設(shè)備上報(bào)的所有數(shù)據(jù)將自動(dòng)存儲(chǔ)到我們?cè)O(shè)置的TSDB中
物可視大屏展示
使用物可視大屏將設(shè)備數(shù)據(jù)匯總顯示锅很,關(guān)于物可視的使用請(qǐng)閱讀物可視相關(guān)文檔,此處我們直接展示本次基于單臺(tái)設(shè)備通過拖拉拽零代碼實(shí)現(xiàn)的大屏效果凤跑,更多效果可以進(jìn)一步發(fā)掘
更多控制方法
此外爆安,你也可以發(fā)掘更多的控制方法,比如控制端通過創(chuàng)建MQTT連接仔引,向更新設(shè)備影子主題發(fā)布包含desired的消息扔仓,以下原始控制指令可參考
指令下發(fā)topic
$baidu/iot/shadow/demo01/update
設(shè)置閾值
{
"requestId":"1234567890",
"desired":{
"humidity_alarm_value":12,
"temperature_alarm_value":24
}
}
消除告警
{
"requestId":"1234567890",
"desired":{
"temperature_alarm":0,
"humidity_alarm":0
}
}
完整node-red流程
復(fù)制并粘貼保存為.json文件或 點(diǎn)擊下載,導(dǎo)入你的node-red即可使用(一定要記得修改MQTT節(jié)點(diǎn)的信息)
[{"id":"e0b82240.b53ae","type":"tab","label":"獲取環(huán)境溫濕度","disabled":false,"info":""},{"id":"79ed18b7.b0cba8","type":"tab","label":"獲取設(shè)備影子","disabled":false,"info":""},{"id":"fcc59dc.0f9396","type":"tab","label":"上報(bào)設(shè)備狀態(tài)","disabled":false,"info":""},{"id":"eb9cfc66.b315","type":"tab","label":"指示燈告警","disabled":false,"info":""},{"id":"cc69204f.1129b","type":"tab","label":"接收云端指令","disabled":false,"info":""},{"id":"876561eb.4dc06","type":"mqtt-broker","z":"","name":"mqtt-demo01","broker":"1cb210f148044b6c8ac45382ef0d9e02.mqtt.iot.gz.baidubce.com","port":"1883","clientid":"demo01","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"aeaf02d5.9bfb","type":"mqtt out","z":"79ed18b7.b0cba8","name":"","topic":"$baidu/iot/shadow/demo01/get","qos":"0","retain":"","broker":"876561eb.4dc06","x":670,"y":60,"wires":[]},{"id":"1c4ebe.2fe0c143","type":"mqtt in","z":"79ed18b7.b0cba8","name":"","topic":"$baidu/iot/shadow/demo01/get/accepted","qos":"0","datatype":"json","broker":"876561eb.4dc06","x":220,"y":160,"wires":[["32f0f23.b929f0e"]]},{"id":"32f0f23.b929f0e","type":"function","z":"79ed18b7.b0cba8","name":"初始化設(shè)備","func":"\n\nglobal.set('temperature_alarm_value',msg.payload.reported.temperature_alarm_value)\n\nglobal.set('humidity_alarm_value',msg.payload.reported.humidity_alarm_value)\n\nglobal.set('humidity_alarm',0)\n\nglobal.set('temperature_alarm',0)\n\nglobal.set('humidity',0)\n\nglobal.set('temperature',0)\n\nglobal.set('humidity_alarm_has_reported',false)\n\nglobal.set('temperature_alarm_has_reported',false)\n\n\n\nreturn {};\n\n\n","outputs":1,"noerr":0,"x":520,"y":160,"wires":[["27781181.55b2be"]]},{"id":"27781181.55b2be","type":"debug","z":"79ed18b7.b0cba8","name":"獲取影子成功","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":740,"y":160,"wires":[]},{"id":"ad0d021d.af49f","type":"function","z":"79ed18b7.b0cba8","name":"get_shadow","func":"var res={}\n\nvar parmars={\n \"requestId\": new Date().getTime(),\n}\n\nres.payload=parmars\n\nreturn res;","outputs":1,"noerr":0,"x":370,"y":60,"wires":[["aeaf02d5.9bfb"]]},{"id":"1e5c7528.4ac8eb","type":"status","z":"79ed18b7.b0cba8","name":"","scope":["1c4ebe.2fe0c143"],"x":140,"y":60,"wires":[["ad0d021d.af49f"]]},{"id":"55874635.ff36b8","type":"inject","z":"fcc59dc.0f9396","name":"周期上報(bào)","topic":"","payload":"","payloadType":"date","repeat":"30","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":260,"wires":[["464a70e6.efb22","b17d0f7c.856ed"]]},{"id":"464a70e6.efb22","type":"function","z":"fcc59dc.0f9396","name":"formate","func":"var res={}\n\nvar parmars={\n \"requestId\": new Date().getTime(),\n \"reported\": {}\n}\nparmars.reported.randomnum=Math.random(100)*1000\n\nparmars.reported.temperature=global.get('temperature')\n\nparmars.reported.humidity=global.get('humidity')\n\n\nres.payload=parmars\n\nreturn res;","outputs":1,"noerr":0,"x":380,"y":260,"wires":[["a4e7196e.af6578","66b34bbc.208864"]]},{"id":"a4e7196e.af6578","type":"mqtt out","z":"fcc59dc.0f9396","name":"","topic":"$baidu/iot/shadow/demo01/update","qos":"0","retain":"","broker":"876561eb.4dc06","x":720,"y":260,"wires":[]},{"id":"bcdb86b3.317e38","type":"rpi-gpio out","z":"cc69204f.1129b","name":"溫度告警燈","pin":"16","set":true,"level":"0","freq":"","out":"out","x":650,"y":200,"wires":[]},{"id":"41b5ee5b.2e3fa","type":"mqtt out","z":"cc69204f.1129b","name":"","topic":"$baidu/iot/shadow/demo01/update","qos":"0","retain":"","broker":"876561eb.4dc06","x":980,"y":320,"wires":[]},{"id":"1b30c52e.29897b","type":"function","z":"cc69204f.1129b","name":"t_alarm_off","func":"\nvar res={}\nvar turn_on=0\n\nif(msg.payload.desired.temperature_alarm==1){\n turn_on=1\n}\nres.payload=turn_on\n\nreturn res;","outputs":1,"noerr":0,"x":390,"y":160,"wires":[["479a6a71.773114"]]},{"id":"8a6175a2.3e85a8","type":"function","z":"cc69204f.1129b","name":"h_alarm_off","func":"var res={}\nvar turn_on=0\n\nif(msg.payload.desired.humidity_alarm==1){\n turn_on=1\n}\n\n\nres.payload=turn_on\n\nreturn res;","outputs":1,"noerr":0,"x":390,"y":280,"wires":[["d9c514fd.bd0d88"]]},{"id":"4fab39be.a5d838","type":"rpi-gpio out","z":"cc69204f.1129b","name":"濕度告警燈","pin":"22","set":true,"level":"0","freq":"","out":"out","x":650,"y":280,"wires":[]},{"id":"67b64145.91291","type":"mqtt in","z":"cc69204f.1129b","name":"","topic":"$baidu/iot/shadow/demo01/delta","qos":"0","datatype":"json","broker":"876561eb.4dc06","x":150,"y":280,"wires":[["1b30c52e.29897b","8a6175a2.3e85a8","c2f39cb9.96667","4f0b2611.ff8238"]]},{"id":"8b3c3146.b77f2","type":"function","z":"cc69204f.1129b","name":"t_alarm_reported","func":"\nvar res={}\nglobal.set('temperature_alarm',0)\nglobal.set('temperature_alarm_has_reported',false)\n\nvar parmars={\n \"requestId\": new Date().getTime(),\n \"reported\": {}\n}\nparmars.reported.randomnum=Math.random(100)*1000\n\nparmars.reported.temperature_alarm=msg.payload==1?1:0\n\n\nres.payload=parmars\n\nreturn res;","outputs":1,"noerr":0,"x":670,"y":160,"wires":[["41b5ee5b.2e3fa","c207e56d.a67738"]]},{"id":"67886b16.9d5274","type":"function","z":"cc69204f.1129b","name":"h_alarm_reported","func":"\n\nvar res={}\nglobal.set('humidity_alarm',0)\nglobal.set('humidity_alarm_has_reported',false)\nvar parmars={\n \"requestId\": new Date().getTime(),\n \"reported\": {}\n}\nparmars.reported.randomnum=Math.random(100)*1000\n\nparmars.reported.humidity_alarm=msg.payload==1?1:0\n\n\nres.payload=parmars\n\nreturn res;","outputs":1,"noerr":0,"x":670,"y":320,"wires":[["41b5ee5b.2e3fa","c207e56d.a67738"]]},{"id":"4ff05547.39024c","type":"function","z":"cc69204f.1129b","name":"set_alarm","func":"var parmars={\n \"requestId\": new Date().getTime(),\n \"reported\": {\n temperature_alarm_value:global.get('temperature_alarm_value')||100,\n humidity_alarm_value:global.get('humidity_alarm_value')||100\n }\n}\n\nif(msg.payload.desired.temperature_alarm_value){\n global.set('temperature_alarm_value',msg.payload.desired.temperature_alarm_value)\n parmars.reported.temperature_alarm_value=msg.payload.desired.temperature_alarm_value\n}\n\nif(msg.payload.desired.humidity_alarm_value ){\n global.set('humidity_alarm_value',msg.payload.desired.humidity_alarm_value) \n parmars.reported.humidity_alarm_value=msg.payload.desired.humidity_alarm_value\n}\n\n\nmsg.payload=parmars\n\nreturn msg;","outputs":1,"noerr":0,"x":640,"y":420,"wires":[["41b5ee5b.2e3fa","c207e56d.a67738"]]},{"id":"d9c514fd.bd0d88","type":"switch","z":"cc69204f.1129b","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":410,"y":320,"wires":[["67886b16.9d5274","4fab39be.a5d838"]]},{"id":"479a6a71.773114","type":"switch","z":"cc69204f.1129b","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":410,"y":200,"wires":[["8b3c3146.b77f2","bcdb86b3.317e38"]]},{"id":"c2f39cb9.96667","type":"function","z":"cc69204f.1129b","name":"if_set_alarm","func":"\nvar parmars=msg.payload.desired.hasOwnProperty(\"temperature_alarm_value\")||msg.payload.desired.hasOwnProperty(\"humidity_alarm_value\")\n\nmsg.to_set=parmars\n\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":380,"wires":[["bdaa5c71.ca261"]]},{"id":"bdaa5c71.ca261","type":"switch","z":"cc69204f.1129b","name":"","property":"to_set","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":410,"y":420,"wires":[["4ff05547.39024c"]]},{"id":"8b4e4b3d.6d7ae8","type":"mqtt in","z":"fcc59dc.0f9396","name":"","topic":"$baidu/iot/shadow/demo01/update/accepted","qos":"0","datatype":"json","broker":"876561eb.4dc06","x":230,"y":400,"wires":[["66b34bbc.208864"]]},{"id":"66b34bbc.208864","type":"debug","z":"fcc59dc.0f9396","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":630,"y":400,"wires":[]},{"id":"ca8cd639.9a4258","type":"debug","z":"79ed18b7.b0cba8","name":"獲取影子失敗","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":540,"y":260,"wires":[]},{"id":"a9da906e.29bde","type":"mqtt in","z":"79ed18b7.b0cba8","name":"","topic":"$baidu/iot/shadow/demo01/update/rejected","qos":"0","datatype":"json","broker":"876561eb.4dc06","x":230,"y":260,"wires":[["ca8cd639.9a4258"]]},{"id":"2981152c.63017a","type":"rpi-dht22","z":"e0b82240.b53ae","name":"讀DHT11數(shù)據(jù)","topic":"rpi-dht11","dht":"11","pintype":"0","pin":"18","x":400,"y":240,"wires":[["a2c9546.eeb9ca8","b835ff81.33c0c"]]},{"id":"a2c9546.eeb9ca8","type":"function","z":"e0b82240.b53ae","name":"設(shè)置設(shè)備當(dāng)前溫濕度狀態(tài)","func":"\nglobal.set('humidity',Number(msg.humidity).toFixed(2))\nglobal.set('temperature',Number(msg.payload).toFixed(2))\n\nreturn {};","outputs":1,"noerr":0,"x":630,"y":240,"wires":[["c3c0dca4.3972d"]]},{"id":"6662a21c.a9726c","type":"inject","z":"e0b82240.b53ae","name":"周期觸發(fā)器","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":240,"wires":[["2981152c.63017a"]]},{"id":"cb542070.5ebac","type":"rpi-gpio out","z":"eb9cfc66.b315","name":"溫度告警燈","pin":"16","set":true,"level":"0","freq":"","out":"out","x":790,"y":420,"wires":[]},{"id":"aecdb85d.c5d758","type":"rpi-gpio out","z":"eb9cfc66.b315","name":"濕度告警燈","pin":"22","set":true,"level":"0","freq":"","out":"out","x":790,"y":240,"wires":[]},{"id":"3da13ab1.337fb6","type":"inject","z":"eb9cfc66.b315","name":"","topic":"","payload":"humidity","payloadType":"global","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":240,"wires":[["85ca2d2.6cbc7d"]]},{"id":"69c07bf5.efa0c4","type":"inject","z":"eb9cfc66.b315","name":"","topic":"","payload":"temperature","payloadType":"global","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":420,"wires":[["72da2f99.47e7b"]]},{"id":"65a20d4f.e76724","type":"mqtt out","z":"eb9cfc66.b315","name":"","topic":"$baidu/iot/shadow/demo01/update","qos":"0","retain":"","broker":"876561eb.4dc06","x":1060,"y":360,"wires":[]},{"id":"72da2f99.47e7b","type":"function","z":"eb9cfc66.b315","name":"t_alarm_on","func":"var res={}\nvar turn_on=0\n\n\n\nif(global.get('temperature')>global.get('temperature_alarm_value')){\n\n if(global.get('temperature_alarm_has_reported')){\n\n}else{\n global.set('temperature_alarm_has_reported',true) \n turn_on=1\n}\n}\nres.payload=turn_on\n\nreturn res;","outputs":1,"noerr":0,"x":450,"y":420,"wires":[["7066ca16.508d64"]]},{"id":"85ca2d2.6cbc7d","type":"function","z":"eb9cfc66.b315","name":"h_alarm_on","func":"\nvar res={}\nvar turn_on=0\nif(global.get('humidity')>global.get('humidity_alarm_value')){\n global.set('humidity_alarm',1)\n if(global.get('humidity_alarm_has_reported')){\n \n }else{\n global.set('humidity_alarm_has_reported',true)\n turn_on=1\n }\n}\nres.payload=turn_on\nreturn res;","outputs":1,"noerr":0,"x":450,"y":240,"wires":[["4dd08a6c.87ecc4"]]},{"id":"7066ca16.508d64","type":"switch","z":"eb9cfc66.b315","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":610,"y":420,"wires":[["cb542070.5ebac","c83ff371.eaa0e"]]},{"id":"4dd08a6c.87ecc4","type":"switch","z":"eb9cfc66.b315","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":610,"y":240,"wires":[["aecdb85d.c5d758","c83ff371.eaa0e"]]},{"id":"c3c0dca4.3972d","type":"debug","z":"e0b82240.b53ae","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":850,"y":240,"wires":[]},{"id":"c83ff371.eaa0e","type":"function","z":"eb9cfc66.b315","name":"formate","func":"var res={}\n\nvar parmars={\n \"requestId\": new Date().getTime(),\n \"reported\": {}\n}\nparmars.reported.randomnum=Math.random(100)*1000\n\nparmars.reported.temperature=global.get('temperature')\n\nparmars.reported.humidity=global.get('humidity')\n\nparmars.reported.temperature_alarm=global.get('temperature_alarm')\n\nparmars.reported.humidity_alarm=global.get('humidity_alarm')\n\n\nres.payload=parmars\n\nreturn res;","outputs":1,"noerr":0,"x":780,"y":340,"wires":[["65a20d4f.e76724","c3d36c20.b83a6"]]},{"id":"c3d36c20.b83a6","type":"debug","z":"eb9cfc66.b315","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":990,"y":280,"wires":[]},{"id":"4f0b2611.ff8238","type":"debug","z":"cc69204f.1129b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":390,"y":520,"wires":[]},{"id":"c207e56d.a67738","type":"debug","z":"cc69204f.1129b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":930,"y":220,"wires":[]},{"id":"b835ff81.33c0c","type":"change","z":"e0b82240.b53ae","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"100","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":400,"wires":[["a490710e.a86f7"]]},{"id":"a490710e.a86f7","type":"debug","z":"e0b82240.b53ae","name":"測(cè)試change","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":890,"y":400,"wires":[]},{"id":"b17d0f7c.856ed","type":"change","z":"fcc59dc.0f9396","name":"組裝數(shù)據(jù)","rules":[{"t":"set","p":"payload.requestId","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":140,"wires":[["791b173b.086ba8"]]},{"id":"791b173b.086ba8","type":"debug","z":"fcc59dc.0f9396","name":"組裝數(shù)據(jù)結(jié)果","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":680,"y":140,"wires":[]}]