This is only a preview of the January 2024 issue of Silicon Chip. You can view 35 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "Raspberry Pi Clock Radio, Pt1":
Items relevant to "WiFi Relay Modules":
Items relevant to "USB to PS/2 Keyboard Adaptors":
Items relevant to "Secure Remote Switch, Pt2":
Items relevant to "Multi-Channel Volume Control Part 2":
Purchase a printed copy of this issue for $12.50. |
Feature by Tim Blythman
WiFi Relay Modules
Connecting a microcontroller to a WiFi network is something we almost
take for granted today, but 10 years ago, it was more expensive and
difficult. This article examines two relay modules based on an ESP-01
module that can be controlled remotely over WiFi.
T
he Espressif Systems ESP8266 is a
32-bit microcontroller incorporating a WiFi radio. Initially, it came with
firmware that included a TCP/IP stack.
It could be controlled via a serial interface that allowed commands to be sent
similarly to an old Hayes-compatible
phone-line modem.
The ESP-01 is a minimalist standalone ESP8266 module that we
reviewed in April 2018 (siliconchip.
au/Article/11042). We also used the
ESP-01 to create the Clayton’s GPS
Time Source (April 2018; siliconchip.
au/Article/11039). Its relatively simple
circuit is shown in Fig.1.
It wasn’t long before it became possible to program the various ESP8266
modules directly. The possibility of
doing this with the Arduino IDE, and
later the Python language in the form
of MicroPython, meant that working
with WiFi suddenly became very easy.
Indeed, the ESP8266 is one of the
main reasons the Arduino IDE has
been updated to support so many different processor architectures and
board types.
The two WiFi relay modules covered in this article are based on the
ESP8266 processor and both contain
a removable ESP-01 module. That
means both are programmable with the
Arduino IDE, among other methods.
They both come loaded with functional firmware, which means that
they can be used without having to be
programmed. We’ll look at their design
and operation, then describe how they
can be controlled. We’ll also look at
the benefits of reprogramming them.
Why WiFi?
There are numerous possible applications for a WiFi relay, especially for
things like home automation, as WiFi
networks can easily cover the average
home (or be expanded to do so).
While the relays on both modules
are rated for switching mains, you
should not use them to switch mains
directly. That’s because the modules
are so compact that it’s impossible to
ensure safe separation of the mains
Fig.1: the ESP-01 module circuit is pretty simple, with the ESP8266 IC being connected to an antenna, crystal, serial
flash memory chip (IC2), power LED, plus 8-pin connector CON1 for power and communications.
44
Silicon Chip
Australia's electronics magazine
siliconchip.com.au
Fig.2: the Z6427 has about the minimum circuitry needed for an ESP-01
module to control a relay. Several pullup resistors set the correct operating
mode for the microcontroller, and a power LED and a reset button are
provided. A high-side PNP transistor drives the relay. The module has no
onboard voltage regulator and requires a 3.3V supply.
and low-voltage parts of the circuit.
You could easily use them to trigger
a safer external mains relay, though.
On the other hand, out-of-the-box,
they are ideal for controlling things
like low-voltage (eg, 12V LED) lighting, DC motors and other decorative
applications.
The Altronics Z6427
The Altronics Z6427 is a compact
module measuring 36 × 24 × 16mm.
It has a 4-pin right-angle header overhanging one end and a three-way
screw terminal at the opposite end.
The ESP-01 module sits over the relay
and is attached to the module using a
2×4 pin socket.
It’s very neatly laid out and there
are mounting holes in each corner.
Fig.2 shows the schematic diagram
of the module. As you can see, there
is not much to it. The header has connections for 3.3V power, ground and
serial UART lines.
These four pins connect directly to
their corresponding pins on the ESP01 module. A red LED indicates when
3.3V power is applied, while the tactile switch can be used to reset the
microcontroller on the ESP-01 module. Some of the ESP-01’s pins are
pulled up to 3.3V by either 1kW or
4.7kW resistors.
One of the ESP-01’s I/O pins, GPIO0,
drives a high-side PNP transistor.
When GPIO0 is driven low, the transistor conducts and powers the relay
coil. A diode is provided to quench
the coil voltage generated when the
transistor switches off. The relay has
3A-rated contacts.
The module was designed by Keyestudio, and more information about
siliconchip.com.au
the module can be found at (including a link to download a binary image
of the firmware and software tools):
siliconchip.au/link/abpv
Firmware
The firmware tries to connect to an
access point named “KeyesWifi_S”
(with the password “KeyesWifi”) if
such an access point is present. If that
doesn’t work, after a short while, the
firmware on the Z6427 sets up a WiFi
access point called “KeyesWifi_A”,
with the same password.
In either case, the firmware opens
TCP port 8080 for incoming connections. The relay contacts will close if
the string “PIN00=0” is received on
that port. If “PIN00=1” is received, the
relay contacts will open. This corresponds to the inverted logic that the
circuit presents.
The GPIO0 pin (Pin 5) that is used
to drive the relay is also used to set
the boot mode of the processor; that is
probably the reason for the somewhat
unusual PNP transistor drive circuit.
This pin is also driven low as the processor boots, causing the relay to close
briefly. Such behaviour may not be
desired in your application!
The Keyestudio web page also provides a “NetAssist” Windows PC program that can be used to test the module's operation. We have also written
some Arduino sketches that can be
used to test and control the relays,
The Altronics
Z6427
WiFi Relay
Module is
compact, with
mounting
holes being
a handy
touch. The
detachable
ESP-01
module sits
over the 3A sugarcube-sized relay. 3.3V power and
ground can be connected at one end,
with the relay contacts available at
the other end.
which will be described shortly.
Since the header on the Z6427 also
carries serial data lines, we hooked up
a serial-USB adaptor to see if anything
was being sent. Fig.3 shows how you
can connect this module to a CP2102
USB-serial module. There is diagnostic data at the unusual rate of 74,880
baud, which can be seen in Screen 1.
The Jaycar XC3804
The Jaycar WiFi Relay is a bit larger
at 45 × 28mm and has a more complex
circuit; in fact, there is another microcontroller on the main module, aside
from the ESP8266 on the attached ESP01 module.
Fig.4 shows its schematic. There are
three external connections equivalent
to those on the Altronics unit. A threeway screw terminal presents the relay
contacts, while a four-way pin header
provides serial data and power, in this
case, 5V.
Another two-way screw terminal
parallels the 5V and ground connections, which may be preferred for some
applications. The XC3804 also hosts
an AMS1117 3.3V regulator to power
the 3.3V ESP-01 module.
The Jaycar unit uses a low-side NPN
transistor to drive the coil of a 5V relay.
There is also a quenching diode. An
indicator LED and its ballast resistor
are in parallel with the coil, so the LED
illuminates when the coil is powered.
This relay has 10A contacts.
Fig.3: connecting the
Z6427 to a CP2102 USBserial converter module
allows the diagnostic
boot data to be viewed at
the unusual baud rate of
74,880.
Australia's electronics magazine
January 2024 45
◀
Screen 1: the Z6427’s data includes
information about the access point
it creates, as well as its progress in
connecting to other access points.
Screen 2: the XC3804 produces data
about the access point and URL you
need to connect to. The accented
characters are actually binary
commands to the STC15F104W chip
that are also echoed to the external
serial lines.
Interestingly, the transistor is controlled by an 8-pin STC15F104W
microcontroller. This micro is powered from the 5V rail and is also connected to the serial UART lines of the
ESP-01 and the four-way header.
There are unofficial reports that the
ESP8266 processor has 5V-tolerant
inputs, allowing the direct connection
of the nominally 3.3V ESP8266 to a 5V
microcontroller. The ENABLE pin of
the ESP-01 is pulled up to 3.3V, and
our module had several unpopulated
component footprints too.
The data sheet for this module
includes the Arduino source code
(siliconchip.au/link/abpw). The code
is straightforward and contains elements from Arduino example sketches.
The XC3804 creates an open access
point named “Duinotech WiFi Relay”
and also sets up a DNS responder for
the “relay.net” hostname. This means
that the Relay can be accessed via this
host name as well as its IP address.
There is also a web (HTTP) server
hosting a page that provides a pair of
links to control the relay remotely. The
links point to the URLs relay.net/open
and relay.net/close
According to both the source code
and the behaviour we saw, the “open”
command sets the transistor’s base
high, energising the relay, while
“close” de-energises the relay. That is
opposite to what we expected.
Otherwise, the XC3804 worked as
expected and was perhaps slightly easier to operate due to its inbuilt HTTP
server rather than a raw TCP server.
Despite the extra microcontroller, the
relay on the XC3804 occasionally chattered when powered on but less often
than the Z6427.
Also, the relay status LED (as fitted
to the XC3804) is more useful than the
power LED on the Z6427; the ESP-01
module already has a tiny red LED that
lights up when it is powered.
This module can be wired up to a
CP2102 USB-serial module, as shown
in Fig.5. There is little diagnostic
data available from the XC3804, apart
from an instructional boot message at
9600 baud, shown in Screen 2. Further binary data (the line of accented
characters) is sent whenever the URLs
are requested. This data appears to be
the commands to the STC15F104W for
it to drive the relay.
Demonstration software
We have written software demonstrating how to control these modules over WiFi. Naturally, we needed
a WiFi-capable microcontroller, and
we have chosen to use the Pico W as
it can be programmed with either the
Arduino IDE or with BASIC using the
WebMite firmware.
Since the Pico W’s UF2 firmware
files are easy to upload, we have also
provided those as downloads, so you
can try out our examples without even
having the Arduino IDE installed. You
will just need a serial terminal program, such as TeraTerm on Windows
or Minicom on Linux.
For the Arduino IDE, we’ve used
the arduino-pico board profile version
3.1.0 from siliconchip.au/link/abpx
Some of the Arduino sketches have
also been tested to work with the D1
Mini ESP8266-based boards.
Fig.4: the XC3804 includes an AMS1117 voltage regulator, so it will work with a 5V DC supply. It has a second
microcontroller in addition to the ESP8266 on the ESP-01 module, which receives commands over a serial pair and
activates the relay via a standard low-side NPN transistor arrangement.
46
Silicon Chip
Australia's electronics magazine
siliconchip.com.au
Screen 3: our basic Z6427_CLIENT
demo software for the Z6427 connects
to its access point and can control the
relay by sending appropriate data
over the WiFi network.
There are three Arduino sketches
for the Z6427 and one BASIC program.
There is also an Arduino sketch for
the XC3804. There are some limitations to the WebMite WiFi interface
that mean there are some things we
cannot do with it.
For each example, you can load the
UF2 file by pressing the white BOOTSEL button on the Pico W while connecting it to a computer. After that,
copy the appropriate UF2 file to the
RPI-RP2 drive that appears and connect to the virtual USB-serial port with
your terminal program.
You could also compile the sketches
with the Arduino IDE. Note that Arduino and the WebMite firmware use different implementations of the virtual
USB-serial port, so the port name or
number might differ (for the same Pico
W) depending on which firmware is
loaded.
Z6427 remote control
There are three versions of the Arduino demo software for the Z6427. One
version (Z6427_CLIENT) behaves as a
The XC3804 has
screw terminals
for power and
relay contacts,
plus a separate
header
for power
and serial
communications.
It is larger than the
Altronics unit and does
not have mounting holes but the
onboard relay is rated for 10A.
siliconchip.com.au
Screen 4: this second version of the client software can scan and connect to
different Z6427 Relays. It can be pretty slow, as switching between the access
points each Relay provides takes some time.
WiFi station and tries to connect to the
access point on a Z6427.
When it does, it prints its IP address.
The Relay can be controlled by typing “0” or “1” into the serial terminal;
Screen 3 shows the typical output.
Sometimes the connection does not
work immediately, so you may need
to wait up to a minute for the station
to connect to the access point.
It also appears that the Z6427
does not always start its access point
(“KeyesWifi_A”) until it has decided
that it can’t connect to any other access
points (“KeyesWifi_S”).
The Z6427_CLIENT_V2 sketch (or
UF2 file) is designed to allow control of more than one Z6427. This
sketch scans for networks with the
“KeyesWifi_A” name and allocates
them a letter code (A, B, C etc). Screen
4 shows its output.
Entering the letter code will connect
to the appropriate Relay, after which
“0” and “1” will switch the specific
Relay, like the previous sketch. Note
that this sketch is very slow to switch
between Relay access points, so it will
Fig.5: the XC3804 communicates at
9600 baud, and you can see some
brief debugging data output and the
commands to the STC15F104W that
drives the relay.
Australia's electronics magazine
not be suitable for practical uses of
those Relays.
The Z6427_AP_CLIENT demo operates as an access point and allows
Relays to connect to it; its serial output is shown in Screen 5. The “0” and
“1” commands are pushed out to all
Relays that connect.
Unfortunately, there isn’t an easy
way to tell the relays apart (eg, by
querying their MAC addresses) from
within the Arduino code. Like the previous sketch, this version is impractical for anything but demonstration
purposes, but might be handy to show
how the Relays operate in these configurations.
WebMite BASIC
The current version of WebMite
BASIC (5.07.07 at the time of writing)
has some limitations that mean it is not
possible to provide as many examples.
Screen 5: the Z6427_AP_CLIENT
sketch provides an access point to
which the Z6427 Relays can connect.
It shows the connected stations and
sends out the same command to all
the Relays it detects.
January 2024 47
In particular, the WebMite cannot be
configured to work with an open WiFi
network, meaning that it is impossible to use it to communicate with the
Jaycar XC3804, which only offers an
open WiFi access point.
The WebMite cannot be an access
point, so we cannot create an equivalent to the Z6427_AP_CLIENT
sketch. Also, the access point to
which the WebMite connects is fixed
as an OPTION, so it cannot be easily changed at runtime; that rules out
a BASIC program like the Z6427_
CLIENT_V2 sketch.
So, our sole BASIC example for
the WebMite connects it to a single
Z6427 access point and allows remote
control of the relay with “0” and “1”
keystrokes. You can break out of the
program with Ctrl-C if you want to
modify it.
This can be loaded by downloading
the Z6427.UF2 file to a Pico W. The
output is shown in Screen 6. Note that
because all Z6427s use the “KeyesWifi_
A” access point, this UF2 file has
OPTION WIFI set to use that access
point name, so it should just work.
Software for the XC3804
The XC3804 only creates an access
point and does not have the option to
connect to other access points, so there
is only one Arduino example for it,
named XC3804_CLIENT. It works in
much the same fashion as the Z6427_
CLIENT software and connects to the
Relay.
You can then control the relay over
its serial port by sending “0” or “1”. As
expected, the logic is reversed, so “0”
will energise the relay (and the LED
will come on), while “1” will power
off the relay. Screen 7 shows the serial
data produced by this sketch.
Improved firmware
These two relays have handy features but could benefit from some
improvements. In particular, neither
can connect to a pre-existing WiFi network, which is what we expect most
people to do, especially if they wish
to interact with devices on the wider
internet.
The Z6427’s habit of toggling the
relay as it powers up might be sufficient to rule it out of some critical
Screen 6: we also created a version of
the Z6427_CLIENT software in BASIC
for the WebMite, which runs on the
Raspberry Pi Pico W hardware.
Screen 7: like the Z6427_CLIENT
software, XC3804_CLIENT connects
to the access point that the Relay
creates. Since the XC3804 uses the
HTTP protocol, it can also be operated
using a computer and browser.
48
Silicon Chip
Screen 8: our DUPLEX_RELAY_
FIRMWARE_MDNS firmware can
be loaded onto the Z6427 or XC3804
Relays to improve the interface. Our
firmware serves up the web page
shown here and allows it to connect
to an existing WiFi network, such as a
home access point. Other information
shown allows the Relay to be uniquely
identified for later use.
Australia's electronics magazine
applications, but otherwise, its numerous interfaces are pretty handy.
The XC3804’s HTTP interface is
very easy to use, particularly for testing purposes, as any computer with a
browser can operate it. The lack of a
secure access point means it can’t be
used with the WebMite.
With these aspects in mind, we
decided to write our own firmware to
work with both module types.
Our Clayton’s GPS Time Source article had details about wiring up the
ESP-01 module for reprogramming.
We have reproduced the figure from
that article (Fig.6 here) as it shows the
critical details of how to connect the
USB/serial adaptor to the ESP-01 via
a breadboard for programming.
Our improved firmware was also
written with the Arduino IDE; the
sketch is named DUPLEX_RELAY_
FIRMWARE_MDNS. We have also
exported a BIN file you can program
directly into the ESP-01 (or other
ESP8266 board) with the free ESPFlashDownloadTool software.
The ESPFlashDownloadTool software can be downloaded from:
siliconchip.au/link/abpv
That link is also provided on the
Altronics Z6427 product page. The
BIN file (and any other BIN files for
ESP8266 boards) should be programmed to address 0x000000.
Altronics sells the ESP-01 module
separately as the Z6360 (siliconchip.
au/link/abpy), so you can experiment
with this without modifying the ESP01 module that comes with the WiFi
Relay Module if you prefer.
This firmware provides the same
outputs as expected by the Altronics
Z6427 and Jaycar XC3804 Relays, so
an ESP-01 module programmed with
this firmware can be used in either.
Simply remove the original ESP-01
and replace it with one programmed
with our firmware.
Briefly, the updated firmware adds
interfaces to allow it to connect to
a specific WiFi network. That will
enable the relay to connect, for example, to your home WiFi network. Naturally, the selected access point is saved
for automatic connection in the future.
Some basic diagnostic data is now
available via a serial terminal at 9600
baud. This baud rate is necessary to
match the rate used by the ESP-01
when it communicates with the second microcontroller on the XC3804
module.
siliconchip.com.au
Fig.6: this is how you can connect a USB/serial adaptor to an ESP8266 module to reprogram it. The breadboard is
mainly needed so you can connect the required pull-up resistors.
Like the Jaycar XC3804, an open
access point is created, this time with
the name “relay”. A DNS server means
you can browse to http://relay.setup
to easily access the configuration.
Screen 8 shows the web page that is
displayed.
You can test the operation of the
Relay by using the OPEN and CLOSE
buttons on the web page. You can also
set the WiFi SSID and password using
the text entry boxes. The information
at the bottom of the page includes the
IP address of the Relay once it has connected to another network.
The HOST and MDNS fields are
unique names based on the unique
MAC (hardware) address of the ESP01 module. They can be used later to
identify each Relay as they should
never change, even if the IP address
changes.
A password can be entered in the
LOCK password field to prevent the
SSID and password from being modified by someone accessing the Relay’s
access point. Re-entering the LOCK
password will unlock the Relay.
Like any such application, physical access to the relay means that
any security measures can be broken, such as by reprogramming the
module or reading out data from the
flash memory. So we don’t claim that
the Relay is invulnerable to security issues, but this small measure
should help.
The same page is also served up
when the Relay has connected to
another access point, so you should
be able to check operation by browsing to the IP address shown (while
connected to the programmed SSID)
and confirming that you see the same
host address and that the Relay can be
controlled in the same fashion.
siliconchip.com.au
To configure multiple Relays, you
should power on each in turn. When
each one comes up, access its “relay”
access point and configure it to access
your preferred WiFi network. Note the
IP address and HOST/MDNS fields,
then set the LOCK password and
power off the Relay before configuring the next.
Depending on your access point’s
settings, the IP addresses might
change, but the HOST/MDNS will not.
You can then access the Relays via the
following client software.
Client control
A functional test can be made using
the DUPLEX_AP_CLIENT sketch. This
connects to the “relay” WiFi network
and accesses the http://relay.setup
page to control the relays. It is controlled from a serial terminal.
It works in the same fashion as the
XC3804_CLIENT software seen in
Screen 7. Indeed, it is much the same
code-wise apart from the different
access point and web page addresses.
For a more comprehensive control program, use the DUPLEX_STA_
CLIENT_WEBSERVER sketch. It also
has a serial control interface, allowing
it to connect to your WiFi network and
scan for Relays.
On start-up, the sketch scans for
networks and prompts you for a password to allow a connection to your
home network. This network is saved
in emulated EEPROM for future use.
You will then see a menu like Screen 9.
Both the DUPLEX_RELAY_FIRMWARE_MDNS and DUPLEX_STA_
CLIENT_WEBSERVER sketches implement the mDNS (multicast domain
name server) protocol. The Relays are
identified by their MDNS names which
are displayed in their individual configuration web pages.
After a Relay scan (triggered by the
“Y” command), any Relays found are
saved to emulated EEPROM and can be
selected by choosing their letter code
(A, B, C etc). They can be operated by
typing “0” or “1”.
The DUPLEX_STA_CLIENT_WEBSERVER sketch also serves up a web
page at the IP address that it prints on
the serial terminal. Screen 11 shows
a typical display, which, as you can
Screen 9: the DUPLEX_
STA_CLIENT Arduino
sketch provides a
much more advanced
control interface. The
mDNS protocol allows
other Relays to be
found by scanning,
and individual
Relays can be saved
and controlled
independently.
Australia's electronics magazine
January 2024 49
The Altronics
Z6427 (left) and
Jaycar XC3804 (right)
shown enlarged for clarity.
see, will allow you to scan and control
Relays on your local network.
BASIC code
We’ve also provided a BASIC version (for the WebMite) of this sketch.
It is called DUPLEX_STA_CLIENT
and works like its Arduino equivalent, although it lacks the web server
interface.
The UF2 can be loaded onto a blank
Pico W to turn it into a WebMite
already programmed with this software. However, you will still need to
manually configure the OPTION WIFI
parameter to connect to your preferred
network at the command prompt.
Since WebMite BASIC does not
implement the mDNS protocol, it has
to work slightly differently. It accesses
the web page that the Relay generates
and looks for the “MDNS:” text to
extract the unique identifier.
We recommend noting the IP
addresses and then using the “V”
command to check the relay at that IP
address. We’ve included a scan (“Y”)
routine, but it is very slow and does
not always work.
Screen 10 shows the output from the
WebMite BASIC program. It can store
Relays to non-volatile memory and
then control them by typing a letter
(A, B, C etc) and “0” or “1”.
Fixes
We tried adding a capacitor to see
if we could eliminate the relay toggling while the WiFi Relay Modules
are booting. The capacitors are fitted
between the base and emitter of the Q1
transistor in each case. For the Altronics Z6427, around 470μF was required,
while the Jaycar XC3804 only required
10μF. Watch the polarity if you try this
with electrolytic capacitors.
Conclusion
Our updated firmware offers significantly improved options for controlling these WiFi Relays, especially
as it allows them to connect to a known
WiFi network. This simplifies applications where you already have devices
connected to an existing network.
There is still the limitation of the
Altronics Z6427 that the relay contacts close briefly when power is
first applied; the Jaycar XC3804 also
appears to do so occasionally.
For these reasons, we can’t suggest
these Relays for interfacing with things
Screen 10: although
WebMite BASIC does
not support the mDNS
protocol, our DUPLEX_
STA_CLIENT BASIC
program provides similar
features (apart from
scanning) to the DUPLEX_
STA_CLIENT Arduino
sketch.
50
Silicon Chip
Australia's electronics magazine
Screen 11: the HTTP web server
incorporated into the DUPLEX_STA_
CLIENT_WEBSERVER sketch displays
a page that allows you to configure
and control other WiFi Relays. That
means you don’t need to use a serial
terminal apart from the initial setup.
like automatic gates and garage door
openers. A power outage might result
in the garage door receiving a spurious open command in the middle of
the night!
Still, they would be great for controlling low-voltage lights and other
decorative applications. They would
probably be fine for uses where safety
or security is not a concern.
All the software we have written
for these Relays is also available in
compiled form, so you don’t need
the Arduino IDE to try them out. For
example, we have UF2 files that can be
loaded directly onto a Pico W. These
are available for all Arduino sketches
(except the updated Relay firmware,
which is not intended for the Pico W).
Our serial control is simple and
intended to demonstrate how these
devices operate. We expect many readers will add interfaces such as buttons
and sensors to automate the operation
of the Relays further. Having said that,
the web page interface might be sufficient for some readers.
As well as the UF2 files for our Pico
(Arduino or WebMite) programs, some
of the Arduino sketches have also
been exported as BIN files, which can
be programmed into ESP8266-based
boards or modules for testing. We
used a D1 Mini for these tests as it has
a built-in USB-serial interface. Jaycar
sells it as XC3802.
Both Relay modules are available for
$17.95 at the time of writing.
• Altronics Wi-Fi ESP8266 Relay
Module For Arduino: siliconchip.au/
link/abpz
• Jaycar Smart Wi-Fi Relay Main
Board module: www.jaycar.com.
au/p/XC3804
SC
siliconchip.com.au
|