MQTT is perfect lightweight protocol for managing home electronic devices. The only thing that is not very convenient is the need to know the IP of the MQTT broker for all MQTT clients. Meanwhile this IP may well change in a home router (being a router will replaced, in case of a failure with a router, or if a home NET's topology will be changed), or in case of the replacing a MQTT computer with a new one.
Off course, it’s not very difficult to change the IP of the MQTT broker within MQTT clients codes (talking about DIY devices), but if this device been deployed a long time ago, it’s not so easy to recall all program code for making changes and restore all programming toolchains.
Here is described the MQTT-driven IP-relay module, on which the variable part of the MQTT broker’ IP address is set by mechanical switches, and in case of changing this address it is not necessary to change the program of the relay module. This IP-relay module wired connected to the home router, that is located in the same cabinet with AC contactors for end devices.
On the Windows PC (64 bit is preferred) this toolchain was installed:
In STM32CubeMX interface were configured STM32F103C8T6 Blue Pill module:
the serial interface,
one more GPIO pin for W5500,
GPIO pin for inner Blue Pill LED,
all other GPIO pins of Blue Pill as input (16 pins) and output (9 pins usable),
"Makefile" as Toolchain/IDE.
The STM32CubeMX generated code contains sections designated for appending a custom user code. It’s a good idea to write custom code strictly in these sections, which will allow to change the settings through the STM32CubeMX without affecting the custom user code.
Wizchip Ethernet W5500 module was connected to Blue Pill with six wires:
|W5500 pin||Blue Pill pin|
The USB-UART adapter was connected to Blue Pill for debugging time with three wires:
|The USB-UART adapter||Blue Pill pin|
The ST-LINK V2 adapter was connected to Blue Pill for debugging time with four wires:
|The ST-LINK V2 adapter||Blue Pill pin|
Blue Pill was connected to 8-dip switches as this wiring diagram.
Blue Pill was connected to ULN2803 chip of the 8 Channel DC 12V Relay Module as this wiring diagram.
It is possible to use a multiplexor for increase the relays number, as 74HC4067, 74LS154, etc, but only one relay per time can be managed in this case.
First two digits of the IP and the port number of the MQTT server had programmed in the sketch. Here "192.168".
Last two digits obtains by positions of mechanical switches (binary format). Here is 1 and 4. Then "192.168.1.4" is the IP of the MQTT broker.
As this module intended for the management of DIN AC relays with the electric latching,
the "IPRelay/Pulse" MQTT message was programmed within the sketch. Off course, any MQTT messages can be programmed similarly.
With the toolchain installed as above described, only need to "make" in the command line in the directory where the sketch has placed.
While compiling, some warnings were about the code of Wizchip's ioLibrary, but this is no problem.
Once the sketch being compiled, file iprelay.hex was flashed within STM32F103C8T6 using STM32 ST-LINK Utility.
That's all. Now the IP-relay module can be connected to a home router with a patch cord and he can operate 8 devices in response to MQTT messages.
IP-relay's debug/telemetry messages is observable via a COM-port's utilite with parameters 115200,N,8,1.
If no MQTT message will received once per 4 hour by the IP-relay module, this will mean that a MQTT broker abandoned or home network has gone out of action. In this case the IP-relay will reload with new DHCP request to the router. The MQTT message "IPRelay/Ping" to the IP-relay module with any payload data can be used periodically to avoid this reloads.
During the time the IP-relay module starts, if a DHCP server will not responds about 100 seconds, the module reloads again. This avoid hot connect the IP-relay module to the router.
If the IP of the MQTT broker be changed in the home network, then new IP can be typed with two 8-positions mechanical switches on hot. New IP will be accepted within 4 hours while the IP-relay module is waiting for a MQTT message, or immediately after the IP-relay module is switched off/on.
Since this device has relays on board, it's a good idea to have own 12V AC-DC power supply with a transformator for it.