Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Vicente Castellano Gómez
/
robotAmbientales
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
483fd2f2
authored
May 12, 2024
by
Samuel Valverde Garcia
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
conexion2
parent
899b7eb9
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
162 additions
and
36 deletions
conexion.py
conexion2.py
conexion.py
0 → 100644
View file @
483fd2f2
#import paho.mqtt.client as mqtt
import
ujson
from
umqtt.robust
import
MQTTClient
class
Conexion
:
def
__init__
(
self
,
topics
:
list
,
on_msg
):
with
open
(
"conexionConfig.json"
,
'r'
)
as
f
:
data
=
ujson
.
load
(
f
)
self
.
__broker_address
=
data
[
'ipbroker'
]
self
.
__port
=
int
(
data
[
'portbroker'
])
# Configuración del broker MQTT
#self.__broker_address = "192.168.48.245" # Cambia por la dirección de tu broker MQTT
#self.__broker_address = "192.168.0.100" # Cambia por la dirección de tu broker MQTT
#self.__port = 1883 # Puerto predeterminado para MQTT
#self.__topic = ["A3-467/GrupoL","map"]
self
.
__topic
=
topics
#def on_message(client, userdata, message):
# print("message received " ,str(message.payload.decode("utf-8")))
# print("message topic=",message.topic)
# print("message qos=",message.qos)
# print("message retain flag=",message.retain)
# #self.__listaPendientes.append(f'{message.topic}-{message.payload.decode("utf-8")}')
########################################
#broker_address="192.168.0.19"
#broker_address="iot.eclipse.org"
print
(
"creating new instance"
)
#self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
self
.
client
=
MQTTClient
(
"RobotPACO"
,
self
.
__broker_address
)
self
.
client
.
set_callback
(
on_msg
)
self
.
client
.
connect
()
#client.on_message = on_message
#client.on_message=on_message #attach function to callback
#client.on_message = on_msg
"""
for tp,df in zip(topics,on_msg):
self.client.message_callback_add(tp,df)
print(f" Funcion {df} adjunta a {tp}")
print("connecting to broker")
self.client.connect(self.__broker_address,self.__port) #connect to broker
"""
for
tp
in
topics
:
self
.
client
.
subscribe
(
tp
)
print
(
"Subscrito a "
,
tp
)
#print(f" Subscrito a {tp}")
#self.__client = self.__connect_mqtt()
#self.inicializar()
#self.__client.loop_start()
#self.client.loop_start() #start the loop
#self.subscribe(cliente=client,topic=self.__topic)
print
(
"Inicializado"
)
#self.__listaPendientes = listaPendientes
#self.__client.loop_forever()
def
publicar
(
self
,
topic
,
msg
):
#client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
#client.connect(self.__broker_address, self.__port)
print
(
"Enviar a topic"
,
topic
,
" mensage "
,
msg
)
self
.
client
.
publish
(
topic
,
msg
)
#client.disconnect()
#result = self.__client.publish(self.__topic[0],msg)
# result: [0, 1]
def
desconectar
(
self
):
self
.
client
.
loop_stop
()
self
.
client
.
disconnect
()
print
(
"Desconectado"
)
def
desubscribir
(
self
,
topic
):
#self.client.unsubscribe(topic)
print
(
"Desubscrito topic : "
,
topic
)
def
checkMensages
(
self
):
self
.
client
.
check_msg
()
# Función para enviar mensajes MQTT al broker
def
publicarOut
(
topic
,
msg
):
# client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
# client.connect(self.__broker_address, self.__port)
print
(
"Enviar a topic"
,
topic
,
" mensage "
,
msg
)
client
=
MQTTClient
(
"PACO"
,
"192.168.48.245"
)
client
.
connect
()
client
.
publish
(
topic
,
msg
)
"""
def send_mqtt_message( topic,message):
client = MQTTClient("PACO",)
client.connect("192.168.0.19" , 1883)
client.publish(topic, message)
client.disconnect()
# def publish(client):
# msg_count = 1
# while True:
# #time.sleep(1)
# msg = f"messages: {msg_count}"
# result = client.publish(topic, msg)
# # result: [0, 1]
# status = result[0]
# if status == 0:
# print(f"Send `{msg}` to topic `{topic}`")
# else:
# print(f"Failed to send message to topic {topic}")
# msg_count += 1
# if msg_count > 5:
# break
#def __subscribe(client: mqtt, topic):
#def run():
# client = connect_mqtt()
# client.loop_start()
# publish(client)
# client.loop_stop()
#def run():
# client = connect_mqtt()
# subscribe(client)
# client.loop_forever()
"""
conexion2.py
View file @
483fd2f2
#import paho.mqtt.client as mqtt
import
ujson
from
umqtt.robust
import
MQTTClient
import
paho.mqtt.client
as
mqtt
import
json
class
Conexion
:
def
on_message
(
client
,
userdata
,
message
):
print
(
f
'MENSAGE PERDIDO - Recive Topic:{message.topic} Mensage:{str(message.payload.decode("utf-8"))}'
)
def
__init__
(
self
,
topics
:
list
,
on_msg
):
def
__init__
(
self
,
topics
:
list
,
on_msg
:
list
):
with
open
(
"conexionConfig.json"
,
'r'
)
as
f
:
data
=
u
json
.
load
(
f
)
with
open
(
file
=
"conexionConfig.json"
,
mode
=
'r'
)
as
f
:
data
=
json
.
load
(
f
)
self
.
__broker_address
=
data
[
'ipbroker'
]
self
.
__port
=
int
(
data
[
'portbroker'
])
...
...
@@ -32,31 +33,27 @@ class Conexion:
#broker_address="192.168.0.19"
#broker_address="iot.eclipse.org"
print
(
"creating new instance"
)
#self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
self
.
client
=
MQTTClient
(
"RobotPACO"
,
self
.
__broker_address
)
self
.
client
.
set_callback
(
on_msg
)
self
.
client
.
connect
()
self
.
client
=
mqtt
.
Client
(
mqtt
.
CallbackAPIVersion
.
VERSION2
)
#client.on_message = on_message
#client.on_message=on_message #attach function to callback
#client.on_message = on_msg
"""
for
tp
,
df
in
zip
(
topics
,
on_msg
):
self
.
client
.
message_callback_add
(
tp
,
df
)
print
(
f
" Funcion {df} adjunta a {tp}"
)
print
(
"connecting to broker"
)
self
.
client
.
connect
(
self
.
__broker_address
,
self
.
__port
)
#connect to broker
"""
for
tp
in
topics
:
self
.
client
.
subscribe
(
tp
)
print
(
"Subscrito a "
,
tp
)
#print(f" Subscrito a {tp}")
print
(
f
" Subscrito a {tp}"
)
#self.__client = self.__connect_mqtt()
#self.inicializar()
#self.__client.loop_start()
#self.client.loop_start() #start the loop
self
.
client
.
on_message
=
self
.
on_message
self
.
client
.
loop_start
()
#start the loop
#self.subscribe(cliente=client,topic=self.__topic)
...
...
@@ -71,11 +68,15 @@ class Conexion:
#client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
#client.connect(self.__broker_address, self.__port)
print
(
"Enviar a topic"
,
topic
,
" mensage "
,
msg
)
self
.
client
.
publish
(
topic
,
msg
)
result
=
self
.
client
.
publish
(
topic
,
msg
)
#client.disconnect()
#result = self.__client.publish(self.__topic[0],msg)
# result: [0, 1]
status
=
result
[
0
]
if
status
==
0
:
print
(
f
"Send `{msg}` to topic `{topic}`"
)
else
:
print
(
f
"Failed to send message to topic {topic}"
)
def
desconectar
(
self
):
self
.
client
.
loop_stop
()
...
...
@@ -83,26 +84,14 @@ class Conexion:
print
(
"Desconectado"
)
def
desubscribir
(
self
,
topic
):
#self.client.unsubscribe(topic)
print
(
"Desubscrito topic : "
,
topic
)
def
checkMensages
(
self
):
self
.
client
.
check_msg
()
self
.
client
.
unsubscribe
(
topic
)
print
(
f
"Desubscrito topic : {topic}"
)
# Función para enviar mensajes MQTT al broker
def
publicarOut
(
topic
,
msg
):
# client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
# client.connect(self.__broker_address, self.__port)
print
(
"Enviar a topic"
,
topic
,
" mensage "
,
msg
)
client
=
MQTTClient
(
"PACO"
,
"192.168.48.245"
)
client
.
connect
()
client
.
publish
(
topic
,
msg
)
"""
def
send_mqtt_message
(
topic
,
message
):
client =
MQTTClient("PACO",
)
client.connect("192.168.0.1
9
" , 1883)
client
=
mqtt
.
Client
(
mqtt
.
CallbackAPIVersion
.
VERSION2
)
client
.
connect
(
"192.168.0.1
34
"
,
1883
)
client
.
publish
(
topic
,
message
)
client
.
disconnect
()
...
...
@@ -135,6 +124,3 @@ def send_mqtt_message( topic,message):
# client = connect_mqtt()
# subscribe(client)
# client.loop_forever()
"""
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment