Create a "TLS MQTT Pub" box by copying the previous box.
For Certificate, specify the file name of the certificate. If you want to use test.mosquitto.org, download it from here.
Place the certificate to be used in the following location.
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()
This is almost the same as last time.
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()
Run on your PC and wait to receive a message for the topic.
$ python tls_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 tls_sub.py
topic: my/topic/tls/pepper, payload: TLS Hello World from Pepper, qos: 0, retain: 0
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.
Recommended Posts