Skip to content

Commit 3883099

Browse files
authored
Add files via upload
update, changed waiting logic for tick counting logic
1 parent 1fe7a32 commit 3883099

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

main.py

+35-33
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import dht
66
import umqtt.simple as mqtt
77

8+
#Funcion vinculada al evento de nuevo mensaje en topico suscrito
89
def callBack(topic, msg):
910
fan = machine.Pin(12, machine.Pin.OUT, machine.Pin.PULL_UP)
1011
dat = json.dumps(msg).split(':')[-1].split('"')[1]
12+
print(dat)
1113
if str(dat) == "OFF":
1214
fan.value(1)
1315
elif str(dat) == "ON":
@@ -16,50 +18,50 @@ def callBack(topic, msg):
1618
fan.value(1)
1719

1820

19-
# Replace these with your own Wi-Fi and MQTT broker settings
20-
SSID = 'Totalplay-EEA7'
21-
password = 'EEA7506Es9Jd3Tbj'
22-
serverAddr = "3.133.119.117"
21+
#Parametros y etiquetas
22+
SSID = 'SSID'
23+
password = 'PASSWORD'
24+
serverAddr = "192.168.10.102"
2325
serverPort = 1883
2426
clientName = 'Bug_01'
2527
pubTopic = clientName + '/Report'
2628
subTopic = clientName + '/Execute'
29+
lastPub = time.ticks_ms()
30+
pubInterval = 4000
31+
sensor = dht.DHT11(machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP))
2732

2833

29-
# Initialize Wi-Fi
30-
sta_if = network.WLAN(network.STA_IF)
31-
sta_if.active(True)
32-
sta_if.connect(SSID, password)
33-
while not sta_if.isconnected():
34+
#Inicializacion de WiFi
35+
sta = network.WLAN(network.STA_IF)
36+
sta.active(True)
37+
sta.connect(SSID, password)
38+
while not sta.isconnected():
3439
pass
3540

36-
# Initialize MQTT client
37-
mqtt_client = mqtt.MQTTClient(clientName, serverAddr, serverPort)
38-
mqtt_client.connect()
41+
#Inicializacion del cliente MQTT
42+
mqttClient = mqtt.MQTTClient(clientName, serverAddr, serverPort)
43+
mqttClient.connect()
3944

40-
mqtt_client.set_callback(callBack)
41-
mqtt_client.subscribe(subTopic)
45+
mqttClient.set_callback(callBack)
46+
mqttClient.subscribe(subTopic)
4247

43-
sensor = dht.DHT11(machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP))
44-
sensor.
4548

49+
#Programa principal
4650
while True:
51+
#Revisa por mensajes pendientes
52+
mqttClient.check_msg()
4753

48-
try:
49-
sensor.measure()
50-
51-
except:
52-
pass
53-
54-
mqtt_client.check_msg()
55-
56-
57-
# Create a JSON payload
58-
payload = {
59-
'temp': sensor.temperature(),
60-
'humd': sensor.humidity(),
61-
}
62-
# Publish the JSON payload to MQTT
63-
mqtt_client.publish(pubTopic, json.dumps(payload))
54+
#Evalua si ha transcurrido el sampling time, en caso de haberlo hecho, actualiza el tiempo de ultima publicacion
55+
if time.ticks_ms() - lastPub >= pubInterval:
56+
lastPub = time.ticks_ms()
6457

65-
time.sleep(4)
58+
#Solicita una medicion al DHT11, estructura la informacion en formato json y la publica al topico
59+
try:
60+
sensor.measure()
61+
payload = {
62+
'temp': sensor.temperature(),
63+
'humd': sensor.humidity(),
64+
}
65+
mqttClient.publish(pubTopic, json.dumps(payload))
66+
except:
67+
pass

0 commit comments

Comments
 (0)