"""
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Adaptive Water Heater: (Raspberry Pi pico,micropy) ┃
┃ ┃
┃ Develop a smart water heater system that automatically┃ ┃
┃ adjusts the heating temperature based on the ambient ┃
┃ temperature sensed by a DHT sensor and also aims to ┃
┃ improve energy efficiency. ┃
┃ ┃
┃ The DHT sensor will continuously monitor the ┃
┃ surrounding air temperature.Based on the sensed ┃
┃ temperature, the system will determine an appropriate ┃
┃ target heating temperature. For example, on a colder ┃
┃ day, the target temperature might be higher than on a ┃
┃ warmer day.A virtual button on the Blynk app will ┃
┃ allow the user to turn the water heater on or off ┃
┃ manually, or the user can even use the slide switch. ┃
┃ The OLED SSD1306 display will show the current heating ┃
┃ temperature of the water. ┃
┃ ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
"""
from machine import I2C, Pin
from time import sleep
from dht import DHT22
from ssd1306 import SSD1306_I2C
import BlynkLib
import network
import time
const = 0
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 = "RBm4KFhTPoQhUjUTX24UjtMpTG3G4tY8"
# 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:
@blynk.on("V3")
def v3_write_handler(value):
slider_value = int(value[0])
# Map slider value to target temperature based on your requirement
if slider_value == 1:
const = 1030
elif slider_value == 2:
const = 1080
elif slider_value == 3:
const = 1100
elif slider_value == 4:
const = 1250
else:
print("Invalid slider value")
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)*const
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
while True:
blynk.run()
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")