本文來源於 Alliotcloud 物聯雲 與 Makee.io 專欄 - 作者 Archer Huang @ Makee.io
工業電腦大廠新漢為推動物聯網加速創新,宣布將開發已久的 NEXCOM IoT Studio 連網軟體工具開放免費下載,讓開發人員毋須編寫複雜程式碼,即可完成連線設定,本文描述如何將聯發科 Linkit Smart 7688 Duo 的 Sensing Data 透過 WebSocket 傳送到 IoT Studio。
執行步驟
前置準備作業
準備 Linkit Smart 7688 Duo 開發板
準備 Arduino Breakout for LinkIt Smart 7688 Duo
準備 Grove - Temperature & Humidity Sensor
將 Linkit Smart 7688 Duo 與 Arduino Breakout for LinkIt Smart 7688 Duo 組裝、Grove - Temperature & Humidity Sensor 裝到 A0 的位置並將 Linkit Smart 7688 Duo 連接至電腦
安裝 Arduino IDE (version: 1.6.4)
https://www.arduino.cc/en/Main/OldSoftwareReleases電腦安裝 IDE ( Sublime Text or Visual Studio Code )
Windows 平台,請安裝 Putty 與 FileZilla
http://oranwind.org/-linkit/準備 Raspberry Pi 3 Model B 開發板
Raspberry Pi 3 Model B 開發板中已安裝 Raspbian 作業系統 【 安裝方式 】
已安裝 IoT Studio 於 Raspberry Pi 中 【 安裝方式 】
已在 IoT Studio 中建立完成 WebSocket 程式【 安裝方式 】
Linkit Smart 7688 Duo 與 Grove - Temperature & Humidity Sensor 連接圖
Linkit Smart 7688 Duo - MCU ( Arduino ) 端
Step 1. 透過 MCU 讀取 Sensor Data
// DHT
#include "DHT.h"
#define DHTPIN A0
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#include <Bridge.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
dht.begin();
Bridge.begin();
}
void loop() {
// put your main code here, to run repeatedly:
float h = dht.readHumidity();
float t = dht.readTemperature();
// Bridge
Bridge.put("Humidity", String(h));
Bridge.put("Temperature", String(t));
delay(1000); //每秒回傳一次資料
}
Linkit Smart 7688 Duo - MPU ( Python ) 端
Step 1. 透過 Python 讀取 MCU Sensor Data
import time
import sys
import websocket
import datetime
sys.path.insert(0, '/usr/lib/python2.7/bridge/')
from bridgeclient import BridgeClient as bridgeclient
value = bridgeclient()
websocket.enableTrace(True)
ws = websocket.create_connection("ws://192.168.43.219:1880/ws/temperature/receive")
while True:
h0 = value.get("Humidity")
t0 = value.get("Temperature")
t = time.time();
date = datetime.datetime.fromtimestamp(t).strftime('%Y%m%d%H%M%S')
vals = "{\"date\" : \"" + date + "\", \"temperature\":" + t0 + "\", \"humidity\":" + h0 + "}"
print vals;
ws.send(vals)
time.sleep(1)
Step 2. 將 Python Code 傳送到 Linkit Smart 7688 Duo
傳送檔案與登入到開發板 【 Link 】
Step 3. 安裝相關套件於 Linkit Smart 7688 Duo
1. update opkg & install wget & disable bridge
opkg update
opkg install wget
uci set yunbridge.config.disabled=0
uci commit
reboot
2. install setuptools
curl https://bootstrap.pypa.io/ez_setup.py -k -o - | python
3. install six
wget --no-check-certificate https://pypi.python.org/packages/source/s/six/six-1.10.0.tar.gz
tar zxvf six-1.10.0.tar.gz
cd six-1.10.0
python setup.py install
4. install Websocket
wget --no-check-certificate https://pypi.python.org/packages/source/w/websocket-client/websocket_client-0.32.0.tar.gz
tar zxvf websocket_client-0.32.0.tar.gz
cd websocket_client-0.32.0
python setup.py install
Step 4. 執行剛傳到 Linkit Smart 7688 Duo 中的 Python Code
python 檔名.py
python sensingDataToAllIoTCloudWebSocketSend.py
Step 5. Console 執行畫面
Step 6. IoT Studio 上顯示從 Linkit Smart 7688 Duo 所傳進來的 Sensing Data
Step 7. 從 IoT Studio 取得資料
import websocket
websocket.enableTrace(True)
ws = websocket.create_connection("ws://192.168.43.219:1880/ws/temperature/view")
while True:
result = ws.recv()
print "Received '%s'" % result
Step 8. 執行 Python Code
python 檔名.py
python sensingDataToAllIoTCloudWebSocketReceive.py
Step 9. Console 執行畫面
參考資料
- https://www.raspberrypi.org/downloads/raspbian/
- https://www.raspberrypi.org/documentation/installation/installing-images/mac.md