"""
MicroPython IoT Weather Station Example for Wokwi.com
To view the data:
1. Go to http://www.hivemq.com/demos/websocket-client/
2. Click "Connect"
3. Under Subscriptions, click "Add New Topic Subscription"
4. In the Topic field, type "wokwi-weather" then click "Subscribe"
Now click on the DHT22 sensor in the simulation,
change the temperature/humidity, and you should see
the message appear on the MQTT Broker, in the "Messages" pane.
Copyright (C) 2022, Uri Shaked
https://wokwi.com/arduino/projects/322577683855704658
"""
from time import sleep
from instrument import Monitor, Client
from sys import exit
from ujson import dumps
client = Client(network = r'network.json')
try_connection = 0
while not client.connect():
sleep(10)
try_connection += 1
if try_connection > 3:
exit()
monitor = Monitor(config = r'config.json')
rooms = monitor.rooms
last_readed = dict(zip(rooms, [''] * len(rooms)))
while True:
current_readed = monitor.to_monitor()
for rm in rooms:
if last_readed[rm] != current_readed[rm]:
last_readed[rm] = current_readed[rm]
topic = rm
message = dumps(current_readed[rm])
client.publish(
topic = topic,
message = message
)
print('published:', 'topic:', topic, 'message:', message)
sleep(1)