與 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 開發(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í)行畫面

責(zé)任編輯:wv
-
python
+關(guān)注
關(guān)注
57文章
4877瀏覽量
90078 -
IOT
+關(guān)注
關(guān)注
190文章
4397瀏覽量
208644 -
樹莓派
+關(guān)注
關(guān)注
122文章
2079瀏覽量
110488
發(fā)布評(píng)論請(qǐng)先 登錄
紡織車間溫濕度數(shù)據(jù)采集遠(yuǎn)程監(jiān)控系統(tǒng)方案
冷庫(kù)溫濕度監(jiān)控系統(tǒng)物聯(lián)網(wǎng)解決方案
1-Wire溫濕度傳感器DTU配電單元解決方案
用CW32F030做溫濕度采集,ADC讀數(shù)跳變超過(guò)±3LSB正常嗎?
睿擎平臺(tái)實(shí)戰(zhàn):溫濕度采集到MQTT云端通信
溫濕度傳感器HTU31D在溫濕度控器中的應(yīng)用優(yōu)勢(shì)
基于LORA無(wú)線通訊的祥控多點(diǎn)式煙葉溫濕度監(jiān)測(cè)系統(tǒng)
常見(jiàn)的溫濕度傳感器類型?
溫濕度變送器功能有哪些?一文詳細(xì)解析
智能倉(cāng)儲(chǔ):溫濕度監(jiān)控方案應(yīng)用
用樹莓派RP2350 DIY 桌面動(dòng)態(tài)溫濕度計(jì)
LoRa無(wú)線技術(shù)的溫濕度監(jiān)測(cè)預(yù)警系統(tǒng)
溫濕度傳感器:核心原理與跨領(lǐng)域應(yīng)用解析
制藥車間溫濕度數(shù)據(jù)采集監(jiān)控管理方案
如何操作電子溫濕度記錄儀
怎樣用Python透過(guò)樹莓派采集到的溫濕度信息傳送到AWS IoT
評(píng)論