Skip to content

Instantly share code, notes, and snippets.

@li2hub
Last active February 25, 2019 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save li2hub/71f6be020907d10a72bdd8649928c0c0 to your computer and use it in GitHub Desktop.
Save li2hub/71f6be020907d10a72bdd8649928c0c0 to your computer and use it in GitHub Desktop.
import Adafruit_DHT
import paho.mqtt.client as paho
broker="192.168.xx.xxx"
sensor = Adafruit_DHT.DHT11
pin = 4
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
def hum_temp():
if humidity is not None and temperature is not None:
return 'Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity)
else:
return 'Failed to get reading. Try again!'
client= paho.Client("control-001")
client.hum_temp=hum_temp
client.connect(broker)
client.publish("sensor_data",hum_temp())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment