與 IoT 相關(guān)的云端服務(wù)為數(shù)眾多,例如:ThingSpeak、WoT.City、AWS、IBM Bluemix、MediaTek MCS、Google Firebase 等,并在透過這些云端服務(wù)可在對數(shù)據(jù)進(jìn)行數(shù)據(jù)可視化、數(shù)據(jù)分析與其它的應(yīng)用,而本文用Python將透過 Raspberry Pi 采集到的溫濕度信息傳送到 AWS IoT。

情境

材料與準(zhǔn)備
1.準(zhǔn)備 Raspberry Pi 3Model B 開發(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)站申請賬號
Step 2. 登入 AWS 網(wǎng)站

Step 3. 點擊 Services AWS IoT

Step 4. 點擊 Create a resource

Step 5. 點擊 Create a thing 輸入 thing Name Create


Step 6. 點擊剛建立的 thing

Step 7. 點擊 Connect a device

Step 8. 點擊 Node.js Generate certificate and policy

Step 9. 下載 private 與 public keys

Step 10. 點擊 Confirm &connecting

Step 11. 點擊 Return to ThingDetail


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 傳送到 RaspberryPi
Step 3. 執(zhí)行剛傳到 Raspberry Pi 中的 Python 代碼,python 檔名.py
Step 4. Console 執(zhí)行畫面

責(zé)任編輯:wv
-
python
+關(guān)注
關(guān)注
56文章
4849瀏覽量
89187 -
IOT
+關(guān)注
關(guān)注
189文章
4358瀏覽量
205684 -
樹莓派
+關(guān)注
關(guān)注
122文章
2067瀏覽量
109120
發(fā)布評論請先 登錄
溫濕度變送器功能有哪些?一文詳細(xì)解析
智能倉儲:溫濕度監(jiān)控方案應(yīng)用
用樹莓派RP2350 DIY 桌面動態(tài)溫濕度計
LoRa無線技術(shù)的溫濕度監(jiān)測預(yù)警系統(tǒng)
制藥車間溫濕度數(shù)據(jù)采集監(jiān)控管理方案
通過手機(jī)端遠(yuǎn)程監(jiān)控冷庫溫濕度并進(jìn)行遠(yuǎn)程控制
工廠溫濕度監(jiān)控系統(tǒng)
庫房溫濕度監(jiān)測系統(tǒng)
遠(yuǎn)程溫濕度實時監(jiān)測系統(tǒng)應(yīng)用方案
博物館溫濕度監(jiān)控系統(tǒng)
醫(yī)藥倉庫溫濕度監(jiān)控系統(tǒng)應(yīng)用
農(nóng)業(yè)溫濕度監(jiān)控系統(tǒng)
XKCON祥控倉庫存儲環(huán)境溫濕度在線監(jiān)測系統(tǒng)能夠取代人工巡檢,實現(xiàn)遠(yuǎn)程倉庫存儲環(huán)境溫濕度變化的實時

怎樣用Python透過樹莓派采集到的溫濕度信息傳送到AWS IoT
評論