本文描述如何將 物聯(lián)設(shè)備 通過 MQTT協(xié)議 與 sitewhere 對接起來。
環(huán)境準備
在Ubuntu系統(tǒng)里,安裝 sitewhere docker。在docker hub上已經(jīng)有了sitewhere image。
sudo docker pull sitewhere/standalone:1.8.0
特別要注意的就是要指定版本。另外,安裝的是standalone版本。
sudo docker run -p 80:8080 -p 1883:1883 -p 61623:61623 sitewhere/standalone:1.8.0
使用上面的命令啟動sitewhere。
啟動后,就可以通過瀏覽器訪問 sitewhere web console。
MQTT client
在Ubuntu上安裝mosquitto。
發(fā)送MQTT消息
sitewhere standalone版本里,包含了mqtt broker。所以,可以直接向sitewhere 發(fā)送MQTT消息。
準備下面的數(shù)據(jù),保存到 loc.json文件。
{
"hardwareId": "7c1c95e9-eb01-422f-9aec-fd64094d01e3",
"type":"DeviceLocation",
"request": {
"latitude": "33.75",
"longitude": "-84.39",
"elevation": "0",
"updateState": true,
"eventDate": "2018-05-17T19:40:03.390Z"
}
}
mosquitto_pub -h 127.0.0.1 -f loc.json -t SiteWhere/input/json
sitewhere會在它的 mqtt broker 里訂閱 sitewhere/input/json 主題。所以,要向這個topic發(fā)消息。消息數(shù)據(jù)中的 hardwareId,是在sitewhere里定義的 device 。 sitewhere安裝好后,里面有一個例子。這里的這個hardwareId就取自例子中的一個 device。
發(fā)送了消息后,我們就可以在sitewhere中看到。

我們還可以發(fā)送 Alert 消息:
{
"hardwareId": "7c1c95e9-eb01-422f-9aec-fd64094d01e3",
"type":"DeviceAlert",
"request": {
"type": "engine.overheat",
"level": "Warning",
"message": "The engine is about to overheat! Turn the machine off!",
"updateState": false,
"eventDate": "2018-05-17T19:40:03.391Z",
"metadata": {
"name1": "value1",
"name2": "value2"
}
}
}

發(fā)送數(shù)據(jù)格式可參考 http://documentation.sitewhere.io/userguide/sending-data.html
物聯(lián)設(shè)備狀態(tài)變更,觸發(fā)事件
上面的Alert,是外部設(shè)備直接發(fā)來的 Alert 消息。如果需要支持 condition trigger,需要與openHAB集成。
sudo docker pull openhab/openhab:2.2.0-amd64-debian
https://groups.google.com/forum/#!topic/sitewhere/w5Obz3MjHkU
https://docs.openhab.org/installation/docker.html#obtaining-the-official-image-from-dockerhub
http://documentation.sitewhere.io/integration/openhab.html
== 待繼 ==