[ Linkit Smart 7688 ] 將 Sensing Data 傳送到 AWS IoT

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


情境


執行步驟

前置準備作業

  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. AWS 帳號 - 註冊 AWS 帳號


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


設定 AWS IoT 服務

請參考 [ Linkit Smart 7688 ] 傳送 Sensing Data 到 AWS IoT (NEW Version)


設定 AWS DynamoDB 服務

請參考 [ Linkit Smart 7688 ] 設定 AWS DynamoDB 服務 (NEW Version)


設定 AWS Lambda 服務

請參考 [ Linkit Smart 7688 ] 設定 AWS Lambda 服務


設定 AWS IoT 與 AWS Lambda 的 Rule 端

請參考 [ Linkit Smart 7688 ] 設定 AWS IoT 與 AWS Lambda 的 Rule


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); //每秒回傳一次資料
}


Linkit Smart 7688 Duo - MPU ( Python ) 端

Step 1. 透過 Python 讀取 MCU Sensor Data

更新程式中的 host、certificatePath 與 privateKeyPath 的名稱

from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient  
import sys  
import logging  
import time  
import getopt  
import json  
import datetime

# Read in command-line parameters
host = ""  
rootCAPath = "./root-CA.crt"  
certificatePath = "./.cert.pem"  
privateKeyPath = "./.private.key"

myAWSIoTMQTTClient = AWSIoTMQTTClient("publish")  
myAWSIoTMQTTClient.configureEndpoint(host, 8883)  
myAWSIoTMQTTClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath)

# AWSIoTMQTTClient connection configuration
myAWSIoTMQTTClient.configureAutoReconnectBackoffTime(1, 32, 20)  
myAWSIoTMQTTClient.configureOfflinePublishQueueing(-1)  # Infinite offline Publish queueing  
myAWSIoTMQTTClient.configureDrainingFrequency(2)  # Draining: 2 Hz  
myAWSIoTMQTTClient.configureConnectDisconnectTimeout(10)  # 10 sec  
myAWSIoTMQTTClient.configureMQTTOperationTimeout(5)  # 5 sec

# Connect and subscribe to AWS IoT
myAWSIoTMQTTClient.connect()

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

# Publish to the same topic in a loop forever
while True:  
    humidity = value.get("Humidity")
    temperature = value.get("Temperature")
    print "Humi: " + humidity
    print "Temp: " + temperature

    t = time.time();
    date = datetime.datetime.fromtimestamp(t).strftime('%Y%m%d%H%M%S')
    print "humidity: %d, temperature: %d" % (float(humidity), float(temperature))
    myAWSIoTMQTTClient.publish("sensingData/TemperatureHumidity/Room2", json.dumps({"time": date, "temperature": temperature, "humidity": humidity}), 1)
    time.sleep(1)


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


Step 6. DynamoDB 畫面


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