MQTT-driven IP-relay module

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.

Mobirise

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:

  • the MinGW was deployed in folder C:\MinGW. While installing under Basic Setup, were chosen packages mingw-developer-toolkit, mingw32-base, mingw32-gcc-g++, msys-base. Then Installation.Apply Changes.Apply was approved.
  • to the Windows PATH environment variable was added substring C:\MinGW\bin;C:\MinGW\msys\1.0\bin.
  • the GNU-ARM-GCC package was deployed with the checked on "Add path to environment variable" while installing. In that time the .2.1 version was installed.
  • as known bug of v.8 GNU-ARM-GCC package, file arm-none-eabi-objcopy.exe from C:\Program Files (x86)\GNU Tools ARM Embedded\8 2018-q4-major\bin folder was replaced on this one from GNU-ARM-GCC package v.6.
  • any text editor. As Geany (more lightweight), or VSCode (more functional for debugging).
  • STM32CubeMX as a graphical tool for configuration Blue Pill module.
  • STM32 ST-LINK Utility for flashing Blue Pill module.

In STM32CubeMX interface were configured STM32F103C8T6 Blue Pill module:

Mobirise


the serial interface,

Mobirise

UART interface,

Mobirise

SPI interface,

Mobirise

SPI prescaler,

Mobirise

one more GPIO pin for W5500,

Mobirise

GPIO pin for inner Blue Pill LED,

Mobirise

all other GPIO pins of Blue Pill as input (16 pins) and output (9 pins usable),

Mobirise

"Makefile" as Toolchain/IDE.

Mobirise

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.

Mobirise

This sketch with the Wizchip Ethernet W5500 ioLibrary was added in the directory STM32CubeMX had generated, and appropriate changes had been made in Makefile.

Mobirise

Wizchip Ethernet W5500 module was connected to Blue Pill with six wires:

W5500 pinBlue Pill pin
SCLKB13
SCSB12
MOSIB15
MISOB14
3.3V3.3
GNDG
Mobirise

The USB-UART adapter was connected to Blue Pill for debugging time with three wires:

The USB-UART adapterBlue Pill pin
TxA9
RxA10
GNDG
Mobirise

The ST-LINK V2 adapter was connected to Blue Pill for debugging time with four wires:

The ST-LINK V2 adapterBlue Pill pin
3.3V3V3
SWCLKSWCLK
SWDIOSWO
GNDGND
Mobirise

Blue Pill was connected to 8-dip switches as this wiring diagram.

Mobirise

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.

Mobirise

First two digits of the IP and the port number of the MQTT server was programmed in the sketch. Here "192.168".

Mobirise

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.

Mobirise

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.

Mobirise

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.

Mobirise

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.

Mobirise

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.

Mobirise

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.

A software you can load from this site is free for a non-commercial use except for components for which their owners declared their own rights. Can be limited by local laws. No of your data will be collected with a software you can load from this site.

Subscribe for updates.

Set up your own site - Go here