MQTT is a 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 be 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.
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 was 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 is wired to the home router, that is located in the same cabinet with AC contactors for end devices.
This main components were collected:
- 8 Channel DC 12V Relay Module with ULN2803 driver and 12V to 5V DC-DC converter installed;
- STM32F103C8T6 aka Blue Pill module;
- Wizchip Ethernet W5500 module;
- two 8-dip switches;
- prototype printed circuit panel (green colored about having metalized holes).
On the Windows PC (64 bit is preferred) this toolchain was installed:
In STM32CubeMX interface were configured STM32F103C8T6 Blue Pill module:
the serial interface,
UART interface,
SPI interface,
SPI prescaler,
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 changing 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 |
---|---|
SCLK | B13 |
SCS | B12 |
MOSI | B15 |
MISO | B14 |
3.3V | 3.3 |
GND | G |
The USB-UART adapter was connected to Blue Pill for debugging time with three wires:
The USB-UART adapter | Blue Pill pin |
---|---|
Tx | A9 |
Rx | A10 |
GND | G |
The ST-LINK V2 adapter was connected to Blue Pill for debugging time with four wires:
The ST-LINK V2 adapter | Blue Pill pin |
---|---|
3.3V | 3V3 |
SWCLK | SWCLK |
SWDIO | SWO |
GND | GND |
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 to increase the relay 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 was programmed in the sketch. Here "192.168".
Last two digits obtained by positions of mechanical switches (binary format). Here is 1 and 4. Then "192.168.1.4" is the IP of the MQTT broker.
Not only the IP, but the MAC of the IP-relay module must be programmed in the sketch. The MAC must be one generated uniquely.
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. Of course, any MQTT messages can be programmed similarly.
With the toolchain installed as described above, 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 utility with parameters 115200,N,8,1.
If no MQTT message is 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 a 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 these reloads.
During the time the IP-relay module starts, if a DHCP server will not respond for about 100 seconds, the module reloads again. This avoids hot connecting the IP-relay module to the router.
If the IP of the MQTT broker is changed in the home network, then the 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 his own 12V AC-DC power supply with a transformator for it.
Set up your own site - Go here