"""
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
"""
import network
import ubinascii
import machine
from Display_Module import DisplayModule
from Time_Module import TimeModule
import temperatureModule
import MQQTSettings
import MQQTModule
from WeatherData import WeatherData
import Setting_Module
class Main:
def __init__(self):
print("Init main.py")
settings = Setting_Module(self)
# #PINS SETUP
# self.LED_SCL_PIN= 22
# self.LED_SDA_PIN=21
# self.TEMP_DHT_PIN=15 #DHT22 Sensor Module
# self.CURRENT_DEVICENAME="HRTSK001"
# self.SLEEPTIMER_TOREADAGAIN_INSEC = 5
# #MQQT SERVER SETTINGS - START
# self.MQTT_CLIENT_ID = ubinascii.hexlify(machine.unique_id())
# self.MQTT_BROKER = "7ba792769c5548b7a087b43beb88df5d.s2.eu.hivemq.cloud:8884"
# self.MQTT_USER = "HeatSeeker_push_001"
# self.MQTT_PASSWORD = "B!F2b@qTBB!NvFL"
# self.MQTT_TOPIC = "WeatherMonitor"
#MQQT SERVER SETTINGS - END
self.displayModuleObj = None
self.tempModuleObj= None
self.mqqtModuleObj= None
self.timeObj = None
self.prev_weather = None
def setup_wifi(self):
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!")
def setup(self):
try:
# global displayModuleObj, tempModuleObj, mqqtModuleObj,timeObj # Marking these global so that assignment goes to the global obj instead of the scope
self.setup_wifi()
#Setup Time
self.timeObj = TimeModule()
#Display Module
self.displayModuleObj = DisplayModule(settings.LED_SCL_PIN,settings.LED_SDA_PIN,settings.CURRENT_DEVICENAME)
#Temperature Sensor Module
self.tempModuleObj = temperatureModule.temperatureModule(settings.TEMP_DHT_PIN, settings.timeObj)
#MQQT Module
if settings.ISMQQTENABLED:
messageSetting= MQQTSettings.MQQTSettings(settings.MQTT_CLIENT_ID,settings.MQTT_BROKER,settings.MQTT_USER,sesettingslf.MQTT_PASSWORD,settings.MQTT_TOPIC,settings.CURRENT_DEVICENAME)
self.mqqtModuleObj = MQQTModule.MQQTModule(messageSetting)
except Exception as e:
print("Problem in setup(). Exception: "+str(e))
raise(e)
def loop(self):
# global displayModuleObj, tempModuleObj, mqqtModuleObj, prev_weather, timeObj # Marking these global so that we access global obj instead of the scope
while True:
print("Measuring weather conditions... ", end="")
curr_weather = self.tempModuleObj.readValues()
#Display the weather in the screen
self.displayModuleObj.displayWeather(curr_weather.temperature,curr_weather.humidity)
print(self.prev_weather)
if WeatherData.hasWeatherChanged(curr_weather,self.prev_weather):
print(str(curr_weather))
self.mqqtModuleObj.sendMessage(curr_weather) #If temperature chaned send mqqt msg
self.prev_weather = curr_weather
else:
print("No change")
self.timeObj.sleep(self.SLEEPTIMER_TOREADAGAIN_INSEC)
def main(self):
print("Main() Start, Initiating Setup")
self.setup()
print("Main() Setup Complete, Starting Loop")
self.loop()
print("Main() End")
entry = Main()
entry.main()