from umqtt.simple import MQTTClient
import neopixel
import machine
import network, time, random
import dht
from machine import Pin, PWM, I2C
import ssd1306
from neopixel import NeoPixel
wlan = network.WLAN(network.STA_IF); wlan.active(True)
wlan.connect("Wokwi-GUEST","")
while not wlan.isconnected():
time.sleep(0.2)
print("WIFI UP")
USER= b"Gabe_itch"
cid = ("ic-"+str(random.getrandbits(24))).encode()
client = MQTTClient(cid, "io.adafruit.com", user = USER, password = b"aio_wfjw4928xNhhSB2EDNqmVzJ1pWzd")
client.connect()
sensor = dht.DHT22(Pin(25))
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
oled = ssd1306.SSD1306_I2C(128,64,i2c)
thing = NeoPixel(Pin(15, Pin.OUT),16)
def read_sensor():
try:
sensor.measure()
return sensor.temperature(), sensor.humidity()
except Exception as error:
print("Sensor error: ",error)
return None, None
def update_display(temp,hum):
oled.fill(0)
if temp > 22:
stat = "Hot"
elif temp < 15:
stat = "Cold"
else:
stat= "neutral"
oled.text("Temp: "+str(int(temp)) + "C",0,0)
oled.text("Hum: "+ str(hum)+"%",0,16)
oled.text("Status: "+ stat, 0, 32)
oled.show()
last_read = time.ticks_ms()
while True:
now = time.ticks_ms()
if time.ticks_diff(now,last_read) > 2000:
temp, hum = read_sensor()
if temp is not None:
update_display(temp,hum)
client.publish(USER + b"/feeds/temp", str(temp).encode())
else:
temp = 0
tempnoneg = int(temp) +40
r = int(tempnoneg*3.4)
if r > 255:
r = 255
b = -r
for i in range(16):
thing[i] = (r,b,b)
thing.write()
time.sleep_ms(50)Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ssd1306
ssd1306