|
1
|
|
|
'''this one pushes to mydevices''' |
|
2
|
|
|
import time |
|
3
|
|
|
import sys |
|
4
|
|
|
import paho.mqtt.client as mqtt |
|
5
|
|
|
import Adafruit_DHT |
|
6
|
|
|
|
|
7
|
|
|
print('Waiting 30 seconds in case wireless needs to initialize...') |
|
8
|
|
|
time.sleep(30) #Sleep to allow wireless to connect before starting MQTT |
|
9
|
|
|
|
|
10
|
|
|
#TODO: Move to config |
|
11
|
|
|
USERNAME = "mydevices_name" |
|
12
|
|
|
PASSWORD = "mydevices_password" |
|
13
|
|
|
CLIENTID = "mydevices_clientid" |
|
14
|
|
|
|
|
15
|
|
|
MQTTC = mqtt.Client(client_id=CLIENTID) |
|
16
|
|
|
MQTTC.username_pw_set(USERNAME, password=PASSWORD) |
|
17
|
|
|
MQTTC.connect("mqtt.mydevices.com", port=1883, keepalive=60) |
|
18
|
|
|
MQTTC.loop_start() |
|
19
|
|
|
|
|
20
|
|
|
TOPIC_TEMP = "v1/" + USERNAME + "/things/" + CLIENTID + "/data/3" |
|
21
|
|
|
TOPIC_HUMIDITY = "v1/" + USERNAME + "/things/" + CLIENTID + "/data/4" |
|
22
|
|
|
|
|
23
|
|
|
while True: |
|
24
|
|
|
try: |
|
25
|
|
|
#pin 2 or 4 = power, pin 6 = gnd, pin 7 = gpio4 |
|
26
|
|
|
#https://www.raspberrypi.org/documentation/usage/gpio-plus-and-raspi2/README.md |
|
27
|
|
|
HUMIDITY22, TEMP22 = Adafruit_DHT.read_retry(22, 4) |
|
28
|
|
|
#22 is the sensor type, 4 is the GPIO pin number (not physical pin number) |
|
29
|
|
|
|
|
30
|
|
|
if TEMP22 is not None: |
|
31
|
|
|
TEMP22 = "temp,c=" + str(TEMP22) |
|
32
|
|
|
MQTTC.publish(TOPIC_TEMP, payload=TEMP22, retain=True) |
|
33
|
|
|
if HUMIDITY22 is not None: |
|
34
|
|
|
HUMIDITY22 = "rel_hum,p=" + str(HUMIDITY22) |
|
35
|
|
|
MQTTC.publish(TOPIC_HUMIDITY, payload=HUMIDITY22, retain=True) |
|
36
|
|
|
time.sleep(5) |
|
37
|
|
|
except (EOFError, SystemExit, KeyboardInterrupt): |
|
38
|
|
|
MQTTC.disconnect() |
|
39
|
|
|
sys.exit() |
|
40
|
|
|
|