I think it is quite difficult to prepare the environment for operation with a little confirmation. I would like to easily set the environment with C4SA and operate Nifty Cloud MQTT.
Please complete the registration to C4SA in advance. It can be used free of charge for 15 days. There is no need to prepare anything other than a compatible browser on the client side.
Now, let's create a canvas that runs Python.
Click Create New Canvas and select Python Campus.
Set the canvas name to "Subscriber".
When the creation is completed, you can operate it in the canvas list.
When you open the canvas, you can operate the created application.
C4SA has a CLI function for installing packages and so on. This feature installs Paho to publish from the CLI to MQTT. Open the CLI and run the following command.
pip install paho-mqtt
Refer to here and set the information to connect to Nifty Cloud MQTT from the sample. To create the source, use the canvas editor.
A new file will be created, so click the new button.
Name the file "mqttsub.py".
In the editor, write the following files.
mqttsub.py
# coding=utf8
import paho.mqtt.client as mqtt
USERNAME = "[Administrator user name]"
PASSWORD = "[password]"
def on_connect(client, userdata, result):
print("Connected with result code " + str(result))
def on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload))
if __name__ == '__main__':
client = mqtt.Client()
client.username_pw_set(USERNAME, PASSWORD)
client.on_connect = on_connect
client.on_message = on_message
client.connect("[domain]", [MQTT port])
client.subscribe("topic/#")
client.loop_forever()
Reopen the CLI and run the following command.
python mqttsub.py
The screen will look like the one below.
Next, we will create a publisher.
Then create a client to publish.
Create the canvas and install the required modules in the same way.
Set the information to connect to Nifty Cloud MQTT. To create the source, use the canvas editor.
Set the canvas name to "Publisher".
Name the file "mqttpub.py".
In the editor, write the following files.
mqttpub.py
# coding=utf8
import paho.mqtt.client as mqtt
USERNAME = "[Administrator user name]"
PASSWORD = "[password]"
if __name__ == '__main__':
client = mqtt.Client()
client.username_pw_set(USERNAME, PASSWORD)
client.connect("[domain]", [MQTT port])
client.publish("topic", "C4SA test message")
Reopen the CLI and run the following command.
python mqttpub.py
Let's take a look at my canvas, which the subscriber created and launched earlier.
The message you sent is displayed.
What did you think? I was able to easily operate Nifty Cloud MQTT.