Interacting With a Phone via CloudMQTT
Interacting With a Phone via CloudMQTT
This tutorial describes how easy it is to control and monitor CDP signals remotely over public Internet using cloud MQTT broker and your phone MQTT application using CDP Studio, the independent automation software for open PC-based real-time distributed control systems.
Choose and Sign Up an Account in a Cloud MQTT provider
First, you have to create an account at one of the cloud MQTT providers. Many cloud MQTT broker providers exist with different usage terms (many of them offer also free-of-charge services). For example, one of the easiest to set up MQTT broker with free plan available is at CloudMQTT.
As a result you must obtain broker server address, user name, password and SSL port.
Create MQTTClient System
Follow these steps to add create the MQTT client system:
- Create a new CDP system, name it MQTTTest
- Click on the default application called MQTTTestApp.
- Choose a MQTTClient from Resource tree and click Add to add MQTTClient to the application
Configure MQTTClient for Broker Connection
Follow these steps:
- Click on added MQTTClient component
- Find and configure the following MQTTClient connection properties:
- BrokerHost - set to server address you were provied
- BrokerPort - set to SSL port number you were provided
- UserID - set to user name you were provided
- Password - set to password you were provided
- Encryption - set to TLS to use encrypted message transport
- Debug - set to 2 for MQTTClient to output diagnostic messages and also incoming and outgoing MQTT payloads into Application Output pane
To be able to send values we need to add one PublishTopic to our MQTTClient
To add PublishTopic, follow these steps:
- Choose a PublishTopic<int> from Resource tree
- Click Add to add PublishTopic<int> to MQTTClient
To be able to receive values we also need to add one SubscribeTopic to our MQTTClient. To add SubscribeTopic, follow these steps (similar to adding PublishTopic):
- Choose SubscribeTopic<int> from Resource tree
- Click Add to add SubscribeTopic<int> to MQTTClient
Set Topic Strings
Every topic must have topic string in MQTT.
To be able to send and receive messages, choose and set topic strings:
- Set PublishTopic<int> topic string to value publishtest
- Set SubscribeTopic<int> topic string to value subscribetest
Install and Set Up MQTT Application on Your Phone
Follow these guidelines:
- Open your phone app center and search for applications using keyword MQTT
- Install one of the found MQTT apps, f.e. "MQTT Dash" or "IoT MQTT Panel" etc.
- Run installed app on your phone and configure MQTT broker. Set broker address and ports you were provided by broker
- Configure 2 topics (publish and subscribe) in the application and name them the other way round:
- set publish topic as you set SubscribeTopic in CDP, for example to subscribetest
- set subscribe topic as you set PublishTopic in CDP, for example to publishtest
Test MQTT Message Flow
Now you are ready to run and test this MQTT tutorial. For that :
- Run the system in CDP Studio and connect to it. Verify that Application Output pane shows a message similar to MQTTClient successfully connected to broker at 'xxx.cloudmqtt.com:20151'
- Run and connect your phone MQTT app
Now you can send MQTT messages like this:
- Change PublishTopic to some value and inspect that the value will appear on your phone subscribe topic
- Change publish topic to some value on your phone app and inspect that the value will appear on CDP system at SubscribeTopic
About Security of This Tutorial
Controlling signals over public Internet using MQTT (as in the tutorial above) is secure as long as you follow these guidelines:
- Always use encrypted transmission - then data (including userid/password) can not be eye-spotted by third-parties.
- Always use authentication (userid/password or client certificate authentication).
- Always firewall your CDP installation using your Internet router or separate firewall.
- Set up firewall not to allow incoming connection to your network. MQTT clients always connect to broker (never vice versa).
Note: For more security, you should also set up broker host certificate verification in MQTTClient. Doing that ensures the broker CDP is interacting with is actually the one you initially intended to interact with.
To set up broker certificate verification:
- obtain broker CA (certificate authority) certificate (in PEM format) and put it into application folder
- copy downloaded certificate into CDP application folder
- add the certificate to application in CDP Studio in Code mode right-clicking on application name and choosing Add existing files...
- configure MQTTClient to use the certificate file (property TLSBrokerCACertFile)