Technology / Tutorials

Off-The-Shelf Hacker: Deploying And Testing The ESP8266-PIR Yard Sensor

29 Oct 2016 5:22am, by

sensor-overallIn this week’s Off-The-Shelf Hacker, we’re going to wrap up the ESP8266-PIR yard-sensor project, by putting the device in the field, or in the front yard anyway. Then, I’ll discuss how I’ll gather data over the next few weeks. At some point, in a future article, I’ll share results and insights into any patterns that present themselves.

My tradition is that the first usable, working device that I can demonstrate to fans, friends, family or benefactors will be called version 1.0. Any major changes, such as a board layout, main component change or revamping of the aesthetics, will trigger a bump to the next version. Minor tweaks to sort out operational issues doesn’t usually warrant a new version number.

Buttoning up the Sensor

The final housing design is pretty simple. It consists of a 1.5-inch schedule 40 plastic end plug that fits into the 1.5-inch union. A hole will be drilled in the back surface, for the power connector, and hot glued in place. Slip fitting the parts together seals up the device pretty well. When it’s fully deployed, I’ll probably permanently secure the pipe parts together with PVC pipe solvent.

Sensor - Back View With Duct Tape and Transformer

Sensor — Back View with Duct Tape and Transformer

Observant readers will notice that during testing I simply duct-taped the housing to the lattice work on my front porch with the sensor pointed toward the driveway. I also temporarily used tape over the back of the device to keep out bugs and the morning dew. Using tape makes it easy to plug in the programming cable and access to the various jumpers.

Picking out a deployment spot for this type of sensor takes some thought and experimentation. I selected the location in my porch lattice to be able to cover a 10-foot arc, watching over the front door.

The sensor points away from the door and directly covers the driveway area in front of our two cars. It’s a natural “choke point” for anyone moving toward the door, from the street. Intruders have to move tangentially across the front of the sensor to navigate around the cars on the way to the front door. PIR sensors need tangential movement in order to pick up movement. They are less sensitive to objects moving directly in a line to or from the sensor. The device was mounted about seven feet above the driveway.

I came up with an interesting way to map the coverage area in front of the sensor, using a Raspberry Pi 2 with a 3.5-inch color TFT touch screen and EDIMax USB WiFi adapter, all powered by a 5.0-volt cell phone power pack.

TFT Display Shield Mated To A Raspberry Pi 2 For Testing

TFT Display Shield Mated to a Raspberry Pi 2 for Testing

After powering up the ESP8266-PIR sensor with a 12.0 volt (1.2 Amp) wall wart, I simply ran netcat in a terminal window, on the mobile Pi-TFT machine. Here’s the netcat command.

pi%  netcat 1337

Any movement in front of the sensor then increments the scrolling number in the tiny window. By crisscrossing in front of the sensor I was able to map out the coverage area. Turns out that the pattern isn’t exactly uniform from left to right. There were a couple of dead spots. Also, I expected to be able to detect movement up to about 15 feet. Even using a 12-volt input source, the best I could get was a fairly reliable 10 feet.

The standard passive infrared (PIR) sensor has a plastic “focusing” dome in front of the actual silicon chip. The dome has a patchwork of Fresnel-type lenses covering the surface. Rotating the sensor allow me to change the coverage area. Clearly, I’ll need to spend a bit more time analyzing the orientation of the sensor for the best coverage of a given area.

One last thing I wanted to mention is that running on the 12-volt wall wart caused the 3.3-volt regulator to get hot enough that it could actually burn my finger when I touched it. In the fine hacking tradition, I simply bolted on a couple of pieces of copper angle brackets with a dab of thermal paste, to dissipate the heat. The copper brackets were left over when I built new pivots for the Steampunk Presentation Machine arm.

Recycled Copper Bracket As A Heat Sink For The 3.3 Volt Regulator

Recycled Copper Bracket as a Heat Sink for the 3.3-Volt Regulator

The lesson here is salvage, organize and recycle parts from your junk box.

With the hardware working, the next thing on the list is the analysis of the data, which is what we’ll briefly discuss next. A few weeks out we’ll review my finding on any patterns or insights I gain from logging sensor activity over a few days and nights.

The Analysis Plan

Logging the data is pretty straightforward. I started the ESP8266-PIR device, then connected to it, over the network using netcat. I used my Asus Linux notebook as the logging machine. The code on the ESP8266 sends a sequential number, to the Linux notebook, every time motion is detected in front of the sensor. Note that the ESP8266-PIR code doesn’t include a time stamp in the data it sends to the logging machine. Time stamping the data is handled on the logging machine. You could just as easily use a Raspberry Pi or CHIP computer for logging purposes.

As we saw earlier, you display the data from the ESP8266-PIR sensor with the netcat command.

pi%  netcat 1337

Although this logs the data from the sensor to your screen, it’s not very useful because you have no idea what time the movement occurred. You also probably would like to record then movement occurred, the time and then save it to a file for possible later analysis.

That’s easily fixed. Just “pipe” the data from the netcat command through that ubiquitous Linux text processing application called awk.

pi%  netcat 1337 | awk '{print strftime("%H:%M:%S %Y-%m-%d") " - " $0}'

As data streams in from the sensor, it will print the time/date and the data number on the screen.

You can then use another traditional Linux command-line function, called redirection, to save the data to a file.

pi%  netcat 1337 | awk '{print strftime("%H:%M:%S %Y-%m-%d") " - " $0}' > log.txt

Here’s a sample of the data.

23:25:07 2016-10-26 - 1
23:25:08 2016-10-26 - 2
23:25:09 2016-10-26 - 3
23:25:10 2016-10-26 - 4
23:25:12 2016-10-26 - 5
23:25:17 2016-10-26 - 6
23:25:18 2016-10-26 - 7
23:25:24 2016-10-26 - 8
23:25:25 2016-10-26 - 9
23:25:34 2016-10-26 - 10
23:25:36 2016-10-26 - 11
23:25:39 2016-10-26 - 12
23:25:46 2016-10-26 - 13

One caveat is that if you kill the netcat, pipe, redirection command line with a <CTRL>C, it will create a blank log.txt file. A workaround might be to have another job periodically copy the log.txt file to another file name, say log-backup.txt. I’ll leave that for readers to ponder.

I’ve thought about generating the time/date-stamp on the ESP8266. That approach has merits particularly as microcontrollers get increasingly powerful. I like the idea of syncing all the data on a logging machine, from multiple sensors, so the time is consistent and reliable. An interesting experiment might be to implement time/date logging into the ESP8266-PIR data stream from the device. The functions exist in the various Arduino libraries to make that happen.

You Take It from Here

Now it’s your turn to take an ESP8266-PIR sensor to the next level. You might make up 4 or 5 boards and distribute them around your yard. You could play around with awk and the data format coming out of the sensor to be more descriptive and useful. You could add some kind of sensor number that is sent to the logging computer, along with the data, so you can accurately determine where a zombie or clown keep showing up in your yard. There’s a lot of room for enhancements both at the sensor and on the logging machine.

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

View / Add Comments