import machine
import time
import network
from umqtt.simple import MQTTClient
ledR = machine.Pin(25,machine.Pin.OUT)
ledG = machine.Pin(26,machine.Pin.OUT)
ledV = machine.Pin(27,machine.Pin.OUT)
adc = machine.ADC(machine.Pin(32),atten=machine.ADC.ATTN_11DB)
SSID = 'dd-wrt'
PASSWD = '123456789'
sta_if = network.WLAN(network.STA_IF)
print('connecting to network...')
sta_if.active(True)
sta_if.connect(SSID, PASSWD)
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
SERVER = '172.22.5.222' # Modificare con indirizzo Broker
CLIENT_ID = 'ESP32' # Nome dispositivo
TOPIC = 'POT' # Topic
client = MQTTClient(CLIENT_ID,SERVER,1883,keepalive=60)
client.connect()
msg_prec = ""
while True:
pot = adc.read()
if pot<4096/3:
msg = "MIN"
ledV.on()
ledG.off()
ledR.off()
elif pot<4096*2/3:
msg = "MED"
ledV.off()
ledG.on()
ledR.off()
else:
msg = "MAX"
ledV.off()
ledG.off()
ledR.on()
if msg != msg_prec:
client.publish(TOPIC,msg)
msg_prec = msg
print(msg+" POT="+str(pot))
#time.sleep(1)