"""
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, utime
import ntptime
from machine import Pin, ADC, RTC
import dht
import ujson
from umqtt.simple import MQTTClient
# MQTT Server Parameters
MQTT_CLIENT_ID = b"esp_test"
MQTT_BROKER = b"f9b9e46629b944a4b376428b075f83ef.s1.eu.hivemq.cloud"
MQTT_USER = b"jonas"
MQTT_PASSWORD = b"BugzHotel1"
MQTT_TOPIC = b"wokwi-weather"
EPOC_TIME = 946684800 ## 1.1.2000 00.00.00
p0 = Pin(0, Pin.OUT)
p1 = Pin(4, Pin.OUT)
p2 = Pin(16, Pin.OUT)
p3 = Pin(17, Pin.OUT)
gpios_sens = [p3, p2, p1, p0]
pot = ADC(Pin(35))
pot.atten(ADC.ATTN_11DB)
ref = ADC(Pin(34))
ref.atten(ADC.ATTN_11DB)
sensor = dht.DHT22(Pin(15))
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!")
time.sleep(1)
print("Connecting to MQTT server... ", end="")
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, port=8883, user=MQTT_USER, password=MQTT_PASSWORD, ssl=True, ssl_params={'server_hostname':"f9b9e46629b944a4b376428b075f83ef.s1.eu.hivemq.cloud"})
client.connect()
print("Connected!")
print("Setting device time by NTP server")
ntptime.settime()
time_local = utime.localtime()
RTC().datetime(time_local)
ntptime.settime()
# Sync device to ntp time
print("Setting up hotel rooms")
room_filter = {i: [0, 0, 0, 0] for i in range(9)}
rooms_occupied = {i:0 for i in range(9)}
def set_mux(address):
sens_address = '{:04b}'.format(address)
for i, port in enumerate(gpios_sens):
port.value(int(sens_address[i]))
def room_check(room_numb):
set_mux(room_numb)
time.sleep(0.1)
pot_value = 4063 - pot.read()
ref_value = 4063 - ref.read()
if pot_value < ref_value:
room_filter[room_numb].insert(0,1)
else:
room_filter[room_numb].insert(0,0)
room_filter[room_numb].pop()
filt_val = sum(room_filter[room_numb])
if filt_val >= 1:
rooms_occupied[room_numb] = 1
else:
rooms_occupied[room_numb] = 0
prev_weather = ""
while True:
print("Checking rooms...")
for room, guest in rooms_occupied.items():
room_check(room)
time.sleep(0.5)
measurements = {"rooms": rooms_occupied}
print("Measuring weather conditions... ", end="")
sensor.measure()
temp_hum = {"temp": sensor.temperature(), "humidity": sensor.humidity()}
measurements.update(temp_hum)
message = ujson.dumps(measurements)
if message != prev_weather:
time_stamp = utime.time() + EPOC_TIME
time_stampd = ujson.loads(message)
time_stampd['timestamp'] = time_stamp * 1000
message_timed = ujson.dumps(time_stampd)
print("Updated!")
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, message_timed))
client.publish(MQTT_TOPIC, message_timed)
prev_weather = message
else:
print("No change")
time.sleep(1)
Loading
cd74hc4067
cd74hc4067