import network
import time
import machine
from machine import Pin, ADC, UART, SPI, SDCard
import dht
import ujson
import os
sensor = dht.DHT22(Pin(15))
sd = SDCard(slot=3, mosi=Pin(13), mosi=Pin(12), sck=Pin(14, cs=Pin(5)))
os.mount(sd, "/sd mounted")
prev_weather = ""
while True:
print("Measuring weather conditions... ", end="")
sensor.measure()
message = ujson.dumps({
"temp": sensor.temperature(),
"humidity": sensor.humidity(),
})
if message != prev_weather:
print("Updated!")
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, message))
client.publish(MQTT_TOPIC, message)
with open("temp.txt", "w") as f:
f.write("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, message))
prev_weather = message
else:
print("No change")
time.sleep(1)