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
d3a6ecda
authored
May 05, 2024
by
almagosi
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Probably finally final commit
parent
2e04393a
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
123 additions
and
70 deletions
conexion.py
conexionConfig.json
main_interfaz.py
main_map.py
main_robot.py
mensajeInterfaz.py
mensajeRobot.py
conexion.py
View file @
d3a6ecda
...
...
@@ -3,8 +3,10 @@ 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
,
on_msg
):
def
__init__
(
self
,
topics
:
list
,
on_msg
:
list
):
with
open
(
file
=
"conexionConfig.json"
,
mode
=
'r'
)
as
f
:
...
...
@@ -20,6 +22,7 @@ class Conexion:
self
.
__topic
=
topics
#def on_message(client, userdata, message):
# print("message received " ,str(message.payload.decode("utf-8")))
# print("message topic=",message.topic)
...
...
@@ -30,29 +33,30 @@ 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
)
#client.on_message = on_message
#client.on_message=on_message #attach function to callback
client
.
on_message
=
on_msg
#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"
)
client
.
connect
(
self
.
__broker_address
,
self
.
__port
)
#connect to broker
client
.
loop_start
()
#start the loop
self
.
client
.
connect
(
self
.
__broker_address
,
self
.
__port
)
#connect to broker
if
(
len
(
self
.
__topic
)
==
0
):
print
(
f
" Subscrito a {self.__topic}"
)
else
:
for
tp
in
self
.
__topic
:
client
.
subscribe
(
tp
)
for
tp
in
topics
:
self
.
client
.
subscribe
(
tp
)
print
(
f
" Subscrito a {tp}"
)
#self.__client = self.__connect_mqtt()
#self.inicializar()
#self.__client.loop_start()
self
.
client
.
on_message
=
self
.
on_message
self
.
client
.
loop_start
()
#start the loop
#self.subscribe(cliente=client,topic=self.__topic)
self
.
__client
=
client
print
(
"Inicializado"
)
#self.__listaPendientes = listaPendientes
...
...
@@ -62,10 +66,10 @@ 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)
result
=
self
.
client
.
publish
(
topic
,
msg
)
#
client.disconnect()
#result = self.__client.publish(self.__topic[0],msg)
# result: [0, 1]
status
=
result
[
0
]
...
...
@@ -75,12 +79,12 @@ class Conexion:
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
)
self
.
client
.
unsubscribe
(
topic
)
print
(
f
"Desubscrito topic : {topic}"
)
...
...
conexionConfig.json
View file @
d3a6ecda
{
"ipbroker"
:
"192.168.0.19"
,
"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_interfaz.py
View file @
d3a6ecda
import
mensajeInterfaz
as
msg
import
time
msgInterfaz
=
msg
.
Mensa
g
eInterfaz
()
msgInterfaz
=
msg
.
Mensa
j
eInterfaz
()
mapa
=
msgInterfaz
.
getMapa
()
print
(
f
"Mapa recibido {mapa}"
)
...
...
@@ -13,7 +13,10 @@ while(not msgInterfaz.pedidoSolicitado):
posicion
=
msgInterfaz
.
getPosicion
()
print
(
f
"Posicion Recibida {posicion}"
)
print
(
f
'Posicion x = {posicion[0]} : y = {posicion[1]}'
)
pedido
=
((
4
,
5
),(
7
,
9
))
msgInterfaz
.
sendPedido
(
pedido
)
print
(
"Pedido enviado"
)
time
.
sleep
(
60
)
\ No newline at end of file
main_map.py
View file @
d3a6ecda
...
...
@@ -6,4 +6,4 @@ mapa = "0202000105030705000200041109060110031000000200080101100110000106010701"
while
(
True
):
cn
.
send_mqtt_message
(
"map"
,
mapa
)
print
(
"Enviado"
)
time
.
sleep
(
60
)
\ No newline at end of file
time
.
sleep
(
30
)
\ No newline at end of file
main_robot.py
View file @
d3a6ecda
import
mensajeRobot
as
msg
msgRobot
=
msg
.
Mensa
g
eRobot
()
msgRobot
=
msg
.
Mensa
j
eRobot
()
mapa
=
msgRobot
.
getMapa
()
print
(
f
"Mapa recibido {mapa}"
)
...
...
@@ -10,3 +10,4 @@ msgRobot.sendPosicion(posicion)
pedido
=
msgRobot
.
getPedido
()
print
(
f
"Pedido recibido {pedido}"
)
print
(
pedido
[
0
][
0
],
pedido
[
0
][
1
],
pedido
[
1
][
0
],
pedido
[
1
][
1
])
\ No newline at end of file
mensajeInterfaz.py
View file @
d3a6ecda
...
...
@@ -6,46 +6,55 @@ import json
class
Mensa
g
eInterfaz
:
class
Mensa
j
eInterfaz
:
def
__init__
(
self
)
->
None
:
def
filtrarMensage
(
topic
,
mensaje
):
if
(
topic
==
self
.
prefMsg
[
'mapa'
]):
self
.
mapa
=
mensaje
while
(
not
self
.
sinc
):
time
.
sleep
(
1
)
def
on_message_default
(
client
,
userdata
,
message
):
print
(
f
'Recive Topic:{message.topic} Mensage:{str(message.payload.decode("utf-8"))}'
)
mensaje
=
str
(
message
.
payload
.
decode
(
"utf-8"
))
if
(
mensaje
.
split
(
self
.
sepMsg
)[
0
]
==
self
.
prefMsg
[
'pedido'
]):
self
.
pedidoSolicitado
=
True
self
.
__enviarMapa
()
self
.
conex
.
desubscribir
(
topic
)
if
(
mensaje
.
split
(
self
.
sepMsg
)[
0
]
==
self
.
prefMsg
[
'posicion'
]):
mensaje
=
str
(
message
.
payload
.
decode
(
"utf-8"
))
sspos
=
mensaje
.
split
(
self
.
sepMsg
)[
1
]
.
split
(
","
)
ssx
=
int
(
sspos
[
0
]
.
split
(
"("
)[
1
])
ssy
=
int
(
sspos
[
1
]
.
split
(
")"
)[
0
])
self
.
posicion
=
(
ssx
,
ssy
)
else
:
if
(
topic
==
self
.
topicSubs
[
-
1
]):
if
(
mensaje
!=
str
(
self
.
__class__
)):
self
.
sinc
=
True
else
:
if
(
mensaje
.
split
(
self
.
sepMsg
)[
0
]
!=
self
.
prefMsg
[
'pedido'
]):
self
.
pedidoSolicitado
=
True
if
(
mensaje
.
split
(
self
.
sepMsg
)[
0
]
!=
self
.
prefMsg
[
'posicion'
]):
self
.
posicion
=
list
(
mensaje
.
split
(
self
.
sepMsg
)[
1
])
def
on_message_mapa
(
client
,
userdata
,
message
):
print
(
f
'Recive Topic:{message.topic} Mensage:{str(message.payload.decode("utf-8"))}'
)
self
.
mapa
=
str
(
message
.
payload
.
decode
(
"utf-8"
))
while
(
not
self
.
sinc
):
time
.
sleep
(
2
)
self
.
__enviarMapa
()
self
.
conex
.
desubscribir
(
message
.
topic
)
def
on_message
(
client
,
userdata
,
message
):
def
on_message
_sinc
(
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"
)))
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")))
with
open
(
file
=
"conexionConfig.json"
,
mode
=
'r'
)
as
f
:
data
=
json
.
load
(
f
)
#self.topicSubs = ["A3-467/GrupoL/Robot"]
self
.
topicSubs
=
data
[
'topicSubsInterfaz'
]
print
(
f
'Topics a subscribir {self.topicSubs}'
)
#self.topicSend = ["A3-467/GrupoL/Interfaz"]
self
.
topicSend
=
data
[
'topicSendInterfaz'
]
print
(
f
'Topics a enviar {self.topicSend}'
)
#self.prefMsg = {"mapa":'map',
# "pedido":'ped',
...
...
@@ -54,7 +63,8 @@ class MensageInterfaz:
self
.
prefMsg
=
data
[
'prefijoMensajes'
]
self
.
sepMsg
=
data
[
'separadorMensaje'
]
self
.
conex
=
cn
.
Conexion
(
self
.
topicSubs
,
on_msg
=
on_message
)
defs
=
[
on_message_default
,
on_message_mapa
,
on_message_sinc
]
self
.
conex
=
cn
.
Conexion
(
self
.
topicSubs
,
on_msg
=
defs
)
self
.
mapa
=
""
self
.
posicion
:
list
self
.
pedidoSolicitado
=
False
...
...
@@ -63,13 +73,14 @@ class MensageInterfaz:
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
__enviarMapa
(
self
):
self
.
conex
.
publicar
(
self
.
topicSend
,
self
.
prefMsg
[
'mapa'
]
+
self
.
sepMsg
+
self
.
mapa
)
self
.
conex
.
publicar
(
self
.
topicSend
[
0
],
str
(
self
.
prefMsg
[
'mapa'
]
+
self
.
sepMsg
+
self
.
mapa
)
)
def
getMapa
(
self
):
...
...
@@ -79,7 +90,11 @@ class MensageInterfaz:
def
sendPedido
(
self
,
pedido
):
self
.
conex
.
publicar
(
self
.
topicSend
,
self
.
prefMsg
[
'pedido'
]
+
self
.
sepMsg
+
str
(
pedido
))
msg
=
""
for
pos
in
pedido
:
for
num
in
pos
:
msg
=
msg
+
str
(
num
)
self
.
conex
.
publicar
(
self
.
topicSend
[
0
],
self
.
prefMsg
[
'pedido'
]
+
self
.
sepMsg
+
msg
)
self
.
pedidoSolicitado
=
False
...
...
@@ -94,6 +109,3 @@ class MensageInterfaz:
\ No newline at end of file
mensajeRobot.py
View file @
d3a6ecda
...
...
@@ -11,21 +11,34 @@ class MensajeRobot:
def
__init__
(
self
)
->
None
:
def
on_message
(
client
,
userdata
,
message
):
def
on_message
_default
(
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
else
:
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
)
#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,7 +47,8 @@ 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
=
defs
)
self
.
mensaje
=
""
self
.
sinc
=
False
self
.
__sincronizacion
()
...
...
@@ -42,31 +56,47 @@ class MensajeRobot:
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']}"
)
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