Repository navigation
Expand file tree
/
Copy pathgladosMQTT.py
More file actions
147 lines (111 loc) · 3.9 KB
/
Copy pathgladosMQTT.py
File metadata and controls
147 lines (111 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
import platform
import time
import os
import json
#Carga inicial de variables, aunque el valor de estas se reescribira al ejecutar initMQTT
mqttServer = "192.168.1.1"
mqttPort = 1883
nodeName = "test-node"
baseTopic = "node/test-node"
debugTopic = baseTopic+"debug"
mqttClient = mqtt.Client()
def dummy() :
debug(dummy)
nodeConnectedCallback = dummy
nodeMsgCallback = dummy
nodeDisconnectedCallback = dummy
def subscribe(topic) :
mqttClient.subscribe(topic)
def publish(topic,msg,persist = False) :
mqttClient.publish(topic,msg,persist)
def debug(msg):
# Convertir el mensaje a una cadena, si es necesario
if not isinstance(msg, str):
try:
# Intenta convertir el mensaje a una cadena JSON si es un objeto complejo
msg = json.dumps(msg)
except (TypeError, ValueError):
# Si la conversión falla, usa una representación de string genérica
msg = str(msg)
print(msg)
publish(debugTopic, msg)
def on_connect(client, userdata, rc,arg):
global mqttServer
global mqttPort
global nodeName
global baseTopic
global debugTopic
debug("[GladosNode] Connected with result code "+str(rc))
debug("[GladosNode] Node name : " + nodeName)
debug("[GladosNode] Base topic : " + baseTopic)
debug("[GladosNode] Debug topic : " + debugTopic)
debug("[GladosNode] mosquitto_sub -h " + mqttServer + " -t " + debugTopic)
publish("node/hello", "Hello! Im "+ nodeName)
nodeConnectedCallback(client, userdata, rc,arg)
# The callback for when a PUBLISH message is received from the server.
# Aqui recibimos los mensajes, y si no hay que hacer nada con ellos se lo pasamos al callback del nodo para que los procese
def on_message(client, userdata, msg):
try:
debug("[GladosNode] mqtt_rcv: { "+msg.topic + " - " +msg.payload+ " }")
except:
debug("[GladosNode] mqtt_rcv: not str ")
nodeMsgCallback(client, userdata, msg)
def on_disconnect(client, userdata, rc):
nodeDisconnectedCallback(client, userdata, rc)
#Inicia mqtt y devuelve el control
def initMQTT(host,port,name,connectedCallback,msgCallback,disconnectedCallback) :
global mqttServer
global mqttPort
global nodeName
global baseTopic
global debugTopic
global mqttClient
global nodeConnectedCallback
global nodeMsgCallback
global nodeDisconnectedCallback
mqttServer = host
mqttPort = port
nodeName = name
baseTopic = "node/"+nodeName+"/"
debugTopic = baseTopic+"debug"
nodeConnectedCallback = connectedCallback
nodeMsgCallback = msgCallback
nodeDisconnectedCallback = disconnectedCallback
mqttClient.on_connect = on_connect
mqttClient.on_message = on_message
mqttClient.on_disconnect = on_disconnect
print("[GladosNode] Connecting : "+str(mqttServer)+" Port:"+str(mqttPort)+ " node: " + str(name))
try:
mqttClient.connect(mqttServer,port=mqttPort,keepalive=120)
except:
print("Cant connect, will retry automatically")
mqttClient.loop_start()
#Inicia mqtt y captura la ejecucion
def initMQTTandLoopForever(host,port,name,connectedCallback,msgCallback,disconnectedCallback) :
global mqttServer
global mqttPort
global nodeName
global baseTopic
global debugTopic
global nodeConnectedCallback
global nodeMsgCallback
global nodeDisconnectedCallback
mqttServer = host
mqttPort = port
nodeName = name
baseTopic = "node/"+nodeName+"/"
debugTopic = baseTopic+"debug"
nodeConnectedCallback = connectedCallback
nodeMsgCallback = msgCallback
nodeDisconnectedCallback = disconnectedCallback
mqttClient.on_connect = on_connect
mqttClient.on_message = on_message
mqttClient.on_disconnect = on_disconnect
print("[GladosNode] Connecting : "+str(mqttServer)+" Port:"+str(mqttPort)+ " node: " + str(name))
try:
mqttClient.connect(mqttServer,port=mqttPort,keepalive=120)
except:
print("Cant connect, will retry automatically")
mqttClient.loop_forever()