Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- * * * * * /home/pi/mqtt-dht11s.py
- #!/usr/bin/python3
- # Program to read current temperature and humidity and post single message to MQTT BROKER
- # 2019-01-20
- import Adafruit_DHT as dht
- import paho.mqtt.publish as publish
- import json
- import datetime
- #
- # Sensor should be set to Adafruit_DHT.DHT11, Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
- sensor = dht.DHT11
- pin = 4
- BROKER = 'localhost'
- sensor_data = {'date': 0, 'temperature': 0, 'humidity': 0}
- topicPrefix = "weather"
- try:
- humidity,temperature = dht.read_retry(sensor, pin)
- humidity = round(humidity, 2)
- temperature = round(temperature, 2)
- date = datetime.datetime.now().replace(microsecond=0).isoformat()
- # print(u"Date: {:s}, Temperature: {:g}u00b0C, Humidity: {:g}%".format(date, temperature, humidity))
- sensor_data['temperature'] = temperature
- sensor_data['humidity'] = humidity
- sensor_data['date'] = date
- publish.single(topicPrefix, payload=json.dumps(sensor_data), qos=1, retain=True, hostname=BROKER,
- port=1883, client_id="RaspberryPi", keepalive=60)
- except KeyboardInterrupt:
- pass
- #
- # Config file for sSMTP sendmail
- #
- # The person who gets all mail for userids < 1000
- # Make this empty to disable rewriting.
- root=USER.raspberry.pi@gmail.com
- # The place where the mail goes. The actual machine name is required no
- # MX records are consulted. Commonly mailhosts are named mail.domain.com
- mailhub=smtp.gmail.com:587
- AuthUser=USER.raspberry.pi@gmail.com
- AuthPass=…
- AuthMethod=LOGIN
- UseTLS=YES
- UseSTARTTLS=YES
- # Where will the mail seem to come from?
- rewriteDomain=gmail.com
- # The full hostname
- hostname=RaspberryPi.USER_HOST
- # Are users allowed to set their own From: address?
- # YES - Allow the user to specify their own From: address
- # NO - Use the system generated From: address
- FromLineOverride=YES
Add Comment
Please, Sign In to add comment