[ Linkit Smart 7688 ] 傳送 Sensor Data 到 ThingSpeak

與 IoT 相關的雲端服務為數眾多,例如:ThingSpeakWoT.CityAWSIBM BluemixMediaTek MCS 等,並在透過這些雲端服務可在對資料進行資料視覺化、資料分析與其它的應用,而本文透過 Linkit Smart 7688 Duo 將 Sensor 資料傳送到 ThingSpeak 並視覺化顯示出相關資訊 。

Imgur

執行步驟

Imgur

前置準備作業

  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.6)
    https://www.arduino.cc/en/Main/OldSoftwareReleases

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

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

Linkit Smart 7688 Duo 與 Grove - Temperature & Humidity Sensor 連接圖

Imgur

ThingSpeak 端

  • Step 1. 到 ThingSpeak 網站申請帳號

https://thingspeak.com/

  • Step 2. 登入 ThingSpeak 網站 Imgur

  • Step 3. 點擊上方 Channels -> My Channels Imgur

  • Step 4. 點擊左方的 New Channel Imgur

  • Step 5. 設定 Channel 的名字、描述、設定欄位名稱、Enable Make Public Imgur

  • Step 6. 設定完成畫面 Imgur

  • Step 7. 點擊中間的 API Keys,以取得 API Key Imgur Imgur

Linkit Smart 7688 Duo - MCU ( Arduino ) 端

  • Step 1. 透過 MCU 讀取 Sensor Data
// MCU To MPU
#include <Bridge.h>

// DHT
#include "DHT.h"
#define DHTPIN A0
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);

void setup() {  
  Serial.begin(9600); 
  dht.begin();
  Bridge.begin();
}

void loop() {  
  // Get Temperature and Humidity
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // Display Temperature and Humidity Value
  Serial.print("DHT-Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("DHT-Temperature: "); 
  Serial.print(t);
  Serial.println(" *C\t");

  // MCU To MPU
  Bridge.put("Humidity", String(h));
  Bridge.put("Temperature", String(t));

  delay(1000); //每秒回傳一次資料
}

Imgur

Linkit Smart 7688 Duo - MPU ( Python ) 端

  • Step 1. 透過 Python 讀取 MCU Sensor Data
import sys  
import time  
import httplib, urllib

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

def post_to_thingspeak(payload):  
    headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
    not_connected = 1
    while (not_connected):
        try:
            conn = httplib.HTTPConnection("api.thingspeak.com:80")
            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", "/update", payload, headers)
    response = conn.getresponse()
    print( response.status, response.reason, payload, time.strftime("%c"))
    data = response.read()
    conn.close()

while True:  
    h0 = value.get("Humidity")
    t0 = value.get("Temperature")
    print "Humi: " + h0
    print "Temp: " + t0
    params = urllib.urlencode({'field1': t0, 'field2': h0, 'key': thinkSpeakApiKey})
    post_to_thingspeak(params)
    time.sleep(5)

Imgur

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

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

opkg update  
uci set yunbridge.config.disabled=0  
uci commit  
reboot  
  • Step 4. 執行剛傳到 Linkit Smart 7688 Duo 中的 Python Code
python 檔名.py  
  • Step 5. Console 執行畫面 Imgur

  • Step 6. ThingSpeak 畫面 Imgur


GitHub
List of blogs

Archer

Having being a full stack engineer. Experience with C, Python, Go, Swift, JavaScript, Node.js, LinkIt 7697, LinkIt Smart 7688, Raspberry Pi, ARM mbed, IoT solutions. Contact us : [email protected]

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