This time, I made a box on the Subscriber side of MQTT and tried it with Pepper.
Click here for the contents of the operation on the Publisher side.
I'm still studying MQTT.
The following libraries are used as the MQTT library for Python.
Set the MQTT broker information and the topic information to be acquired.
Connect to the MQTT broker and subscribe to the topic. After that, loop_forever puts it in the reception standby state. At the end of the program, the topic will be unsubscribed.
For how to use the library in the box, I referred to the following article.
(After reviewing something, I wanted to fix the code)
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
def onLoad(self):
self.framemanager = ALProxy("ALFrameManager")
self.folderName = None
def onUnload(self):
import sys
self.mqttc.unsubscribe(self.topic)
if self.folderName and self.folderName in sys.path:
sys.path.remove(self.folderName)
self.folderName = None
def onInput_onStart(self, payload):
import sys, os
self.folderName = os.path.join(
self.framemanager.getBehaviorPath(self.behaviorId), "../lib")
if self.folderName not in sys.path:
sys.path.append(self.folderName)
import paho.mqtt.client as paho
self.host = self.getParameter("Broker Host")
self.port = self.getParameter("Broker Port")
self.keep_alive = self.getParameter("KeepAlive")
self.topic = self.getParameter("Topic")
self.qos = self.getParameter("Qos")
self.mqttc = paho.Client()
self.mqttc.on_message = self.onMessage
self.mqttc.connect(self.host, self.port, self.keep_alive)
self.mqttc.subscribe(self.topic, self.qos)
self.mqttc.loop_forever()
def onMessage(self, mqttc, obj, msg):
self.logger.info("topic: " + msg.topic + ", payload: " + str(msg.payload) + ", qos: " + str(msg.qos) + ", retain: " + str(msg.retain))
def onInput_onStop(self):
self.onUnload()
self.onStopped()
Just skip "hello world" to the MQTT broker.
pub.py
# coding=utf8
import paho.mqtt.client as paho
mqttc = paho.Client()
mqttc.connect("test.mosquitto.org", 1883, 60)
mqttc.publish("my/topic/pepper5", "hello world", 1)
Run the program in Choregraphe and wait for the message to be received.
Run Publisher from the terminal.
$ python pub.py
The message Publisher-> Broker-> Subscriber is transmitted, and the content is displayed in the log viewer of Choregraphe on the Subscriber side.
If you try changing the processing when receiving a message on the Subscriber side to the form below, Pepper will talk.
def onMessage(self, mqttc, obj, msg):
tts = ALProxy("ALTextToSpeech")
tts.say(msg.payload)