-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqttsendpi.py
executable file
·35 lines (31 loc) · 1.05 KB
/
mqttsendpi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/python3
import paho.mqtt.client as mqtt
import Adafruit_DHT
import time
import os
import sys
from datetime import timezone
from datetime import date
from datetime import datetime
mqttBroker = "test.mosquitto.org"
client = mqtt.Client("Sensor")
client.connect(mqttBroker)
while True:
today = datetime.now()
dy = today.strftime("%d")
mn = today.strftime("%m")
yr = today.strftime("%Y")
hr = today.strftime("%H")
min = today.strftime("%M")
sec = today.strftime("%S")
timestamp=today.strftime("%Y-%m-%dT%H:%M:%S.000Z")
sensor=Adafruit_DHT.DHT11
gpio=17
humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)
if humidity is not None and temperature is not None:
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
client.publish("yotta/db/temp", str(timestamp) + "#" + "{0:0.1f}".format(temperature))
client.publish("yotta/db/humid", str(timestamp) + "#" + "{0:0.1f}".format(humidity))
else:
print('Failed to get reading. Try again!')
time.sleep(60)