Make an MQTT Publisher Box Part 2

Introduction

TLS MQTT Publisher Box

Create a "TLS MQTT Pub" box by copying the previous box.

Setting items

For Certificate, specify the file name of the certificate. If you want to use test.mosquitto.org, download it from here.

2015032901.png

Place the certificate to be used in the following location.

2015032902.png

Python code

The code looks like this. It's almost the same as last time. I just get the crt_file and do tls_set ().

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")
        crt_file   = self.getParameter("Certificate")
        keep_alive = self.getParameter("KeepAlive")
        topic      = self.getParameter("Topic")
        qos        = self.getParameter("Qos")
        retain     = self.getParameter("Retain")

        mqttc = paho.Client()
        mqttc.tls_set(self.folderName + "/crts/" + crt_file)
        mqttc.connect(host, port, keep_alive)
        mqttc.publish(topic, payload, qos, retain)

    def onInput_onStop(self):
        self.onUnload()
        self.onStopped()

Connect with Choregraphe

This is almost the same as last time.

2015032903.png

Subscriber side

Subscriber is almost the same.

tls_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: " + s\
tr(msg.retain))

if __name__ == '__main__':
    mqttc = paho.Client()

    mqttc.on_message = on_message
    mqttc.tls_set("mosquitto.org.crt")
    mqttc.connect("test.mosquitto.org", 8883, 60)
    mqttc.subscribe("my/topic/tls/pepper", 0)

    mqttc.loop_forever()

Operation check

Run Subscriber

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

$ python tls_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 tls_sub.py
topic: my/topic/tls/pepper, payload: TLS Hello World from Pepper, qos: 0, retain: 0

Confirmation of communication contents

For the time being, let's capture the packet and see if it is SSL / TLS. I haven't looked at the details of the contents, but I can see that it seems to be communicating with SSL / TLSv1.

2015032904.png

Impressions

Recommended Posts

Make an MQTT Publisher Box Part 2
Make an MQTT Publisher box
Make an MQTT Subscriber box
Make a Blueqat backend ~ Part 1
Make a Blueqat backend ~ Part 2
Let's make Splatoon AI! part.1
Creating an HTTP Get box
Make an effector for video conferencing with Spout + OpenCV + dlib (Part 1)
Let's make an IoT shirt with Lambda, Kinesis, Raspberry Pi [Part 1]