from machine import Pin, ADC
import utime
# Ultrasonic sensor
trigger = Pin(2, Pin.OUT)
echo = Pin(3, Pin.IN)
distance = 0 # Initialize distance as a global variable
# Simulate the turbidity sensor with a potentiometer
potentiometer_pin = 26 # Use Pin 26 for the ADC input
turbidity_adc = ADC(Pin(potentiometer_pin))
# Simulate the pH sensor with another potentiometer
ph_potentiometer_pin = 27 # Use Pin 27 for the ADC input of the simulated pH sensor
ph_adc = ADC(Pin(ph_potentiometer_pin))
# Simulate the water flow sensor with a push button
water_flow_pin = 14 # Use Pin 14 for the push button input
water_flow_sensor = Pin(water_flow_pin, Pin.IN, Pin.PULL_UP)
water_flow_count = 0
water_flow_pressed = False # Flag to track button press
# Function to perform filtering process
def perform_filtering_process():
# Simulated filtering process
print("Performing filtering process...")
# Wokwi simulation code
def send_data_to_webpage(distance, turbidity_value, simulated_ph_value):
print("Sending data to the web page...")
print(f"{{'distance': {distance}, 'turbidity': {turbidity_value}, 'pH': {simulated_ph_value}}}")
def ultra():
global distance # Declare distance as a global variable
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = utime.ticks_us()
while echo.value() == 1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
distance = (timepassed * 0.0343) / 2
while True:
ultra()
utime.sleep(1)
if 70 < distance < 90:
print("Water tank is going to be full")
if distance < 100 and not (70 < distance < 90):
print("Water tank is full")
break
# Simulate turbidity level with the potentiometer
turbidity_value = turbidity_adc.read_u16()
print("Turbidity Level:", turbidity_value)
# Simulate pH level with the potentiometer
simulated_ph_value = ph_adc.read_u16()
print("Simulated pH Level:", simulated_ph_value)
# Call the function to send data to the web page
sensorData=send_data_to_webpage(distance, turbidity_value, simulated_ph_value)
parent.postMessage(sensorData, '*');
utime.sleep(1)