What is MQTT? For details, please check the following sites, but a brief explanation is the lightweight protocol of the Pub / Sub messaging model. Since it is assumed to be operated by a weak device that can be connected by IoT and WoT, various low-cost processing is possible.
I think Pepper can use it, so I'll give it a try.
MQTT I'm studying.
The following libraries are used as the MQTT library for Python.
Create a Python box and change the input Type from "bang" to "string".
The setting items are like this.
Allows you to specify the MQTT broker host, port, KeepAlive value, and the topic to send the message to, the QOS value, and the Retain value.
The code looks like this. Get various parameters, connect to MQTT broker and send a message.
For how to use the library in the box, I referred to the following article.
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
def onLoad(self):
self.framemanager = ALProxy("ALFrameManager")
self.folderName = None
def onUnload(self):
import sys
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
host = self.getParameter("Broker Host")
port = self.getParameter("Broker Port")
keep_alive = self.getParameter("KeepAlive")
topic = self.getParameter("Topic")
qos = self.getParameter("Qos")
retain = self.getParameter("Retain")
mqttc = paho.Client()
mqttc.connect(host, port, keep_alive)
mqttc.publish(topic, payload, qos, retain)
def onInput_onStop(self):
self.onUnload()
self.onStopped()
Connect the "Text Edit" box for setting the message content and the "MQTT" box you created.
Prepare a Subscriber that will receive messages sent from the MQTT Publisher side. It uses the same Paho library as the Pub side.
sub.py
import paho.mqtt.client as paho
def on_message(mqttc, obj, msg):
print("topic: " + msg.topic + ", payload: " + str(msg.payload) + ", qos: \
" + str(msg.qos) + ", retain: " + str(msg.retain))
if __name__ == '__main__':
mqttc = paho.Client()
mqttc.on_message = on_message
mqttc.connect("test.mosquitto.org", 1883, 60)
mqttc.subscribe("my/topic/pepper", 0)
mqttc.loop_forever()
Run on your PC and wait to receive a message for the topic.
$ python sub.py
Run the program in Choregraphe and send a message.
The message Publisher-> Broker-> Subscriber was transmitted and displayed on the Subscriber side.
$ python sub.py
topic: my/topic/pepper, payload: Hello World from Pepper, qos: 0, retain: 0