Make an MQTT Publisher box

Introduction

MQTT Publisher Box

The following libraries are used as the MQTT library for Python.

Create a Python box and change the input Type from "bang" to "string".

2015032101.png

Setting items

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.

2015032102.png

Python code

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 with Choregraphe

Connect the "Text Edit" box for setting the message content and the "MQTT" box you created.

2015032103.png

Subscriber side

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()

Operation check

Run Subscriber

Run on your PC and wait to receive a message for the topic.

$ python sub.py

Run Publisher

Run the program in Choregraphe and send a message.

Result display

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

Impressions

Recommended Posts

Make an MQTT Publisher box
Make an MQTT Publisher Box Part 2
Make an MQTT Subscriber box
Let's make an Errbot plugin
Creating an HTTP Get box
Make a Tweet box for Pepper
Make an RGB 3 color composite diagram
Make Scrapy an exe with Pyinstaller