Guest User

Untitled

a guest
Jan 20th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. * * * * * /home/pi/mqtt-dht11s.py
  2.  
  3. #!/usr/bin/python3
  4. # Program to read current temperature and humidity and post single message to MQTT BROKER
  5. # 2019-01-20
  6.  
  7. import Adafruit_DHT as dht
  8. import paho.mqtt.publish as publish
  9. import json
  10. import datetime
  11. #
  12. # Sensor should be set to Adafruit_DHT.DHT11, Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
  13. sensor = dht.DHT11
  14. pin = 4
  15.  
  16. BROKER = 'localhost'
  17.  
  18. sensor_data = {'date': 0, 'temperature': 0, 'humidity': 0}
  19. topicPrefix = "weather"
  20.  
  21. try:
  22. humidity,temperature = dht.read_retry(sensor, pin)
  23. humidity = round(humidity, 2)
  24. temperature = round(temperature, 2)
  25. date = datetime.datetime.now().replace(microsecond=0).isoformat()
  26. # print(u"Date: {:s}, Temperature: {:g}u00b0C, Humidity: {:g}%".format(date, temperature, humidity))
  27. sensor_data['temperature'] = temperature
  28. sensor_data['humidity'] = humidity
  29. sensor_data['date'] = date
  30. publish.single(topicPrefix, payload=json.dumps(sensor_data), qos=1, retain=True, hostname=BROKER,
  31. port=1883, client_id="RaspberryPi", keepalive=60)
  32. except KeyboardInterrupt:
  33. pass
  34.  
  35. #
  36. # Config file for sSMTP sendmail
  37. #
  38. # The person who gets all mail for userids < 1000
  39. # Make this empty to disable rewriting.
  40. root=USER.raspberry.pi@gmail.com
  41.  
  42. # The place where the mail goes. The actual machine name is required no
  43. # MX records are consulted. Commonly mailhosts are named mail.domain.com
  44. mailhub=smtp.gmail.com:587
  45.  
  46. AuthUser=USER.raspberry.pi@gmail.com
  47. AuthPass=…
  48. AuthMethod=LOGIN
  49. UseTLS=YES
  50. UseSTARTTLS=YES
  51.  
  52. # Where will the mail seem to come from?
  53. rewriteDomain=gmail.com
  54.  
  55. # The full hostname
  56. hostname=RaspberryPi.USER_HOST
  57.  
  58. # Are users allowed to set their own From: address?
  59. # YES - Allow the user to specify their own From: address
  60. # NO - Use the system generated From: address
  61. FromLineOverride=YES
Add Comment
Please, Sign In to add comment