# Sample project to save water
# Using Pico W to enhance this project to be operated over WiFi/Bluetooth
#
# System modules
import network
from machine import I2C, Pin, ADC
from time import sleep
# I2C library for LCD by
# Programmer: Adrian Josele G. Quional
# Wokwi project link: https://wokwi.com/projects/376470497467180033
# very important
# this module needs to be saved in the Raspberry Pi Pico in order for the LCD I2C to be used
from pico_i2c_lcd import I2cLcd
# Library for HC-SR04 ultrasonic sensor
# for more information visit: https://microcontrollerslab.com/hc-sr04-ultrasonic-sensor-raspberry-pi-pico-micropython-tutorial/
# Save this module to Pi Pico to this code to run.
# noticed an error of 1cm for for 100cms of range in the simulation.
from hcsr04 import HCSR04
sleep(0.1) # Wait for USB to become ready
# Connect to WiFi
SSID = "Wokwi-GUEST"
PASSWORD = ""
wlan = ""
ip = ""
# creating an I2C object, specifying the data (SDA) and clock (SCL) pins used in the Raspberry Pi Pico
# any SDA and SCL pins in the Raspberry Pi Pico can be used (check documentation for SDA and SCL pins)
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
# getting I2C address
I2C_ADDR = i2c.scan()[0]
print(I2C_ADDR)
# creating an LCD object using the I2C address and specifying number of rows and columns in the LCD
# LCD number of rows = 4, number of columns = 20
lcd = I2cLcd(i2c, I2C_ADDR, 4, 20)
# Create ultrasonic sensor objects
borewell = HCSR04(trigger_pin=2, echo_pin=3, echo_timeout_us=30000)
muncipal = HCSR04(trigger_pin=4, echo_pin=5, echo_timeout_us=30000)
sump = HCSR04(trigger_pin=6, echo_pin=7, echo_timeout_us=30000)
# Monitor the board temperature. Can get hot in closed spaces
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)
# For Motion activated backlight display
pir = machine.Pin(8, mode=Pin.IN, pull=Pin.PULL_DOWN);
# Menu control
show_menu = False
# Joystick for menu control
xAxis = ADC(Pin(26))
yAxis = ADC(Pin(27))
button = Pin(22,Pin.IN, Pin.PULL_UP)
# Methods to perform some tasks
def print_bar(value):
if value > 10:
value=10
if value < 0:
value=0
for bar in range(0,value):
lcd.putstr(chr(255))
def write_data(ohSalt, ohFresh, ugFresh, pumpStatus, temperature):
lcd.clear()
lcd.move_to(0,0)
lcd.putstr("Borewell: ")
print_bar(ohSalt)
lcd.move_to(0,1)
lcd.putstr("Muncipal: ")
print_bar(ohFresh)
lcd.move_to(0,2)
lcd.putstr("Sump : ")
print_bar(ugFresh)
lcd.move_to(0,3)
lcd.putstr("Motor: "+pumpStatus)
lcd.putstr(" "+temperature+chr(223)+"C")
def get_temperature():
reading = sensor_temp.read_u16() * conversion_factor
temperature = 27 - (reading - 0.706)/0.001721
return round(temperature, 1) # not working in wokwi
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
while wlan.isconnected() == False:
print('Waiting for connection...')
sleep(1)
print("Connected to WiFi!!!")
return wlan.ifconfig()[0]
def display_water_levels():
distance1 = borewell.distance_cm()
print("Borewell Tank level: ", distance1)
distance2 = muncipal.distance_cm()
print("Muncipal Tank level: ", distance2)
distance3 = sump.distance_cm()
print("Sump Tank level: ", distance3)
# Activate backlight if motion is detected
print("Print Motion detected: ",pir.value())
if(pir.value()==1):
lcd.backlight_on()
else:
lcd.backlight_off()
# Show the data on LCD display
write_data(distance1/40, distance2/40, distance3/40,'Off', str(get_temperature()))
def display_settings():
while show_menu:
xValue = xAxis.read_u16()
yValue = yAxis.read_u16()
buttonValue = button.value()
buttonStatus = "not pressed"
if buttonValue == 0:
buttonStatus = "pressed"
print("X: " + str(xValue) + ", Y: " + str(yValue) + " -- button value: " + str(buttonValue) + " button status: " + buttonStatus)
sleep(0.2)
# Get the IP address - Not working in wokwi
try:
ip = connect_wifi()
print(f'Connected on {ip}')
except:
print("Error occured while detching IP address")
# Main logic to perform the function
while True:
# putstr method allows printing of the text in the LCD screen
# for other methods that can be used, check lcd_api module
#lcd.backlight_off()
#write_data(6,5,4,'Off')
xValue = xAxis.read_u16()
yValue = yAxis.read_u16()
buttonValue = button.value()
buttonStatus = "not pressed"
if buttonValue == 0:
buttonStatus = "pressed"
print("X: " + str(xValue) + ", Y: " + str(yValue) + " -- button value: " + str(buttonValue) + " button status: " + buttonStatus)
sleep(0.2)
if(show_menu):
display_settings()
else:
display_water_levels()
sleep(5)
Loading
pi-pico-w
pi-pico-w