# Importamos los módulos
from machine import Pin, ADC
from umqtt.simple import MQTTClient
import ujson
import network
import utime as time
# Device Setup
DEVICE_ID = "wokwiPG"
# WiFi Setup
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASSWORD = ""
# MQTT Setup
MQTT_BROKER = "broker.mqttdashboard.com"
MQTT_CLIENT = DEVICE_ID
MQTT_TELEMETRY_TOPIC = "iot/telemetry"
MQTT_CONTROL_TOPIC = "iot/control"
# Creando objetos
led_Verde = Pin (27, Pin.OUT)
led_Amarillo = Pin (26, Pin.OUT)
RED_LED = Pin(12, Pin.OUT)
BLUE_LED = Pin(13, Pin.OUT)
FLASH_LED = Pin(2, Pin.OUT)
SDS_PIN = ADC(Pin(32))
RED_LED.on()
BLUE_LED.on()
#Ajustar resolución de trabajo
SDS_PIN.width(ADC.WIDTH_10BIT)
SDS_PIN.atten(ADC.ATTN_11DB)
while True:
lectura = int(SDS_PIN.read())
print(lectura)
utime.sleep_ms(0.5)
ppm= 1200 / 1023
pm2_5 = ppm / lectura
print("PM2.5 :", pm2_5, " ug/m3")
utime.sleep_ms(1000)
if pm2_5 >= 100:
led_Verde.value(1)
print("Llegando al límite máximo permitido")
led_Amarillo.value(0)
else:
led_Verde.value(0)
led_Amarillo.value(1)
print("Estado normal")
# Metodos wifi
def did_recieve_callback(topic, message):
print('\n\nData Recieved! \ntopic = {0}, message = {1}'.format(topic, message))
# device_id/lamp/color/state
# device_id/lamp/state
# lamp/state
if topic == MQTT_CONTROL_TOPIC.encode():
if message == ('{0}/lamp/red/on'.format(DEVICE_ID)).encode():
RED_LED.on()
elif message == ('{0}/lamp/red/off'.format(DEVICE_ID)).encode():
RED_LED.off()
elif message == ('{0}/lamp/blue/on'.format(DEVICE_ID)).encode():
BLUE_LED.on()
elif message == ('{0}/lamp/blue/off'.format(DEVICE_ID)).encode():
BLUE_LED.off()
elif message == ('{0}/lamp/on'.format(DEVICE_ID)).encode() or message == b'lamp/on':
RED_LED.on()
BLUE_LED.on()
elif message == ('{0}/lamp/off'.format(DEVICE_ID)).encode() or message == b'lamp/off':
RED_LED.off()
BLUE_LED.off()
elif message == ('{0}/status'.format(DEVICE_ID)).encode() or message == ('status').encode():
global telemetry_data_old
mqtt_client_publish(MQTT_TELEMETRY_TOPIC, telemetry_data_old)
else:
return
send_led_status()
def mqtt_connect():
print("Connecting to MQTT broker ...", end="")
mqtt_client = MQTTClient(MQTT_CLIENT, MQTT_BROKER, user="", password="")
mqtt_client.set_callback(did_recieve_callback)
mqtt_client.connect()
print("Connected.")
mqtt_client.subscribe(MQTT_CONTROL_TOPIC)
return mqtt_client
def create_json_data(particulas):
data = ujson.dumps({
"device_id": DEVICE_ID,
"pm_2.5": particulas,
"type": "sensor"
})
return data
def mqtt_client_publish(topic, data):
print("\nUpdating MQTT Broker...")
mqtt_client.publish(topic, data)
print(data)
def send_led_status():
data = ujson.dumps({
"device_id": DEVICE_ID,
"red_led": "ON" if RED_LED.value() == 1 else "OFF",
"blue_led": "ON" if BLUE_LED.value() == 1 else "OFF",
"type": "lamp"
})
mqtt_client_publish(MQTT_TELEMETRY_TOPIC, data)
# Logica
# Conectando WiFi
wifi_client = network.WLAN(network.STA_IF)
wifi_client.active(True)
print("Connecting device to WiFi")
wifi_client.connect(WIFI_SSID, WIFI_PASSWORD)
# Reconección
while not wifi_client.isconnected():
print("Connecting")
time.sleep(0.1)
print("WiFi Connected!")
print(wifi_client.ifconfig())
# Conectar a MQTT
mqtt_client = mqtt_connect()
RED_LED.off()
BLUE_LED.off()
mqtt_client_publish(MQTT_CONTROL_TOPIC, 'lamp/off')
dss_sensor = ADC(SDS_PIN)
telemetry_data_old = ""
while True:
mqtt_client.check_msg()
print(". ", end="")
FLASH_LED.on()
try:
dht_sensor.measure()
except:
pass
time.sleep(0.2)
FLASH_LED.off()
telemetry_data_new = create_json_data(dss_sensor.particulas())
if telemetry_data_new != telemetry_data_old:
mqtt_client_publish(MQTT_TELEMETRY_TOPIC, telemetry_data_new)
telemetry_data_old = telemetry_data_new
time.sleep(0.1)