[ Linkit Smart 7688 ] 傳送 Sensor Data 到 AWS IoT

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

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

AWS IoT 端

  • Step 1. 到 AWS 網站申請帳號 https://aws.amazon.com/tw/

  • Step 2. 登入 AWS 網站 Imgur

  • Step 3. 點擊 Services -> AWS IoT Imgur

  • Step 4. 點擊 Create a resource Imgur

  • Step 5. 點擊 Create a thing -> 輸入 thing Name -> Create Imgur Imgur

  • Step 6. 點擊剛建立的 thing Imgur

  • Step 7. 點擊 Connect a device Imgur

  • Step 8. 點擊 Node.js -> Generate certificate and policy Imgur

  • Step 9. 下載 private 與 public keys Imgur

  • Step 10. 點擊 Confirm & connecting Imgur

  • Step 11. 點擊 Return to Thing Detail 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
#pip install paho-mqtt

import time  
import sys  
import websocket  
import socket  
import datetime  
import paho.mqtt.client as paho  
import ssl  
import os  
import json

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

def on_connect(client, userdata, flags, rc):  
    global connflag
    connflag = True
    print("Connection returned result: " + str(rc) )

def on_message(client, userdata, msg):  
    print(msg.topic+" "+str(msg.payload))

mqttc = paho.Client()  
mqttc.on_connect = on_connect  
mqttc.on_message = on_message

awshost = "a3iprlpgye4dmu.iot.us-west-2.amazonaws.com"  
awsport = 8883  
clientId = "sensorData"  
thingName = "sensorData"  
caPath = "./root-CA.crt"  
certPath = "./17b281e33b-certificate.pem.crt"  
keyPath = "./17b281e33b-private.pem.key"

mqttc.tls_set(caPath, certfile=certPath, keyfile=keyPath, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)  
mqttc.connect(awshost, awsport, keepalive=60)  
mqttc.loop_start()

while True:  
    humidity = value.get("Humidity")
    temperature = value.get("Temperature")
    print "%d degrees humidity" % float(humidity)
    print "%d degrees temperature" % float(temperature)

    t = time.time();
    date = datetime.datetime.fromtimestamp(t).strftime('%Y%m%d%H%M%S')
    if connflag == True:
        mqttc.publish("topic/sensorData", json.dumps({"time": date, "temperature": temperature, "humidity": humidity}), qos=1)
    else:
        print("waiting for connection...")
    time.sleep(1)

Imgur

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

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

update opkg & disable bridge  
opkg update  
uci set yunbridge.config.disabled=0  
uci commit  
pip install paho-mqtt  
reboot  
  • Step 4. 執行剛傳到 Linkit Smart 7688 Duo 中的 Python Code
python 檔名.py  
  • Step 5. Console 執行畫面 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