Culture / Edge / IoT

Off-The-Shelf Hacker: Microcontroller Wireless Messaging with MQTT

9 Mar 2019 6:00am, by

My original plan for building a backyard sensor was to use an ESP8266 model 01 wireless transceiver and matching relay board along with a passive infrared (PIR) device to detect warm bodies as they slink by. I even ordered a single-channel relay board that mounts the tiny ESP8226 and a USB programmer that could be used for flashing firmware.

Alas, I haven’t been able to get the USB programming board to work, so putting firmware on the 01 microcontroller is a pain. I also forgot that the 8266-01 only has two semi-usable general-purpose I/O (GPIO) pins. One goes to the relay and the other is used for resetting the processor or some such thing.

The slightly larger NodeMCU board turned out to be a better choice for an outdoor sensor device. The board has plenty of pins and is easy to program using just a USB cable. They go for less than $5 each.

We covered hooking up the PIR sensor to the NodeMCU wireless module last week. This week we’ll look at the code, which now incorporates MQTT messaging and a simple workaround to use the single-channel relay board (designed for the ESP8266-01) with the NodeMCU.

Coding Particulars

The code starts with the usual run-of-the-mill variables, library setup and initializations. We only needed the libraries for the 8266-specific WiFi and MQTT client (PubSubClient) to make this code work.

Next comes connecting to my wireless access point. Of course, you’ll use your own SSID, username and password. WiFi.begin() function starts the wireless network access.

After this comes a callback. It toggles the D1 (GPIO 16) pin on and off according to the messages received by the MQTT client code, that’s initialized in the setup part of the program. Whenever a message comes into the MQTT client, the program breaks out of the main loop and executes the code in the callback. Upon completion, it then jumps back to wherever it was and continues. Switching the relay is done by publishing either a “0” or a “1” to the “inTopic” topic on the MQTT broker.

Naturally, an MQTT broker needs to be running on the network, as well as the MQTT client on the NodeMCU for the whole thing to work. I used the Mosquitto server (broker) on my Linux notebook for testing. At some point soon, I’ll install a permanent MQTT broker on a spare Raspberry Pi clone to route messages for the sprinkler controller and the yard sensors.

Handling the PIR sensor input and sending the MQTT message out was the most challenging part of the project.

The PIR sensor works by actually comparing two internal heat sensors against each other. As an object moves in front of the device, the first sensor will pick up the heat from the human or animal. A split second later, the second sensor will also detect the heat and the electronics will register the difference between the two sensors. There is a plastic lens on the front of the device to widen the field of view. As a result, you’ll get a quick succession of 5 or 10 “hits” as something moves in front of the sensor. The 8266 chip is certainly fast enough to pick up ALL those hits.

We definitely don’t need to detect and publish 10 or 20 MQTT messages each time the PIR sensor trips. We just need one message every couple of seconds.

My solution is to publish the initial “somebody is here” MQTT message, then measure the time between successive sensor “hits.” If we receive another input within a certain time, in this case, 500 milliseconds, just ignore it and keep looping until no new inputs are detected. Then wait for the next “hit” and go through the timing loop, again over and over.

During this whole process, we might also receive a message to actuate the relay. I arbitrarily chose a “2” for ON and a “0” for OFF. The relay will power a couple of 120-volt floodlights. The floodlights will turn on upon receiving a “2” from the MQTT broker.

With the code working, let’s look at the relay board.

Hacking the Relay Board

I puzzled and puzzled about how I could use the 8266-01 modeled relay board with the NodeMCU. Then it hit me. Simply wire one of the NodeMCU pins in place of the 01 device. Hello… that’s why we call this the off-the-shelf hacker column.

The relay board is shown below.

Single-channel relay board for ESP8266-01

We’d normally plug an ESP8266-01 board into the yellow connector, after updating the firmware. Instead, I just ran a jumper from pin D1 (GPIO 16) on the NodeMCU board to pin 0 on the yellow connector. This corresponds to GPIO0 on the ESP8266-01 board. The relay logic is hooked up to the GPIO0 pin. Looking at the relay board with the terminals at the top (relay at the bottom) the GPIO0 pin is on the bottom row, second from the left on the yellow connector. I’ll wire in a pin that goes into the socket when the device gets buttoned up in a weatherproof case.

Of course, we also have to connect +5 volts and ground to the relay board for power. Since the project is still at the prototype stage I just connected those into the existing power rail on the NodeMCU breadboard. The NodeMCU D1 pin is then toggled on/off according to the code, to actuate the relay.

Works like a charm.

Next Steps

I’m steadily moving toward deploying an array of MQTT-connected yard sensors around my house. The next step is to stuff the electronics in a waterproof case and hook it up to the house current and floodlights. I’m thinking that everything might fit neatly into a standard round plastic electrical outlet box. The PIR sensor will have to be waterproofed and mounted on the box, as well.

Look for a roundup on the yard alarm sensor, in the near future.

A newsletter digest of the week’s most important stories & analyses.

View / Add Comments

Please stay on topic and be respectful of others. Review our Terms of Use.