Q. How to make MQTT to work in your local network.
A. You need to have MQTT Broker installed on SBC or general-purpose computer.
In this example we will install Mosquitto on Windows platform but its the same for other system.
We will configure the mosquitto.conf so we could connect ex. Nano ESP32 to MQTT Broker.
# ======================================================================
# User Settings
# ======================================================================
allow_anonymous true
listener 1883
This allow any device on the local network to access the port 1883, if we need a more secure connection we need to
add more things to the mosquitto.conf but for this example its enough.
In Windows you could start Mosquitto Broker as Service, but I'm not sure it include the configure file. At least I didn't get
that to work (investigate). We could solve this by starting Mosquitto from terminal.
-> <PATH_TO_MOSQUITTO>\mosquitto.exe -c <PATH_TO_MOSQUITTO>\mosquitto.conf
Now we have started the Mosquitto Broker on your local computer. If you couldn't start it on windows platform make sure the Mosquitto Broker Service is not started, if so stop it. And try again.
Open OpenPLC and load the example MQTT_Send, change the Broker
test.mosquitto.org to your <IP_Local>
Optional change the topic message. Make sure you have Modbus TCP enabled
Now we will open MQTT Explorer to visual the message. We configure before we connect as you could see in picture below.
And click connect
We would now see the message Nano ESP32 is sending, meaning the IP of Nano ESP32 board is communicate on the local network to publish a message in interval of 500ms. We use MQTT Explorer to connect to Mosquitto Broker so we could debug the message.
So if we like to have 2 devices connected, we need to publish a message with topic on the first device and on the second device we subscript with topic. When the first device measure the temperature from a livingroom. The second device could subscript to topic sensors/livingroom/temp and use the information to trigger a event.
Raspberry Pi a very cheap general-purpose computer could be your MQTT Broker. Or use Docker...
I hope this explain how MQTT is working.