"""
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 time
from machine import Pin, I2C, PWM
import dht
import ssd1306
# import ujson
# from umqtt.simple import MQTTClient
# # MQTT Server Parameters
# MQTT_CLIENT_ID = "micropython-weather-demo"
# MQTT_BROKER = "broker.mqttdashboard.com"
# MQTT_USER = ""
# MQTT_PASSWORD = ""
# MQTT_TOPIC = "wokwi-weather"
sensor = dht.DHT22(Pin(15))
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
shallWater = 0
led = Pin(2, Pin.OUT)
p13 = Pin(13, Pin.OUT)
servo = PWM(p13, freq=50)
# Creates a function for mapping the 0 to 180 degrees
# to 20 to 120 pwm duty values
def map(x, in_min, in_max, out_min, out_max):
return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
# Creates another function for turning
# the servo according to input angle
def rotateServo(pin, angle):
pin.duty(map(angle, 0, 180, 20, 120))
def waterPlants():
# To rotate the servo from 0 to 180 degrees
# by 10 degrees increment
for i in range(0, 181, 10):
rotateServo(servo, i)
time.sleep(0.5)
# To rotate the servo from 180 to 0 degrees
# by 10 degrees decrement
for i in range(180, -1, -10):
rotateServo(servo, i)
time.sleep(0.5)
def main():
# 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!")
# print("Connecting to MQTT server... ", end="")
# client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
# client.connect()
# print("Connected!")
prev_weather = ""
message = ""
while True:
# print("Measuring weather conditions... ", end="")
sensor.measure()
# message = ujson.dumps({
# "temp": sensor.temperature(),
# "humidity": sensor.humidity(),
# })
message = {
"temp": sensor.temperature(),
"humidity": sensor.humidity(),
}
print(message)
# If the current result is not equal to the previous (it changed), clear the lcd
if message != prev_weather:
print("Updated!")
oled.fill(0)
# Continue running this code block
# print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, message))
oled.text("Temp: {}".format(str(sensor.temperature())), 10, 10)
oled.show()
oled.text("Humidity: {}".format(str(sensor.humidity())), 10, 20)
oled.show()
# client.publish(MQTT_TOPIC, message)
prev_weather = message
# If humidity or temp is greater than 50% or 50C respectively, activate servo
if (sensor.temperature() > 50.0) or (sensor.humidity() > 50.0):
shallWater = 1
# Deactive servo if not
else:
shallWater = 0
# If servo is activated, activate LED and execute servo motor function
# Inside the infinite while so this gets executed indefinitely
if (shallWater == 1):
led.value(1)
waterPlants()
# If servo is deactived, deactivate LED and put the servo to its off position
# Inside the infinite while so this gets executed indefinitely
else:
led.value(0)
rotateServo(servo, 101)
### Commented-out codeblock - just in case na need pa, can uncomment.
# # water the plants for two iterations
# for _ in range(2):
# waterPlants()
# else:
# print("No change")
# # led.value(0)
# # rotateServo(servo, 101)
time.sleep(1) # Delay of one (1) second
if __name__ == '__main__':
main()Loading
ssd1306
ssd1306