[ Linkit Smart 7688 ] 透過 HTTP 傳送 Sensing Data 到 IoT Studio

本文來源於 Alliotcloud 物聯雲Makee.io 專欄 - 作者 Archer Huang @ Makee.io

工業電腦大廠新漢為推動物聯網加速創新,宣布將開發已久的 NEXCOM IoT Studio 連網軟體工具開放免費下載,讓開發人員毋須編寫複雜程式碼,即可完成連線設定,本文描述如何將聯發科 Linkit Smart 7688 Duo 的 Sensing Data 透過 HTTP 傳送到 IoT Studio。


執行步驟


前置準備作業

  1. 準備 Linkit Smart 7688 Duo 開發板

  2. 準備 Arduino Breakout for LinkIt Smart 7688 Duo

  3. 準備 Grove - Temperature & Humidity Sensor

  4. 將 Linkit Smart 7688 Duo 與 Arduino Breakout for LinkIt Smart 7688 Duo 組裝、Grove - Temperature & Humidity Sensor 裝到 A0 的位置並將 Linkit Smart 7688 Duo 連接至電腦

  5. 安裝 Arduino IDE (version: 1.6.4)
    https://www.arduino.cc/en/Main/OldSoftwareReleases

  6. 電腦安裝 IDE ( Sublime Text or Visual Studio Code )

  7. Windows 平台,請安裝 Putty 與 FileZilla
    http://oranwind.org/-linkit/

  8. 準備 Raspberry Pi 3 Model B 開發板

  9. Raspberry Pi 3 Model B 開發板中已安裝 Raspbian 作業系統 【 安裝方式 】

  10. 已安裝 IoT Studio 於 Raspberry Pi 中 【 安裝方式 】

  11. 已在 IoT Studio 中建立完成 HTTP 程式【 安裝方式 】


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 datetime  
import sys  
import httplib, urllib  
import json

sys.path.insert(0, '/usr/lib/python2.7/bridge/')  
from bridgeclient import BridgeClient as bridgeclient

value = bridgeclient()

def post_to_alliotcloud(payload):  
    headers = {"Content-type": "application/json",}
    not_connected = 1
    while (not_connected):
        try:
            conn = httplib.HTTPConnection("192.168.43.219:1880")
            conn.connect()
            not_connected = 0
        except (httplib.HTTPException, socket.error) as ex:
            print "Error: %s" % ex
            time.sleep(10)  # sleep 10 seconds

    conn.request("POST", "/v1/temperatureHumidity", json.dumps(payload), headers)
    response = conn.getresponse()
    print( response.status, response.reason, json.dumps(payload), time.strftime("%c"))
    data = response.read()
    conn.close()

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')
    payload = {"date":date , "temperature":t0, "humidity":h0}
    post_to_alliotcloud(payload)
    time.sleep(10)


Step 2. 將 Python Code 傳送到 Linkit Smart 7688 Duo

傳送檔案與登入到開發板 【 Link 】


Step 3. 安裝相關套件於 Linkit Smart 7688 Duo

1. update opkg & disable bridge

opkg update  
uci set yunbridge.config.disabled=0  
uci commit  
reboot  

Step 4. 執行剛傳到 Linkit Smart 7688 Duo 中的 Python Code

python 檔名.py

python sensingDataToAllIoTCloudHTTPSend.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 執行畫面


參考資料
GitHub
List of blogs

Archer

Having being a full stack engineer. Experience with C, Python, Objective-C, Swift, Node.js, Lua, Linkit Smart 7688, Raspberry Pi, ARM mbed, Arduino, IoT solutions. Contact us : [email protected]

ALL RIGHTS RESERVED. COPYRIGHT © 2016. Designed and Coded by Makee.io