Hacking on the Weirdest ESP Module

Sometimes I see a component that’s bizarre enough that I buy it just to see if I can actually do something with it. That’s the case with today’s example, the ESP-14. At first glance, you’d ask yourself what AI Thinker, the maker of many of the more popular ESP8266 modules, was thinking.

The ESP-14 takes the phenomenally powerful ESP8266 chip and buries it underneath one of the cheapest microcontrollers around: the 8-bit STM8S003 “value line” chip. Almost all of the pins of the ESP chip are locked inside the RF cage’s metal tomb — only the power, bootloader, and serial TX/RX pins see the light of day, and the TX/RX pins are shared with the STM8S. The rest of the module’s pins are dedicated to the STM8S. Slaving the ESP8266 to an STM8S is like taking a Ferrari and wrapping it inside a VW Beetle.

I had never touched an STM8 chip before, and just wanted to see what I could do with this strange beast. In the end, ironically, I ended up doing something that wouldn’t be too far out of place on Alibaba, but with a few very Hackaday twists: a monitor for our washer and dryer that reports power usage over MQTT, programmed in Forth with a transparent WiFi serial bridge into the chip for interactive debugging without schlepping down into the basement. Everything’s open, tweakable, and the Forth implementation for the STM8S was even developed here on Hackaday.io.

It’s a weird project for the weirdest of ESP modules. I thought I’d walk you through it and see if it sparks you to come up with any alternative uses for the ESP8266-and-STM8S odd couple that is the ESP-14.

Putting a Dumb Chip on the Net

The STM8S series of 8-bit parts are, well, cheap. Consequently they show up in all manner of commercial products where a bare minimum of microcontrollering is needed. And consequently, there are a lot of Chinese designers who are familiar with the chip, which is where things start making sense again. According to the ESP-14 datasheet (PDF mirror of a datasheet we downloaded from Watterott), the intended use is to provide WiFi connectivity to the diminutive STM8S, through UART and using the ESP8266’s default AT command set firmware. Which is to say that if you’ve already got a slightly fancy light switch design that uses an STM8, you’re just a few AT commands away from having a super fancy IoT light switch with the ESP-14.

And the ESP8266 modules all have an Achilles heel: the single ADC channel. So if you wanted to make an Internet-connected device that reads more than one analog value, you had to add your own multiplexer circuitry. Or use it in combination with a common, cheap microcontroller with a built-in ADC and some free pins. And thus, we conjecture, the bizarro ESP-14 was born.

Resources and The Plan

The STM8S provides multiple ADCs, and the ESP8266 brings WiFi connectivity and a lot of memory into the mix. I was trying to think up a project for this combination. At about the same time, my wife wanted a monitor that would tell her how the washer and dryer were doing without walking all the way down to the basement. Two ADCs, one for each appliance, would be necessary. Perfect. Can’t do that with an ESP-12!

ST has a fairly complete looking Standard Peripheral Library in C for the STM8 series that should make getting stuff done pretty easy. If you’re looking for other resources, The Way of the Register is full of good working examples for most nooks and crannies of the STM8S’s hardware. Compiling for the STM8S is supported by SDCC, and uploading code using cheap ST-Link dongles is supported by stm8flash. You’ll probably also want the datasheet and the reference manual.

dscf9179But this is a strange project, and that means a strange programming language. I’ve been playing around with Forth a lot lately, and [Thomas] ported a Forth environment to the STM8S over on Hackaday.io. If you’re not into Forth, it’s an interactive programming language that’s somewhere between Python and assembly, but with the syntax of an old TI calculator. It’s an acquired taste, but being interactive means that it requires a serial connection to program.

JeeLab’s esp-link is a great tool to have in your box. The software turns any bog-standard ESP module into a transparent WiFi-to-serial bridge, and it adds all manner of bells and whistles. These include providing the connected microcontroller MQTT and REST functionality over a SLIP interface. For this project, it is a perfect fit: the ESP8266 half of the ESP-14 module would let me call in to the Forth interpreter running on the STM8, and provide it with a “simple” means to send out the washer’s and dryer’s power usage to an MQTT broker. This means that I could build up the circuit, install it in the basement, and hack away from the comfort of my office. Almost.

Reset and Power

Programming in Forth, at least when I do it, means hitting the reset button a lot; hard crashes usually take down the interactive shell running on the microcontroller. Fortunately, esp-link provides a reset functionality from the web interface of the ESP8266, and it’s possible to remap this to the single exposed ESP8266 pin (GPIO0) from the esp-link’s web interface. The end result is that the STM8S can be reset by sending a POST request to the ESP: curl -XPOST "http://${ip}/pgm/sync" if you care. After a couple of seconds, all is well again.

power_meterAs shown in the schematic, there are jumpers everywhere in this project at the moment, but they’ve all come in handy, especially because the shared RX/TX lines make it hard to talk to one chip without the other overhearing or interrupting. For instance, to flash the ESP8266, you can disconnect power from the STM8, use the programming jumper to pull the ESP8266’s boot pin low, and then transfer in your software. To program the STM8, you can use either the SWIM interface, or disconnect the ESP8266’s power pin, switch up the TX and RX wires, and you’ve got a direct serial connection. And when running, with all the jumpers in place, everything works over the network. Sweet. If you wanted to build an ESP-14 breakout board, this would be a good place to start.

Other Hardware and Safety

I had two high-current current transformers in my junk box, so that was easy. Almost. Plugging in an old incandescent light bulb of known wattage, the transformer only put out a tiny signal, in the tens of millivolts. I added as many loops of thick copper wire from the appliances as I could fit into the beefy toroids, and that would just have to do. In the end, both the washer and dryer read about 500 mV peak-to-peak on the output of the transformers, so it’s easily readable with an ADC that uses a 3.3 V reference voltage, even if it’s not ideal. The right current transformers would help: they come in rated amperages if you buy them off the shelf. Plan ahead.

dscf9181The mains-voltage portion of this project is sealed up entirely inside an IKEA sandwich box for “safety”. Since this was my first PCB-based project that I’ve built since moving to 230 V, I actually looked up the regulations, and allowed a bit more safety margin: 1 cm on the PCB between mains and any other pads. Two extension cables were sacrificed and soldered onto the PCB to provide one connection to the wall and one each for washer and dryer.

The outputs from the current transformers, and the 3.3 V supply for the ESP-14 snake out of the box for the low-voltage logic circuits. This is the one part of the design that gives me pause; if the high voltage ever works its way into the low-voltage wires, the ESP-14 on the outside of the box might become hot. I separated everything with as much air-gap as I could, and applied liberal amounts of hot glue to keep it all in place. Still, I’ll treat this thing with respect when I have to handle it. As it stands, it’s tucked away behind the washer anyway.

Since the signal from the current transformers is (low-voltage) AC, and symmetric about ground, a capacitor and voltage divider put the offset back into a reasonable range for the STM8’s ADC. I did this on the same PCB as the high-voltage circuitry, but in retrospect I could have done all of the low-voltage signal processing outside of the box. The PCB was a convenient module for testing on the bench with some lightbulbs and my oscilloscope during the transformer-prototyping phase.


I wanted to do a peak detection in hardware with a diode and some capacitors, but the small voltage signal made that impossible, so I’m doing it in software.

A Load in the WasherA Load in the Washer

The wires that come out of the sandwich box carry a voltage that’s centered about halfway between zero and 3.3 V, and makes a 50 Hz approximate sine wave. The amplitude of this sine wave is proportional to the current flowing through the washer or dryer. I’m not interested in the actual RMS power as much as simply knowing when the washer or dryer are running, so a peak-detection algorithm would work just fine.

A 50 Hz signal is ridiculously slow for an ADC that can pull off tens of kilohertz without breaking a sweat, so there’s a lot of room for oversampling and averaging here, which is good because the raw signal is fairly noisy. I tried a number of schemes, but the simplest of them was to take a sixteen-value exponentially-weighted moving average, and keep track of the maximum and minimum averaged values over two periods of the AC power cycle. The difference between the max and min is a great proxy for how much power the appliances are using. This is the value that gets sent to the MQTT broker.


Since the esp-link software has an MQTT client inside, all that remains is to talk to it. It uses Serial Line Internet Protocol (SLIP), which might not be familiar to you if you haven’t done any dial-up networking since the 1990’s, but which couldn’t be simpler. In principle, you just tag a SLIP END character at the end of every packet. It can be nice to know when a packet starts too, so modern SLIPs simply put another END character at the beginning as well — packets of zero length are silently dropped. There’s also an escape and escaped versions of the escape and end characters, but I didn’t need them.

esp-linkEsp-link also uses a particular data format which is tailored to work well with C code and function pointer callbacks. It’s also got a CRC over the whole packet to reject conversations that accidentally look like a SLIP message. If you’re using C/C++ based code, including writing for Arduinos, there are libraries and examples that make it simple to communicate with MQTT/REST servers or over generic UDP and TCP sockets, and all this at the same time as it’s running the WiFi bridge.

To sum up the MQTT story, I needed to set up the data packets to match what the esp-link wanted, compute a CRC on the data, and then wrap it in the SLIP END characters. The STM8 then simply sends this out to the serial port at 115,200 baud and everything is groovy.

The complete firmware is divided up across a few Forth files. Have a look if you’re interested. The mqtt.fs file and power_meter.fs files have almost all of the user-useful vocabulary, and the rest is support. The end result is that something like mqtt.preamble washer.topic 42 message.value qos.and.retain send sends the number 42 (in ASCII) to the broker using the washer’s MQTT topic, quality-of-service level 0 and no retain flag.


Wrapping the project up is a display that alerts us when the washer or dryer is done. For the moment, it’s a simple MQTT client built on a boring ESP-12 module that reads the washer and dryer topics from the broker and displays the data as a colored bar graph on a strip of WS2812 LEDs. It’s essentially a multi-LED version of the display node that I built up for this column on using MQTT with NodeMCU.

Inventions and Dimensions

The whole point of this project was to do something weird with a weird part. To that end, I think it’s a mixed success. The final device is quite pedestrian in this age of the connected home: a dual-channel power monitor that reports to the (local) cloud. But under the hood, it’s ridiculous.

asciinema_thumbHalf of that ridiculousness is thanks to [Thomas] for his STM8 Forth. And with the esp-link doing the heavy WiFi lifting as well as providing a remote reset, it’s a dream to work on the code remotely. In case you’re not sure how ridiculous this is, click that image to the right to watch me log in to the STM8, send arbitrary values, and then put it back into its default monitoring mode.

Of course you could just use the ESP-14 as it was intended: a pre-compiled C firmware running on the STM8 and using the stock AT-command firmware on the ESP8266. You could probably get exactly the same device built in just about the same amount of time, if you were familiar with the STM8 libraries. You’d have to write the MQTT protocol bits yourself, but that’s not actually all that hard for simple messages like these.

But there’s just an extra little bit of geek satisfaction in taking a module that makes very little sense, reflashing both chips inside, and cobbling together something odd but functional. I can telnet into a fifty-cent microcontroller in my basement and ask it how much power the washer is using. On port 23! How cool is that?

In truth, I hacked this together in a few hours of time spread out over a week, and just got it up to the minimum viable product phase. Using the setup over time will tell what hard edges need sanding down and what just doesn’t work. The real point of this is that I got to use the strangest ESP module that I’ve ever seen, and that although it is ridiculous, it can be useful. What would you do with a tiny little microcontroller strapped to an ESP8266 module?

This entry was posted in home hacks, IoT, mains, mqtt, power, stm8s, transformer, wireless hacks. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.