from machine import I2C, Pin
from time import sleep
from dht import DHT22
from ssd1306 import SSD1306_I2C
import BlynkLib
import network
import time
pix_res_x = 128
pix_res_y = 64
switch_pin = machine.Pin(10, machine.Pin.IN, machine.Pin.PULL_UP)
def init_i2c(scl_pin, sda_pin):
# Initialize I2C device
i2c_dev = I2C(1, scl=Pin(scl_pin), sda=Pin(sda_pin), freq=200000)
i2c_addr = [hex(ii) for ii in i2c_dev.scan()]
if not i2c_addr:
print('No I2C Display Found')
sys.exit()
else:
print("I2C Address : {}".format(i2c_addr[0]))
print("I2C Configuration: {}".format(i2c_dev))
return i2c_dev
def display_text(oled, text1, text2=''):
oled.fill(0)
oled.text(text1, 5, 5)
oled.text(text2, 5, 15)
oled.show()
# connect the network
print("Connecting to WiFi", end="")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Wokwi-GUEST", "")
while not wlan.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
print(wlan.ifconfig())
wait = 10
while wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
wait -= 1
print('waiting for connection...')
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else:
print('connected')
ip=wlan.ifconfig()[0]
print('IP: ', ip)
BLYNK_AUTH = "iaFNx8IwN0bhzlqfQQz40e1eolP-jJRK"
"Connection to Blynk"
# Initialize blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)
# Register virtual pin handler
@blynk.on("V0") #virtual pin V0
def v0_write_handler(value): #read the value
if int(value[0]) == 1:
while True:
for i in range(3):
dht.measure()
temp.append(dht.temperature())
hum = dht.humidity()
print("Temperature: {}°C".format(temp))
sleep(2)
room = sum(temp)/len(temp)
display_text(oled, f"Temp : {room}C")
break
sleep(2)
required = (1/room)*1080
display_text(oled, "Heating to", str(int(required))+"C")
dht = DHT22(Pin(15))
i2c_dev = init_i2c(scl_pin=27, sda_pin=26)
oled = SSD1306_I2C(pix_res_x, pix_res_y, i2c_dev)
temp = []
room = 0
blynk.run()
while True:
if switch_pin.value():
for i in range(3):
dht.measure()
temp.append(dht.temperature())
hum = dht.humidity()
print("Temperature: {}°C".format(temp))
sleep(2)
room = sum(temp)/len(temp)
display_text(oled, f"Temp : {room}C")
break
sleep(2)
required = (1/room)*1080
display_text(oled, "Heating to", str(int(required))+"C")