[ Raspberry Pi ] 透過 Python 傳送溫溼度資訊到 AWS IoT

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


情境


前置準備作業

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

2.準備 Micro SD (已安裝 Raspbian) - 【 透過 MacOS 安裝 Raspbian 教學 】

3.將 Micro SD 裝入到 Raspberry Pi 中

4.已設定網路

5.安裝 GrovePi+

6.安裝 Grove – Temperature and Humidity Sensor (D4)

7.安裝 GROVE - LCD RGB BACKLIGHT (I2C-2)

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

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

10.其它前置作業
請參考 [ Raspberry Pi ] 透過 Python 顯示溫溼度資訊到 Grove LCD


Sensor 、 LCD 、 GrovePi+ 與 Raspberry Pi 連接圖


AWS IoT 端

Step 1. 到 AWS 網站申請帳號


Step 2. 登入 AWS 網站


Step 3. 點擊 ServicesAWS IoT


Step 4. 點擊 Create a resource


Step 5. 點擊 Create a thing ➙ 輸入 thing NameCreate


Step 6. 點擊剛建立的 thing


Step 7. 點擊 Connect a device


Step 8. 點擊 Node.jsGenerate certificate and policy


Step 9. 下載 private 與 public keys


Step 10. 點擊 Confirm & connecting


Step 11. 點擊 Return to Thing Detail


Raspberry Pi 端

Step 1. 透過 Python 讀取溫溼度資訊並傳送到 AWS IoT

#**************************************************** 
# Import Package                                                                           
#**************************************************** 

import time  
import datetime  
import paho.mqtt.client as paho  
import json  
import ssl  
import sys  
sys.path.append('/home/pi/rpi/code/Package')  
import grovepi  
from grove_rgb_lcd import *

#**************************************************** 
# Set Pin No, AWS Config                                                                          
#**************************************************** 

sensor = 4  
blue = 0    # The Blue colored sensor.  
white = 1   # The White colored sensor.

connflag = False

#**************************************************** 
# Set AWS Connection                                                   
#**************************************************** 

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 = "./000cd28455-certificate.pem.crt"  
keyPath = "./000cd28455-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()

#**************************************************** 
# Publish AWS                                                  
#**************************************************** 

while True:  
    [temp,humidity] = grovepi.dht(sensor,blue)  
    print("temp = %.02f C humidity =%.02f%%"%(temp, humidity))
    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": temp, "humidity": humidity}), qos=1)
    else:
        print("waiting for connection...")
    time.sleep(1)


Step 2. 將 Python Code 與 Keys 傳送到 Raspberry Pi


Step 3. 執行剛傳到 Raspberry Pi 中的 Python Code

python 檔名.py  

Step 4. 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