from machine import Pin, ADC
import time
import math
# Khai báo chân cảm biến và LED
mq3 = ADC(Pin(34)) # GPIO34 - Cảm biến MQ3 (giả lập)
led_unripe = Pin(25, Pin.OUT) # LED chưa chín (Xanh)
led_ripe = Pin(26, Pin.OUT) # LED chín (Vàng)
led_overripe = Pin(27, Pin.OUT) # LED quá chín (Đỏ)
# Thiết lập thang đo cho MQ3 (giả lập)
mq3.atten(ADC.ATTN_11DB) # Điện áp đọc 0-3.3V
mq3.width(ADC.WIDTH_12BIT) # cấu hình độ phân giải
def map_value(value, in_min, in_max, out_min, out_max):
return (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
def get_alcohol_level():
raw_value = mq3.read()
voltage = raw_value * 3.3 / 4095
# Giả lập nồng độ alcohol (0-100%)
alcohol_level = map_value(voltage, 0, 3.3, 0, 100)
return alcohol_level
# Hiển thị gauge trên Wokwi (dùng print với format đặc biệt)
def show_gauge(value):
print("GAUGE:{:.1f}".format(value))
while True:
alcohol = get_alcohol_level()
# Điều khiển LED
led_unripe.value(0)
led_ripe.value(0)
led_overripe.value(0)
if alcohol < 30: # Chưa chín
led_unripe.value(1)
status = "UNRIPE"
elif 30 <= alcohol < 70: # Chín
led_ripe.value(1)
status = "RIPE"
else: # Quá chín
led_overripe.value(1)
status = "OVERRIPE"
# Hiển thị gauge và trạng thái
show_gauge(alcohol)
print(f"Status: {status} | Alcohol: {alcohol:.1f}%")
time.sleep(1)
print("Hello, ESP32!")