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
754f3408
authored
May 06, 2024
by
Vicente Castellano Gómez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
- Funcando de una vezzzzzz
parent
6b9b7537
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
148 additions
and
59 deletions
conexion.py
conexionConfig.json
main.py
mensajeRobot.py
conexion.py
View file @
754f3408
import
paho.mqtt.client
as
mqtt
import
json
#import paho.mqtt.client as mqtt
import
ujson
from
umqtt.robust
import
MQTTClient
class
Conexion
:
def
__init__
(
self
,
topics
,
on_msg
):
def
__init__
(
self
,
topics
:
list
,
on_msg
):
with
open
(
file
=
"conexionConfig.json"
,
mode
=
'r'
)
as
f
:
data
=
json
.
load
(
f
)
with
open
(
"conexionConfig.json"
,
'r'
)
as
f
:
data
=
u
json
.
load
(
f
)
self
.
__broker_address
=
data
[
'ipbroker'
]
self
.
__port
=
int
(
data
[
'portbroker'
])
...
...
@@ -19,6 +20,7 @@ class Conexion:
#self.__topic = ["A3-467/GrupoL","map"]
self
.
__topic
=
topics
#def on_message(client, userdata, message):
# print("message received " ,str(message.payload.decode("utf-8")))
...
...
@@ -30,29 +32,34 @@ class Conexion:
#broker_address="192.168.0.19"
#broker_address="iot.eclipse.org"
print
(
"creating new instance"
)
client
=
mqtt
.
Client
(
mqtt
.
CallbackAPIVersion
.
VERSION2
)
#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
print
(
"connecting to broker"
)
client
.
connect
(
self
.
__broker_address
,
self
.
__port
)
#connect to broker
client
.
loop_start
()
#start the loop
#client.on_message = on_msg
if
(
len
(
self
.
__topic
)
==
0
):
print
(
f
" Subscrito a {self.__topic}"
)
else
:
for
tp
in
self
.
__topic
:
client
.
subscribe
(
tp
)
print
(
f
" Subscrito a {tp}"
)
"""
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)
self
.
__client
=
client
print
(
"Inicializado"
)
#self.__listaPendientes = listaPendientes
...
...
@@ -62,31 +69,39 @@ class Conexion:
def
publicar
(
self
,
topic
,
msg
):
client
=
mqtt
.
Client
(
mqtt
.
CallbackAPIVersion
.
VERSION2
)
client
.
connect
(
self
.
__broker_address
,
self
.
__port
)
result
=
client
.
publish
(
topic
,
msg
)
client
.
disconnect
()
#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]
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
()
self
.
__
client
.
disconnect
()
self
.
client
.
loop_stop
()
self
.
client
.
disconnect
()
print
(
"Desconectado"
)
def
desubscribir
(
self
,
topic
):
self
.
__client
.
unsubscribe
(
topic
)
print
(
f
"Desubscrito topic : {topic}"
)
self
.
client
.
unsubscribe
(
topic
)
#print(f"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
=
mqtt
.
Client
(
mqtt
.
CallbackAPIVersion
.
VERSION2
)
client =
MQTTClient("PACO",
)
client.connect("192.168.0.19" , 1883)
client.publish(topic, message)
client.disconnect()
...
...
@@ -120,4 +135,6 @@ def send_mqtt_message( topic,message):
# client = connect_mqtt()
# subscribe(client)
# client.loop_forever()
"""
conexionConfig.json
View file @
754f3408
{
"ipbroker"
:
"192.168.
0.19
"
,
"ipbroker"
:
"192.168.
48.245
"
,
"portbroker"
:
"1883"
,
"topicSubsRobot"
:
[
"A3-467/GrupoL/Robot"
,
"A3-467/GrupoL/Sinc"
],
"topicSubsInterfaz"
:
[
"A3-467/GrupoL/Interfaz"
,
"map"
,
"A3-467/GrupoL/Sinc"
],
"topicSendRobot"
:
"A3-467/GrupoL/Interfaz"
,
"topicSendInterfaz"
:
"A3-467/GrupoL/Robot"
,
"topicSubsRobot"
:
[
"A3-467/GrupoL/Robot"
,
"A3-467/GrupoL/Sinc
Robot
"
],
"topicSubsInterfaz"
:
[
"A3-467/GrupoL/Interfaz"
,
"map"
,
"A3-467/GrupoL/Sinc
Interfaz
"
],
"topicSendRobot"
:
[
"A3-467/GrupoL/Interfaz"
,
"A3-467/GrupoL/SincInterfaz"
]
,
"topicSendInterfaz"
:
[
"A3-467/GrupoL/Robot"
,
"A3-467/GrupoL/SincRobot"
]
,
"separadorMensaje"
:
":"
,
"prefijoMensajes"
:
{
"mapa"
:
"map"
,
"pedido"
:
"ped"
,
"posicion"
:
"pos"
"posicion"
:
"pos"
,
"sinc"
:
"Sincronizar"
}
}
\ No newline at end of file
main.py
View file @
754f3408
...
...
@@ -2,9 +2,37 @@
from
robot
import
Robot
import
camino
from
camino
import
MovGiro
as
mov
from
umqtt.robust
import
MQTTClient
import
conexion
as
cn
def
sub_cp
(
topic
,
msg
):
print
(
"Recibido topic"
,
topic
.
decode
(),
" mensage "
,
msg
.
decode
())
subs
=
[
"A3-467/GrupoL/Robot"
,
"A3-467/GrupoL/SincRobot"
]
send
=
[
"A3-467/GrupoL/Interfaz"
,
"A3-467/GrupoL/SincInterfaz"
]
#cx = cn.Conexion(subs,on_msg=sub_cp)
#cx.publicar(send[0],"BBBBBBBBBB")
cx
=
cn
.
Conexion
(
subs
,
sub_cp
)
print
(
"Creado"
)
while
True
:
cx
.
checkMensages
()
"""
import mensajeRobot as m
mR = m.MensajeRobot()
print("creada conexion")
mapa = mR.getMapa()
print(mapa)
pos = (4,4)
mR.sendPosicion(pos)
"""
"""
robot = Robot()
mapa = "0202000105030705000200041109060110031000000200080101100110000106010701"
#robot.calibraSensorColor()
...
...
@@ -15,3 +43,4 @@ robot.recorreRuta(mov,cas)
robot.recogePaquete()
"""
\ No newline at end of file
mensajeRobot.py
View file @
754f3408
#from typing import Any
import
conexion
as
cn
import
time
import
json
import
u
json
class
MensajeRobot
:
def
__init__
(
self
)
->
None
:
print
(
"Inicizlizando"
)
def
__init__
(
self
)
->
None
:
def
on_message
(
client
,
userdata
,
message
):
print
(
f
'Recive Topic:{message.topic} Mensage:{str(message.payload.decode("utf-8"))}'
)
if
(
message
.
topic
==
self
.
topicSubs
[
-
1
]):
if
(
str
(
message
.
payload
.
decode
(
"utf-8"
))
!=
str
(
self
.
__class__
)):
self
.
sinc
=
True
def
sub_cp
(
topic
,
msg
):
print
(
"Recibido topic"
,
topic
,
" mensage "
,
msg
)
if
(
topic
==
self
.
topicSubs
[
-
2
]):
self
.
sinc
=
True
else
:
self
.
mensaje
=
str
(
message
.
payload
.
decode
(
"utf-8"
))
self
.
mensaje
=
str
(
msg
.
decode
(
'utf-8'
))
def
on_message_default
(
client
,
userdata
,
message
):
#print(f'Recive Topic:{message.topic} Mensage:{str(message.payload.decode("utf-8"))}')
self
.
mensaje
=
str
(
message
.
payload
.
decode
(
"utf-8"
))
if
(
self
.
mensaje
.
split
(
self
.
sepMsg
)[
0
]
!=
self
.
prefMsg
[
'mapa'
]):
print
(
"EL MAPA EL MAPA EL MAPA"
)
def
on_message_sinc
(
client
,
userdata
,
message
):
#print(f'Recive Topic:{message.topic} Mensage:{str(message.payload.decode("utf-8"))}')
self
.
sinc
=
True
#def on_message(client, userdata, message):
# print(f'Recive Topic:{message.topic} Mensage:{str(message.payload.decode("utf-8"))}')
# filtrarMensage(message.topic,str(message.payload.decode("utf-8")))
#if(message.topic == self.topicSubs[-1]):
# if(str(message.payload.decode("utf-8")) != str(self.__class__)):
# self.sinc = True
#else:
# self.mensaje = str(message.payload.decode("utf-8"))
with
open
(
file
=
"conexionConfig.json"
,
mode
=
'r'
)
as
f
:
data
=
json
.
load
(
f
)
with
open
(
"conexionConfig.json"
,
'r'
)
as
f
:
data
=
u
json
.
load
(
f
)
#self.topicSubs = ["A3-467/GrupoL/Robot"]
self
.
topicSubs
=
data
[
'topicSubsRobot'
]
#print(f'Topics a subscribir {self.topicSubs}')
#self.topicSend = ["A3-467/GrupoL/Interfaz"]
self
.
topicSend
=
data
[
'topicSendRobot'
]
#print(f'Topics a enviar {self.topicSend}')
#self.prefMsg = {"mapa":'map',
# "pedido":'ped',
...
...
@@ -34,39 +55,58 @@ class MensajeRobot:
self
.
prefMsg
=
data
[
'prefijoMensajes'
]
self
.
sepMsg
=
data
[
'separadorMensaje'
]
self
.
conex
=
cn
.
Conexion
(
self
.
topicSubs
,
on_msg
=
on_message
)
defs
=
[
on_message_default
,
on_message_sinc
]
self
.
conex
=
cn
.
Conexion
(
self
.
topicSubs
,
on_msg
=
sub_cp
)
self
.
mensaje
=
""
print
(
"sincronizando"
)
self
.
sinc
=
False
self
.
__sincronizacion
()
#self.sinc = True
def
__sincronizacion
(
self
):
while
(
not
self
.
sinc
):
self
.
conex
.
publicar
(
self
.
topicSubs
[
-
1
],
str
(
self
.
__class__
))
time
.
sleep
(
5
)
self
.
conex
.
publicar
(
self
.
topicSend
[
-
1
],
self
.
prefMsg
[
'sinc'
])
time
.
sleep
(
2
)
print
(
"SINCRONIZADO"
)
self
.
conex
.
desubscribir
(
self
.
topicSubs
[
-
1
])
self
.
conex
.
publicar
(
self
.
topicS
ubs
[
-
1
],
str
(
self
.
__class__
)
)
self
.
conex
.
publicar
(
self
.
topicS
end
[
-
1
],
self
.
prefMsg
[
'sinc'
]
)
def
getMapa
(
self
):
while
(
self
.
mensaje
.
split
(
self
.
sepMsg
)[
0
]
!=
self
.
prefMsg
[
'mapa'
]):
time
.
sleep
(
1
)
time
.
sleep
(
2
)
return
self
.
mensaje
.
split
(
self
.
sepMsg
)[
1
]
def
getPedido
(
self
):
self
.
conex
.
publicar
(
self
.
topicSend
,
self
.
prefMsg
[
'pedido'
]
+
self
.
sepMsg
)
self
.
conex
.
publicar
(
self
.
topicSend
[
0
]
,
self
.
prefMsg
[
'pedido'
]
+
self
.
sepMsg
)
while
(
self
.
mensaje
.
split
(
self
.
sepMsg
)[
0
]
!=
self
.
prefMsg
[
'pedido'
]):
print
(
f
"Esperando Pedido - {self.mensaje.split(self.sepMsg)[0],self.prefMsg['pedido']}"
)
#
print(f"Esperando Pedido - {self.mensaje.split(self.sepMsg)[0],self.prefMsg['pedido']}")
time
.
sleep
(
1
)
return
self
.
mensaje
.
split
(
self
.
sepMsg
)[
1
]
ped
=
[]
pos
=
[]
i
=
0
for
c
in
self
.
mensaje
.
split
(
self
.
sepMsg
)[
1
]:
pos
.
append
(
int
(
c
))
if
(
i
%
2
==
1
):
ped
.
append
(
pos
)
pos
=
[]
i
=
i
+
1
return
ped
def
sendPosicion
(
self
,
posicion
):
self
.
conex
.
publicar
(
self
.
topicSend
,
self
.
prefMsg
[
'posicion'
]
+
self
.
sepMsg
+
str
(
posicion
))
self
.
conex
.
publicar
(
self
.
topicSend
[
0
],
self
.
prefMsg
[
'posicion'
]
+
self
.
sepMsg
+
str
(
posicion
))
...
...
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