import network #Allows the device to connect to a Wi-Fi network.
import time #Provides functions to control time and playback delay.
from machine import Pin, I2C #Pin: Used to interact with the GPIO connections on the device.
#I2C: Used to communicate with external devices such as OLED displays.
import dht #Used to read data from DHT sensors
import ujson #Used to convert data to JSON format and vice versa, making it easier to interact with the MQTT broker and display data on the OLED display.
from umqtt.simple import MQTTClient #Used to connect to an MQTT broker and publish and receive messages.
from ssd1306 import SSD1306 #Contains functions to control OLED screens using the SSD1306 microcontroller. They are used to display data on the OLED screen in this project.
#MQTT server connection settings
MQTT_CLIENT_ID = "micropython-weather-demo" #The one the device uses when connecting to an MQTT server.
MQTT_BROKER = "broker.mqttdashboard.com" #It is the server that the device will connect to to publish messages or subscribe to Topics.
MQTT_USER = ""
MQTT_PASSWORD = ""
MQTT_TOPIC = "wokwi-weather"
def setup_wifi(): #The goal is to connect the device to a WiFi network.
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
return sta_if
def connect_mqtt(): #It is intended to connect the device to an MQTT (Message Queuing Telemetry Transport) server.
print("Connecting to MQTT server... ", end="")
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
client.connect()
print("Connected!")
return client
def read_sensor(): #The goal is to read data from the DHT22 sensor and return the read values.
sensor = dht.DHT22(Pin(15))
sensor.measure()
temperature = 30
humidity = 50
return {"temp": temperature, "humidity": humidity}
def display_on_oled(data): #The goal is to display temperature and humidity data on an OLED display using the I2C protocol.
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = SSD1306(128, 64, i2c)
oled.fill(0)
oled.text("Temp: " + str(data["temp"]) + "C", 0, 0)
oled.text("Humidity: " + str(data["humidity"]) + "%", 0, 16)
oled.show()
def publish_to_mqtt(client, data): #The goal is to publish the measured data (temperature and humidity) to the MQTT server.
message = ujson.dumps(data)
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, message))
client.publish(MQTT_TOPIC, message)
def main():
wifi = setup_wifi() #Call setup_wifi function to connect to WiFi network and Store the WiFi connection object in the variable wifi.
mqtt_client = connect_mqtt() #Call the connect_mqtt function to connect to the MQTT server and Store the MQTT client object in the mqtt_client variable.
prev_weather = "" #Initialize the variable prev_weather as an empty string to store the previous weather condition.
while True:
data = read_sensor() #Call read_sensor function to read sensor data.
display_on_oled(data) #Call the display_on_oled function to display temperature and humidity data on the OLED display.
if ujson.dumps(data) != prev_weather:#Convert the data dictionary into a JSON text string and compare it with the previous case.
#If the current data is different from the previous data, the next block of code is executed.
print("Updated!")
publish_to_mqtt(mqtt_client, data) #Call the publish_to_mqtt function to publish the new data to the MQTT server.
prev_weather = ujson.dumps(data) #Update the variable prev_weather to store the current data after converting it to a JSON string.
else:
print("No change")
time.sleep(1) #A one-second delay before starting the next cycle of the loop.
if __name__ == "__main__":
main()