ที่มา : https://community.mydevices.com/t/sht10-sensor/2450
Wiring the SHT10 Sensor:
Red Wire - to 3.3V
Green Wire - to GND
Blue Wire(Data) to PIN23
Yellow Wire(Clock) to PIN24
Important: Use 10K pullUp resistor from VCC(Red Wire) to DATA(Blue Wire).
Install Library:
I use this library:
pip3 install pi-sht1x
if there is error, you have to uprade the pip3 with the following command:sudo easy_install3 -U pip
Also before start writing your python code, you have to install paho-mqtt for python3
sudo pip3 install paho-mqtt
Go to Cayenne Dashboard -> Add New -> Devices & Widgets and press the big blue button Add Your Own Thing
Then copy your MQTT Username, MQTT Password and Cliend ID to the following places in the code:
import paho.mqtt.client as mqtt
import time
import sys
from time import sleep
from pi_sht1x import SHT1x
import RPi.GPIO as GPIO
import datetime
#time.sleep(60) #Sleep to allow wireless to connect before starting MQTT
mqttc = mqtt.Client(client_id="YOUR CLIENT ID")
mqttc.username_pw_set("YOUR USERNAME", password="YOUR PASSWORD")
mqttc.connect("mqtt.mydevices.com", port=1883, keepalive=60)
topic_sht10_temp = "v1/YOUR USERNAME/things/CLIENT ID/data/1"
topic_sht10_humidity = "v1/YOUR USERNAME/things/CLIENT ID/data/2"
while True:
try:
with SHT1x(23, 24, gpio_mode=GPIO.BCM) as sensor:
temp11 = sensor.read_temperature()
humidity11 = sensor.read_humidity(temp11)
if temp11 is not None:
temp11 = "temp,c=" + str(temp11)
mqttc.publish(topic_sht10_temp, payload=temp11, retain=True)
if humidity11 is not None:
humidity11 = "rel_hum,p=" + str(humidity11)
mqttc.publish(topic_sht10_humidity, payload=humidity11, retain=True)
sleep(5)
time.sleep(5)
except (EOFError, SystemExit, KeyboardInterrupt):
mqttc.disconnect()
sys.exit()
Save and run the file using:
sudo python3 yourfilename.py
ไม่มีความคิดเห็น:
แสดงความคิดเห็น