Question

how to setup mqtt

how to connect hmi divice to cloud server use by mqtt


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
September 29, 2024

1. Install and Configure an MQTT Broker

An MQTT broker is required to facilitate communication between the HMI device and the cloud server. You can use cloud-hosted brokers like Mosquitto, AWS IoT, Google Cloud IoT, or Azure IoT Hub, depending on your requirements.

Example with Mosquitto on a Cloud Server:

  • Install the MQTT broker (Mosquitto) on your cloud server:
sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients
  • Secure the Mosquitto broker by setting up username/password authentication and SSL encryption (optional but recommended for production).

2. Configure MQTT on the Cloud Server

Once installed, configure the Mosquitto MQTT broker. Edit /etc/mosquitto/mosquitto.conf to allow remote connections.

sudo nano /etc/mosquitto/mosquitto.conf

Add the following lines:

listener 1883  # Default MQTT port for non-SSL communication
allow_anonymous true  # For testing (disable in production)

Restart Mosquitto:

sudo systemctl restart mosquitto

3. Configure HMI Device for MQTT

Next, configure your HMI device to send data using MQTT. Many modern HMIs support MQTT out of the box. Here’s the general process:

  • Set MQTT Broker: On the HMI interface, specify the broker IP (cloud server’s IP address) and the port number (1883 by default or 8883 for secure connections).
  • Topic Subscription/Publishing: Define MQTT topics that the HMI will subscribe to or publish to. For example, the HMI might publish sensor readings or status updates to a topic like hmi/status or subscribe to commands sent from the cloud via cloud/commands.

4. Develop Cloud-Side MQTT Client

To interact with the HMI from the cloud, you’ll need to set up an MQTT client on your cloud server or application to send or receive messages. You can use libraries like Paho MQTT in Python, mqtt.js for Node.js, or any other MQTT client library depending on your development environment.

Example with Python and Paho MQTT:

Install the Paho MQTT client:

pip install paho-mqtt

Create a simple Python script to publish or subscribe to the HMI’s MQTT topics:

import paho.mqtt.client as mqtt

# Define the callback function
def on_message(client, userdata, message):
    print(f"Received message: {message.payload.decode()} on topic {message.topic}")

# Create an MQTT client instance
client = mqtt.Client()

# Assign the callback function
client.on_message = on_message

# Connect to the cloud MQTT broker (Mosquitto)
client.connect("your-cloud-server-ip", 1883, 60)

# Subscribe to a topic (e.g., commands sent to HMI)
client.subscribe("cloud/commands")

# Publish a message to the HMI
client.publish("hmi/status", "Status: Online")

# Start the client loop
client.loop_forever()

5. HMI and Cloud Interaction

Once the cloud server and the HMI device are both connected to the MQTT broker:

  • The HMI device can publish messages (such as sensor data or status updates) to specific MQTT topics.
  • The Cloud server can subscribe to those topics and process the data in real-time.
  • Similarly, the Cloud server can publish commands or data to specific topics that the HMI subscribes to.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more