from machine import Pin, ADC, time_pulse_us
from time import sleep, sleep_us
from picozero import Servo
import dht
import network
import socket
sensor = dht.DHT22(Pin(0))
adc_tem = ADC(Pin(26))
adc_hum = ADC(Pin(27))
trigger = Pin(1, Pin.OUT)
echo = Pin(2, Pin.IN)
servo = Servo(3)
green_led = Pin(4,Pin.OUT)
red_led = Pin(14,Pin.OUT)
button_g = Pin(12,Pin.IN)
button_r = Pin(13,Pin.IN)
ssid = 'Alvin Boon Wifi'
password = 'JJjj11!!'
pins_G = [
Pin(5, Pin.OUT), # A
Pin(6, Pin.OUT), # B
Pin(7, Pin.OUT), # C
Pin(8, Pin.OUT), # D
Pin(9, Pin.OUT), # E
Pin(10, Pin.OUT), # F
Pin(11, Pin.OUT), # G
]
pins_R = [
Pin(16, Pin.OUT), # A
Pin(17, Pin.OUT), # B
Pin(18, Pin.OUT), # C
Pin(19, Pin.OUT), # D
Pin(20, Pin.OUT), # E
Pin(21, Pin.OUT), # F
Pin(22, Pin.OUT), # G
]
digits = [
[1, 0, 0, 1, 1, 1, 1], # 1
[0, 0, 1, 0, 0, 1, 0], # 2
[0, 0, 0, 0, 1, 1, 0], # 3
[1, 0, 0, 1, 1, 0, 0], # 4
[0, 1, 0, 0, 1, 0, 0], # 5
[0, 1, 0, 0, 0, 0, 0], # 6
[0, 0, 0, 1, 1, 1, 1], # 7
[0, 0, 0, 0, 0, 0, 0], # 8
[0, 0, 0, 1, 1, 0, 0], # 9
]
def reset():
"""Turns off all segments on the 7-segment display."""
for pin in pins_G:
pin.value(1)
for pin in pins_R:
pin.value(1)
def environment_status():
sensor.measure()
analog_tem = adc_tem.read_u16()
analog_hum = adc_hum.read_u16()
addition_temp = analog_tem * (50 / 65535)
addition_humid = analog_hum * (100 / 65535)
temp = sensor.temperature() + addition_temp
hum = sensor.humidity() + addition_humid
print("Environment Status:")
print("Humidity: %3.1f %%" %hum)
print("Temperature: %3.1f C\n" %temp)
def measure_distance():
# Ensure trigger is low initially
trigger.low()
sleep_us(2)
# Send a 10 microsecond pulse to the trigger pin
trigger.high()
sleep_us(10)
trigger.low()
# Measure the duration of the echo pulse (in microseconds)
pulse_duration = time_pulse_us(echo, Pin.high)
# Calculate the distance (in centimeters) using the speed of sound (343 m/s)
distance = pulse_duration * 0.0343 / 2
print("Load Status:")
print("Infill Distance: {:.2f} cm".format(distance))
if distance <= 10:
print("Status: Filled\n")
return ("1")
elif distance > 10:
print("Status: Not Filled\n")
return ("0")
def servo_movement(status):
if status == "0":
servo.min()
elif status == "1":
servo.max()
#Wifi
def connect():
# Connect to WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Wokwi-GUEST", "")
while wlan.isconnected() == False:
print('Waiting for connection...')
sleep(1)
ip = wlan.ifconfig()[0]
print(f'Connected on {ip}')
return ip
def open_socket(ip):
# Open a socket
address = (ip, 80)
connection = socket.socket()
connection.bind(address)
connection.listen(1)
return connection
def webpage(pass_status,fail_status):
html = f"""
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="1">
<style>
p {{
font-size: 20px;
margin-top: 20px;
text-align: center;
}}
h1 {{
font-size: 30px;
margin-top: 20px;
text-align: center;
}}
form {{
text-align: center;
margin-bottom: 20px;
}}
input[type="submit"] {{
background-color: #4CAF50;
border: solid;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
transition-duration: 0.4s;
cursor: pointer;
}}
input[type="submit"]:hover {{
background-color: #45a051;
}}
img {{
width: 400px; /* Set the width to 200 pixels */
height: auto; /* Maintain aspect ratio */
display: block;
margin: 0 auto;
max-width: 100%; /* Set maximum width relative to its container */
}}
</style>
</head>
<body>
<img src="https://cegeogtech.unimap.edu.my/wp-content/uploads/2020/08/logo-unimap.png" alt="UNIMAP Logo">
<h1>EMJ 47703 IoT Mini Project</h1>
<p>Pass Inventory Status: {pass_status}</p>
<form action="/collectpass">
<input type="submit" value="Collect Passed Inventory" />
</form>
<p>Fail Inventory Status: {fail_status}</p>
<form action="/collectfail">
<input type="submit" value="Collect Failed Inventory" />
</form>
</body>
</html>
"""
return str(html)
def serve(connection,pass_status,fail_status):
"""
client = connection.accept()[0]
request = client.recv(1024)
request = str(request)
try:
request = request.split()[1]
except IndexError:
pass
if request == '/collectpass?':
state = 'ON'
elif request == '/collectfail?':
state = 'OFF'
"""
html = webpage(pass_status,fail_status)
client.send(html)
client.close()
if __name__ == "__main__":
ip = connect()
connection = open_socket(ip)
reset()
passed_count = 0
failed_count = 0
pass_status = "NOT FULL"
fail_status = "NOT FULL"
print(f"[{passed_count}]")
environment_status()
status = measure_distance()
servo_movement(status)
while True:
if status == "0":
passed_count += 1
red_led.on()
green_led.off()
if passed_count == 10:
pass_status = "FULL"
serve(connection,pass_status,fail_status)
while button_r.value() == 0:
red_led.toggle()
sleep(0.4)
pass
if passed_count > 9:
passed_count = 1
for i in range(len(pins_R)):
pins_R[i].value(digits[passed_count - 1][i])
print(f"[{passed_count}]")
environment_status()
status = measure_distance()
servo_movement(status)
sleep(1)
if status == "1":
failed_count += 1
green_led.on()
red_led.off()
if failed_count == 10:
pass_status = "FULL"
serve(connection,pass_status,fail_status)
while button_g.value() == 0:
green_led.toggle()
sleep(0.4)
pass
if failed_count > 9:
failed_count = 1
for i in range(len(pins_G)):
pins_G[i].value(digits[failed_count - 1][i])
print(f"[{failed_count}]")
environment_status()
status = measure_distance()
servo_movement(status)
sleep(1)
Loading
pi-pico-w
pi-pico-w