#!/usr/bin/env python
import time
import sys
#import RPi.GPIO as GPIO
#from hx711 import HX711
import machine
import utime
from machine import Pin, ADC, PWM, I2C
#from hx711 import HX711 # Biblioteca necessária
from pico_i2c_lcd import I2cLcd # Biblioteca necessária
# --- CONFIGURAÇÃO DE PINOS (GPIO) ---
MQ2_PIN = ADC(26) # Equivalente ao A0
BUZZER = PWM(Pin(13)) # Pino do Buzzer
RED = PWM(Pin(10))
GREEN = PWM(Pin(11))
BLUE = PWM(Pin(12))
# Configuração da Balança (DT, SCK)
#hx = HX711(d_out=14, sck=15)
#hx.set_scale(420.0)
# Configuração LCD I2C
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=100000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# Variáveis Globais
limit_yellow = 15000 # No Pico ADC vai de 0 a 65535
limit_red = 30000
fator_calibracao = 420.0
def set_color(r, g, b):
# PWM no Pico vai de 0 a 65535
RED.duty_u16(r * 257)
GREEN.duty_u16(g * 257)
BLUE.duty_u16(b * 257)
def beep(freq, duration):
if freq > 0:
BUZZER.freq(freq)
BUZZER.duty_u16(32768) # 50% volume
utime.sleep_ms(duration)
BUZZER.duty_u16(0)
# --- SETUP INICIAL ---
print("Sistema de Monitoramento Iniciado...")
lcd.putstr("Iniciando...")
utime.sleep(1)
#hx.tare()
lcd.clear()
lcd.putstr("Prato Zerado!")
utime.sleep(1)
lcd.clear()
# --- LOOP PRINCIPAL ---
while True:
# Leitura da Balança
#leitura = hx.get_units(5)
#if -0.5 < leitura < 0.5:
leitura = 0
# Leitura do Gás MQ2
gas_level = MQ2_PIN.read_u16()
# Atualiza LCD
lcd.move_to(0, 0)
lcd.putstr(f"Peso: {leitura:.1f}g ")
lcd.move_to(0, 1)
lcd.putstr(f"Gas: {gas_level} ")
# Lógica de Segurança e Alarme
if gas_level < limit_yellow and leitura >= 5:
# TUDO SEGURO - Verde
set_color(0, 255, 0)
BUZZER.duty_u16(0)
elif leitura < 5 or (limit_yellow <= gas_level < limit_red):
# ATENÇÃO - Amarelo
set_color(255, 150, 0)
beep(500, 100)
set_color(0, 0, 0)
else:
# PERIGO - Vermelho
set_color(255, 0, 0)
beep(2000, 100)
set_color(0, 0, 0)
utime.sleep_ms(50)
utime.sleep_ms(200)