from machine import Pin, SoftI2C, PWM #lib that provides access to hardware
functionality
import urequests #lib for make HTTP request
import network #lib for managing network connections
from lcd_api import LcdApi #lib for LCD
from i2c_lcd import I2cLcd #lib for I2C
import time #lib for time
import hcsr04 #lib for ultrasonic sensor
servo_pin = Pin(15, Pin.OUT) #defining the pin for servo motor
servo_pwm = PWM(servo_pin, freq=50)
servo_min_angle = 0 #min angle for servo motor
servo_max_angle = 180 #max angle for servo motor
servo_angle_range = servo_max_angle - servo_min_angle
def set_servo_angle(angle): #funtion for servo motor's angle
duty_cycle = int(50 + ((angle - servo_min_angle) / servo_angle_range) * 65)
servo_pwm.duty(duty_cycle)
I2C_ADDR = 0x3f #address of the i2c LCD is 0x3f
totalRows = 2
totalColumns = 16
green=Pin(25, Pin.OUT) #defining the pin for green LED
red=Pin(26, Pin.OUT) #defining the pin for red LED
ssid = "OnePlus Nord" #network id
password = "nothingxyz" #network password
HTTP_HEADERS = {'Content-Type': 'application/json'}
THINGSPEAK_WRITE_API_KEY = 'W4Q48LINDK2O73SI' #write api key for connecting with
thingspeak
UPDATE_TIME_INTERVAL = 3000 #time for updating the data on the server in ms
last_update = time.ticks_ms()
sta = network.WLAN(network.STA_IF)
sta.active(True)
if not sta.isconnected():
print('connecting to network...')
sta.active(True)
sta.connect(ssid, password) #connecting the ESP32 wifi module with the
hotspot
while not sta.isconnected():
pass
print('network config:', sta.ifconfig()) #printing the ip configuration
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) #defining sda and scl pin for
i2c LCD
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
ultraSonic = hcsr04.HCSR04(trigger_pin=13, echo_pin=12) #defining the trig and
echo pin for ultrasonic sensor
lcd.putstr("Water level:") #printing text on the LCD
while True:
distance = ultraSonic.distance_cm() #reading the distance and storing it in a
variable
print(distance) #print distance for debugging purpose
if distance < 5 and distance > 1: #creating conditions for switching on/off
the water pump and printing the water level on the LCD, here the water pump is
off and red LED is on
x=0
lcd.move_to(0,1)
lcd.putstr("################")
green.value(0)
red.value(1)
set_servo_angle(0)
elif distance < 6 and distance > 5:
lcd.move_to(0,1)
lcd.putstr("############### ")
elif distance < 7 and distance > 6:
lcd.move_to(0,1)
lcd.putstr("############## ")
elif distance < 8 and distance > 7:
lcd.move_to(0,1)
lcd.putstr("############# ")
elif distance < 9 and distance > 8:
lcd.move_to(0,1)
lcd.putstr("############ ")
elif distance < 10 and distance > 9:
lcd.move_to(0,1)
lcd.putstr("########### ")
elif distance < 11 and distance > 10:
lcd.move_to(0,1)
lcd.putstr("########## ")
elif distance < 12 and distance > 11:
lcd.move_to(0,1)
lcd.putstr("######### ")
elif distance < 13 and distance > 12:
lcd.move_to(0,1)
lcd.putstr("######## ")
elif distance < 14 and distance > 13:
lcd.move_to(0,1)
lcd.putstr("####### ")
elif distance < 15 and distance > 14:
lcd.move_to(0,1)
lcd.putstr("###### ")
elif distance < 16 and distance > 15:
lcd.move_to(0,1)
lcd.putstr("##### ")
elif distance < 17 and distance > 16: #here water pump is on so green LED is
onn and red LED is off
x=1
lcd.move_to(0,1)
lcd.putstr("#### ")
green.value(1)
red.value(0)
set_servo_angle(150)
elif distance < 18 and distance > 17:
x=1
lcd.move_to(0,1)
lcd.putstr("### ")
green.value(1)
red.value(0)
set_servo_angle(150)
elif distance < 19 and distance > 18:
x=1
lcd.move_to(0,1)
lcd.putstr("## ")
green.value(1)
red.value(0)
set_servo_angle(150)
else:
x=1
lcd.move_to(0,1)
lcd.putstr("# ")
green.value(1)
red.value(0)
set_servo_angle(150)
if time.ticks_ms()-last_update>=UPDATE_TIME_INTERVAL: #condition for updating
the the data of field 1 on the server
data=20.8-distance
level=(data/20.8)*100 #converting the water level into percentage
WaterLevel = {'field1':level}
request =
urequests.post('http://api.thingspeak.com/update?api_key='+THINGSPEAK_WRITE_API_K
EY,json = WaterLevel,headers = HTTP_HEADERS) #sending the water level to the
server
request.close()
print(WaterLevel)
if time.ticks_ms()-last_update>=UPDATE_TIME_INTERVAL: #condition for updating
the the data of field 2 on the server
WaterPump = {'field2':x}
request =
urequests.post('http://api.thingspeak.com/update?api_key='+THINGSPEAK_WRITE_API_K
EY,json = WaterPump,headers = HTTP_HEADERS) #sending the state of water pump to
the server
request.close()
print(WaterPump)