Try operating Nifty Cloud MQTT from C4SA

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.

Preparation

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.

Make a subscriber

Create a canvas

Now, let's create a canvas that runs Python.

newcanlist.png

Click Create New Canvas and select Python Campus.

newcanname.png

Set the canvas name to "Subscriber".

subcanlist.png

When the creation is completed, you can operate it in the canvas list.

subcandisplay.png

When you open the canvas, you can operate the created application.

Install the required modules

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

subcancliinstall.png

Create a working source

Refer to here and set the information to connect to Nifty Cloud MQTT from the sample. To create the source, use the canvas editor.

subcanfilenew.png

A new file will be created, so click the new button.

subcanfilenew.png

Name the file "mqttsub.py".

subcanfileedit.png

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

Try to make it work

Reopen the CLI and run the following command.

python mqttsub.py

The screen will look like the one below.

subcanrun.png

Next, we will create a publisher.

Make a bubbler

Then create a client to publish.

Introduction

Create the canvas and install the required modules in the same way.

Create a working source

Set the information to connect to Nifty Cloud MQTT. To create the source, use the canvas editor.

pubcannew.png

Set the canvas name to "Publisher".

pubcannewfile.png

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

Try to make it work

Reopen the CLI and run the following command.

python mqttpub.py

pubcanrun.png

Let's take a look at my canvas, which the subscriber created and launched earlier.

subcanoutput.png

The message you sent is displayed.

Finally

What did you think? I was able to easily operate Nifty Cloud MQTT.

Recommended Posts

Try operating Nifty Cloud MQTT from C4SA
Try operating Studio Library from Python. [Anim Save]
Try to make Qiita's Word Cloud from your browser history