You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
IoT-For-Beginners/2-farm/lessons/1-predict-plant-growth/code-publish-temperature/pi/temperature-sensor/app.py

29 lines
564 B

import time
from seeed_dht import DHT
import paho.mqtt.client as mqtt
import json
sensor = DHT("11", 5)
id = '<ID>'
client_telemetry_topic = id + '/telemetry'
client_name = id + 'temperature_sensor_client'
mqtt_client = mqtt.Client(client_name)
mqtt_client.connect('test.mosquitto.org')
mqtt_client.loop_start()
print("MQTT connected!")
while True:
_, temp = sensor.read()
telemetry = json.dumps({'temperature' : temp})
print("Sending telemetry ", telemetry)
mqtt_client.publish(client_telemetry_topic, telemetry)
time.sleep(10 * 60)