Привет. Я пытаюсь подписаться на сообщения / темы с помощью paho-mqtt, но он не подключается к клиенту. Вот фрагмент кода.
import time
import sys
import os
from functools import partial
import paho.mqtt.client as mqttClient
# To convert *.txt to *.csv for later use.
# import pandas as pd
sys.path.append(os.path.join(os.path.dirname(__file__),'../'))
from src.node_utils import create_node_logger, connect_to_broker
from src import topics
class LoggingNode():
"""# use to log the details of this two topics
# state_encoders = root + '/' + 'state_encoders'
# format
# Instance of State class
# estimates the robots position based on encoder ticks
# state_model = root + '/' + 'state_model'
# format
# Instance of State class for where the motion model estimates the
# robot will be based on wheel_velocity
#------------------------------------------------------------------#
#------------------------------------------------------------------# """
def __init__(self, client, logger):
self.state = None
self.logger = logger
self.client = client
# initialize subscription to encoders and wheel_velocity
def on_connect(self, userdata, flags, rc):
suc1 = self.client.subscribe(topics.state_encoders)
suc2 = self.client.subscribe(topics.state_model)
self.logger.info('Connected and Subscribed: {} {}'.format(suc1, suc2))
def on_message(self, client, userdata, message):
print('heelo')
print("Message received: " + message.payload)
with open('/home/test.txt','a+') as f:
f.write("Message received: " + message.payload + "\n")
Connected = False #global variable for the state of the connection
if __name__ == '__main__':
node_name = 'logging_node'
logger = create_node_logger(node_name)
client = connect_to_broker()
node = LoggingNode(client, logger)
client.on_connect = node.on_connect
client.message_callback_add(topics.state_model, node.on_message)
logger.info('Started logging Node loop')
try:
client.loop_forever()
except KeyboardInterrupt:
logger.info("KeyboardInterrupt seen")
Здесь подключена модель состояния. Это также публикация сообщений. Могу ли я в любом случае поставить подписку на данные и поместить их в * .txt и / или * .csv?
Это фактический узел, который правильно публикует сообщения.
jay@jay-MS-7885:~/autonomous-class/src$ python3 state_estimation_node.py $HOSTNAME
2020-03-07 19:04:22,412 node:state_estimate INFO
Started state_estimate loop
2020-03-07 19:04:22,413 node:state_estimate INFO
Connected and Subscribed: (0, 3) (0, 4)