5
5
import dht
6
6
import umqtt .simple as mqtt
7
7
8
+ #Funcion vinculada al evento de nuevo mensaje en topico suscrito
8
9
def callBack (topic , msg ):
9
10
fan = machine .Pin (12 , machine .Pin .OUT , machine .Pin .PULL_UP )
10
11
dat = json .dumps (msg ).split (':' )[- 1 ].split ('"' )[1 ]
12
+ print (dat )
11
13
if str (dat ) == "OFF" :
12
14
fan .value (1 )
13
15
elif str (dat ) == "ON" :
@@ -16,50 +18,50 @@ def callBack(topic, msg):
16
18
fan .value (1 )
17
19
18
20
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 "
23
25
serverPort = 1883
24
26
clientName = 'Bug_01'
25
27
pubTopic = clientName + '/Report'
26
28
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 ))
27
32
28
33
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 ():
34
39
pass
35
40
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 ()
39
44
40
- mqtt_client .set_callback (callBack )
41
- mqtt_client .subscribe (subTopic )
45
+ mqttClient .set_callback (callBack )
46
+ mqttClient .subscribe (subTopic )
42
47
43
- sensor = dht .DHT11 (machine .Pin (14 , machine .Pin .IN , machine .Pin .PULL_UP ))
44
- sensor .
45
48
49
+ #Programa principal
46
50
while True :
51
+ #Revisa por mensajes pendientes
52
+ mqttClient .check_msg ()
47
53
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 ()
64
57
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