與 IoT 相關(guān)的云端服務(wù)為數(shù)眾多,例如:ThingSpeak、WoT.City、AWS、IBM Bluemix、MediaTek MCS、Google Firebase 等,并在透過(guò)這些云端服務(wù)可在對(duì)數(shù)據(jù)進(jìn)行數(shù)據(jù)可視化、數(shù)據(jù)分析與其它的應(yīng)用,而本文用Python將透過(guò) Raspberry Pi 采集到的溫濕度信息傳送到 AWS IoT。
情境
材料與準(zhǔn)備
1.準(zhǔn)備 Raspberry Pi 3Model B 開(kāi)發(fā)板
2.準(zhǔn)備 Micro SD (已安裝 Raspbian)
3.將 Micro SD 裝入到 RaspberryPi 中
4.設(shè)定好網(wǎng)絡(luò)
5.安裝 GrovePi+
6.安裝 Grove – Temperatureand Humidity Sensor (D4)
7.安裝 GROVE - LCD RGBBACKLIGHT (I2C-2)
8.安裝 IDE ( Sublime Textor Visual Studio Code )
Sensor, LCD, GrovePi+ 與 RaspberryPi 連接如下圖
AWS IoT 端
Step 1. 到 AWS 網(wǎng)站申請(qǐng)賬號(hào)
Step 2. 登入 AWS 網(wǎng)站
Step 3. 點(diǎn)擊 Services AWS IoT
Step 4. 點(diǎn)擊 Create a resource
Step 5. 點(diǎn)擊 Create a thing 輸入 thing Name Create
Step 6. 點(diǎn)擊剛建立的 thing
Step 7. 點(diǎn)擊 Connect a device
Step 8. 點(diǎn)擊 Node.js Generate certificate and policy
Step 9. 下載 private 與 public keys
Step 10. 點(diǎn)擊 Confirm &connecting
Step 11. 點(diǎn)擊 Return to ThingDetail
Raspberry Pi 端
Step 1. 透過(guò) 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 傳送到 RaspberryPi
Step 3. 執(zhí)行剛傳到 Raspberry Pi 中的 Python 代碼,python 檔名.py
Step 4. Console 執(zhí)行畫(huà)面
責(zé)任編輯:wv
-
python
+關(guān)注
關(guān)注
55文章
4774瀏覽量
84386 -
IOT
+關(guān)注
關(guān)注
186文章
4163瀏覽量
196027 -
樹(shù)莓派
+關(guān)注
關(guān)注
116文章
1696瀏覽量
105457
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論