Category Archives: Hacks

Long Distance Mailbox Alert using LoRa

You’ve got mail! An IoT project using solar power and long range radio

In these pandemic times of limited mobility, I become more like a dog every day: looking forward with great enthusiasm to meals, walks and car rides. One of the daily events that’s taken on new importance is walking down to the mailbox. But it’s over 100 yards away from the house, so it’s nice to know when there’s actually something in it to collect. So I devised a means of getting an alert when the mail is delivered.

There are simple mechanical devices that set a visible flag when a mailbox is opened but ours is too far from the house to see. And the distance exceeds the range of commercially available Bluetooth and WiFi solutions. There is no source of power near the mailbox. Running hundreds of feet of cable seemed like an expensive and impractical means of solving the problem.

I cobbled together the perfect solution using a repurposed solar-powered floodlight and LoRa transmitter/receiver pair. LoRa (Long Distance Radio) works great for sending short data packets great distances. Unobstructed range can exceed several miles. LoRa modules are inexpensive and consume very little power.

An ideal IoT solar-powered enclosure

I found a $30 solar floodlight on Amazon that is perfect for this and would be for many other IoT (Internet of Things) projects. The device has a weatherproof enclosure, two user-replaceable 2200 mAh batteries and charging circuit. One screw opens it up and there’s plenty of room inside to add your own circuitry inside. The only deficiency I see with this product is that it lacks an O-ring or other means of weatherproofing, although the Amazon product page claims it is IP65 splash resistant.

Interior of solar panel before any modification

The project is described below in detail below in case you want to build something similar

Project Summary

A pair of LoRa radios is used for this project, one as transmitter and the other as receiver. The transmitter spends most of its time in low power mode with the radio sleeping. In that state it draws only around 10 mA. We’re powering it from the solar panel’s two 2600 mAh batteries, enough to power the module for 500+ hours. That’s a lot of cloudy days!

Whenever the state of the connected switch changes, the transmitter sends a short string of text: “1” indicating open or “0” for closed, followed by a space then the battery voltage. For example, “1 4.07”. The receiver listens for these packets. When one is received, an LED and/or servo are activated.

The LoRa module also echos the message to its serial port for interfacing with other systems (more about that in the next post).

Keep in mind that this project could be adapted for many other open/closed, true/false monitoring scenarios, e.g., driveway gates, doors, motion alarms, etc.

Bill of Materials

  • Hikeren Solar Light or similar
  • Pair of LoRa32u4 development boards like this set
  • An ESP8266 WiFi board (optional) like the D1 Mini (see next post)
  • Magnetic reed switch like the kind commonly used for alarm systems or other suitable contact for mailbox door
  • 10K pull-down resistor (exact value isn’t too important)

Arduino code is located at: https://github.com/RatRanch/Long-Distance-Mailbox-Alert

Assemble the Transmitter

  1. If you haven’t yet set up the Arduino IDE for use with your LoRa32u4 board, follow along with this guide. The setup only needs to be performed once.
  2. Upload the mailbox_transmitter sketch to one of the LoRa32u4 boards.
  3. Open the solar panel enclosure by removing one screw
  4. If you are attaching an external antenna, drill a 6mm (1/4″) hole in a suitable location and attach the antenna. Use an O-ring or some silicone sealant to keep it watertight.
  5. Clip the wires that attach the LED floodlight–which isn’t used for this project–to the circuit board. We will be using this cable to connect to a switch instead. Attach these wires to pins 5 and 3V3 (polarity doesn’t matter) and attach 10K pull-down resistor between pin 5 and Gnd. Solder these connections.
  6. Mount the LoRa board above the existing charging board. You can use two of the existing screw holes with spacers or a piece of double-sided tape. Make sure the charging and LoRa boards aren’t touching each other (I used a piece of Kapton tape as an insulator).
  7. Attach positive and negative power leads to the corresponding battery rails.
  8. Then re-attach the enclosure cover. Make sure the power switch on the back of the case is in the On position.

    The completed transmitter assembly interior should look something like this:
Installed LoRa Board

Here’s the installed transmitter assembly:

Installed LoRa Transmitter Assembly

Assemble the Receiver

  1. Upload the mailbox_receiver sketch to one of the LoRa32u4 boards. The code repository is here.
  2. If you just need an indication of when the mailbox is opened, you can stop here. The built-in LED will alternate between on and off every time the mailbox is opened. You could optionally connect an external LED to pin 13.
  3. Or you could have a little more fun and make an enclosure for the receiver. With a working flag that goes up when mail is delivered!
  4. You can more or less follow the instructions here to print and assemble the mailbox enclosure and mount the flag servo. The mounting holes don’t quite line up with the LoRa board but I had good results mounting it with only one screw. Double sided tape would also work fine. A nice remix of the design is here if you want a post and stand.
  5. Solder three wires from the servo to the LoRa board. Power positive goes to the 5V pin and negative to the Gnd pin. The signal wire gets attached to pin 11.
  6. Short pin 5 to 3V3 (or install a momentary switch between these pins) to test the servo action and align the flag.
  7. There is just enough room in the mailbox to fit the “rubber duckie” antenna that comes with the LoRa board if you rotate it to the 90 degree position. Cover the metal portion of the antenna with heat shrink tube, tape or whatever to prevent it from shorting the circuit board. You could also use a 78mm length of wire as an antenna but the range won’t be as good.

Operation

There’s really not much to it. ūüôā

When the receiver is powered on, the flag will rotate clockwise to its down position. Every time the contact switch attached to the transmitter closes, the flag will toggle position. If you’re putting mail into your mailbox for collection, you can just open and close the mailbox door an extra time to reset the receiver flag to its down position. Or cycle power to the receiver, which will also reset the flag.

In the next post, I’ll describe how I interfaced the mailbox receiver with the awesome Indigo Smart Home Software platform. It sends a text message to my cellphone when the mail arrives.

Technical note: conflict between servo and RadioHead libraries

The RadioHead library is used to operate the LoRa 32u4 RFM95 radio. Out of the box, this conflicts with servo.h, which we would normally use for projects involving servos (more details here).

One solution is to modify the RadioHead library to eliminate the conflict by using a different hardware timer, however, I wanted to make this project easy for others to implement.

You’ll note that I’ve “rolled my own” soft servo control code in the mailbox_receiver.ino sketch (moveServo() function). This technique would not work well if any degree of precision is required but we’re just concerned about moving the flag 90 or -90 degrees for this project. Unlike other examples I found on the web, this function sends multiple positioning pulses instead of just one to prevent position drift.

A $2 Development Board Test/Programming Jig

I’ve been using LoRa (Long Distance Radio) boards for a few recent projects. At $20 a pop, these aren’t exactly cheap. I didn’t want to dedicate one to breadboard prototyping by soldering headers to it and sought a commercial solution that would allow me to easily make a reliable temporary connection. I did not find anything like that on the market (please leave a comment if you know of such a device).

I did a bit of research and learned that pogo pins are commonly used for constructing test jigs. The downside to all the designs I saw is that they required either 3D printing parts or drilling.

I found this P75-E2 pogo pin with a diameter that can fit into a standard breadboard. But the problem is that the pins are too long and don’t stay in alignment. As you can see in the photo, my solution is to sacrifice a second breadboard by removing all of its metal clips (pry them out from the bottom) and then stacking it on top of an unmodified breadboard. Mini breadboards like these are cheap enough to devote a pair to each development board format you use.

I find that a piece of rubber electrical tape (the kind without adhesive) or a rubber band is sufficient to keep the part in contact with the board. Here is a jig for Adafruit Feather format boards with a LoRa card on top:

Reflow Madness

Over the past several months, I did a few more reflow repairs with my trusty heat gun.¬† I successfully resurrected a Beats Pill bluetooth speaker but it failed again after a few weeks.¬† Another Pill is still working okay.¬† I rescued a 15″ MacBook Pro with video problems but had no success with a 17-incher.

Meanwhile, my own 17″ MacBook Pro, which had been limping along with disabled AMD graphics (thanks to Cody Krieger‚Äôs excellent gfxCardStatus¬†menu bar app), went fully dim after a reboot.¬† This was my excuse to hang up the heat gun for good and invest in a cheap Chinese reflow oven.

At $369, the Happybuy Reflow Soldering Machine is very cheap for a reflow oven but pricey given its design flaws and build quality.¬† The oven I purchased is an example of the¬†T962A design, which is produced by several different manufacturers.¬† Google “T962A” and you’ll find various hardware fixes as well as improved firmware.

As shipped, the T962A is both unpleasant and dangerous to operate.¬† At this stage, I’ve made two modifications that should be considered mandatory

The first problem is that the metal case is not properly connected to ground, posing the potential risk of electrocution!¬† This is a widely known (and easy to fix) issue.¬† I’m amazed that units with such a serious defect continue to be shipped.

The second problem is that a heat shield is secured with what appears to be paper masking tape.¬† It produces noxious fumes and smoke when the oven heats up.¬† This is also easy to fix by replacing the tape with Kapton tape.¬† LOL, on Amazon the oven and tape are “frequently bought together.”

There are other fixes available to improve the UI and performance that I haven’t bothered with.¬† After fixing the ground and tape issues, the oven performs fine for repair work.¬† I use profile #3 for lead free solder.

To date, I’ve had one success and one failure with the oven.¬† I wrecked the aforementioned Beats Pill by melting the line in and out jacks.¬† These are apparently installed after the SMD components are reflowed.¬† On the other hand, my ailing MacBook Pro was a complete success.¬† Time will tell if this repair lasts longer than the heat gun fix.

A USB adapter for the Apple Newton keyboard

Back in the pre-smartphone days, the Apple Newton was the first device in the PDA category.¬† Newtons were only sold for five years between 1993 and 1998, when Steve Jobs pulled the plug on the program.¬† Although Apple frequently took a ribbing for problems with the Newton’s handwriting recognition feature, its main problem with achieving market dominance was Newton’s high cost.¬† Although it beat the wildly successful Palm Pilot to market, Newton could not compete on price.

One of Newton’s accessories was a very compact keyboard (just a hair over 10 inches wide).¬† I had one rattling around in my spare parts bin and it was the perfect size to use with the SunFounder Display and Raspberry Pi one-piece computer that I recently assembled.

Only one problem, no way to plug it in.  The Newton keyboard outputs a serial data stream and the RasPi (like most other modern computers) requires a USB keyboard.  Further complicating matters, the Newton keyboard does not produce ASCII characters.  Like the legacy IBM XT and AT keyboards, the Newt keyboard outputs a unique scan code for each key.  One code is sent when the key is pressed and another gets sent when the key is released.

I was surprised to find that nobody currently sells a pre-built adapter to do the serial to USB conversion.  (Such adapters exist for IBM keyboards, but are not pin or code compatible with the Newt keyboard).  The only homebrew solution I could locate was from a guy in Japan who posted this video but little else in the way of details.

I was able to piece together the following information from various sources:

  • The Newton keyboard runs at 5V (technically 4.5 volts) and produces a 5V TTL compatible signal
  • Baud rate is 9600-N-8-1
  • We can connect it to a microcontroller using three pins:¬† pin 2 – +5V, pin 8 – signal, shield – ground
  • The key scan codes are similar but not identical to those use by IBM AT keyboards

To make the Newt keyboard impersonate a USB keyboard, I would need to remap the serial data to USB.  This job calls for a small, cheap, 5V tolerant microcontroller having both serial and USB interfaces.

The Teensy 2.0 board is perfect for this purpose.  It can impersonate all kinds of HID devices like mice, keyboards, joysticks, etc. and it is compatible with the Arduino IDE, which comes with a great keyboard library that can do all the heavy lifting.

If you want to build your own adapter and are familiar with building Arduino projects, feel free to grab the sketch here.  Pinouts are in the readme file and you can install the Teensy (or MC board of your choice) within the Newton keyboard or in an external enclosure.

If you’d like step-by-step directions for my build, please continue reading.

Bill of Materials

  • Teensy 2.0 microcontroller development board (there are several Teensy versions;¬† make sure you use one that operates at 5 volts).
  • An 8-pin mini-DIN M/F or F/F cable like this one from Cables to Go.¬† We will be cutting off the female end to connect to the Newton keyboard.¬† Any similar legacy Apple serial cable would work
  • A suitable enclosure for the Teensy.¬† Note that there is plenty of room inside the Newton keyboard case.¬† If you don’t mind opening it up, you can mount the Teensy board inside.
  • A short USB A to mini B cable to connect your computer to the Teensy board

Hardware Assembly

  1. Cut off the female end of your serial cable leaving at least a couple inches of wire.
  2. If you are using the Cables to Go cable, solder the brown ¬†wire to VCC (+5 Volts), the purple wire to D2, and the uninsulated shield to GND (ground) as shown above. ¬†Use a piece of shrink wrap tube or tape to cover the shield wire so it doesn’t short anything on the Teensy.

    If you are using a different mini-DIN connector, use a multimeter to find the wires attached to pins 2, 8 and the connector outer case.  Pin 2 gets soldered to VCC and pin 8 gets soldered to D2.  Looking at the female connector that you will plug the keyboard into, pin 8 is on the upper left.  Here is my sketch of the connector that will surely end up hanging in the Louvre:

  3. Connect the keyboard to the connector you just soldered on and connect a USB cable to the Teensy.  Mount it either inside the keyboard case or in a suitable enclosure.

Programming the Teensy

  1. Install the latest Arduino IDE, if necessary.  It can be downloaded here.
  2. Download and install Teensyduino, which is an add-on for the Arduino software that adds Teensy support to it.
  3. Download the Apple Newton Keyboard to USB Converter library  (Mac users:  please Ctrl-click and choose Save As to prevent the ZIP file from automatically unpacking)
  4. Launch Arduino and install from the ZIP file by selecting
    Sketch–>Include Library–>Add .ZIP Library
  5. Open the Sketch named NewtonKeyboardToUSB, which is located in the folder Arduino/libraries/newtonKeyboard/examples
  6. Upload the sketch to the Teensy.  If this is the first time you are uploading a sketch from the Arduino IDE, you will need to
  7. Your computer should immediately recognize that a new keyboard has been plugged in

Further improvements

There are lots of digital inputs and outputs available on the Teensy.  It might be nice to use one of the outputs to drive an LED that indicates Caps Lock state.  Or perhaps it would be useful to assign some inputs to keyboard macros.

 

Importing RadioRA 2 devices into Indigo

In a recent post, I wrote about a plugin I made for Indigo to add support for Lutron RadioRA 2 devices.¬† I can’t say enough good things about this software.¬† It’s rock solid reliable and extremely flexible.¬† Among other things, I have Indigo set up to email a picture to me when somebody comes up the driveway (but only when nobody is home; how cool is that!) and detect house occupants’ presence by looking for their cellphones on the wifi network.

So it got to be time to add the 80+ RadioRA 2 devices in the house to Indigo’s database.¬† Although this¬†would be¬†a one-time task, I really dreaded the drudgery of typing in all those device names and properties.¬† So I fetched the device database from the RadioRA 2 main repeater (at http://[Repeater IP Address]/DbXmlInfo.xml).¬† This produces an XML file with complete information about the RadioRA 2 installation.

Indigo allows devices to be added to its database programatically.  The Python syntax looks like this:

indigo.device.create(protocol=indigo.kProtocol.Plugin,
    address="F8",
    name="Device Name Here", 
    description="Description Here", 
    pluginId="com.mycompany.pluginId",
    deviceTypeId="myDeviceTypeId",
    props={"propA":"value","propB":"value"},
    folder=1234)

So far so good.¬† My original plan was to use Python to parse the XML file and create the devices.¬† So I cracked open Mark Lutz’s excellent reference book Learning Python.¬† Despite Lutz’s clear examples, I just couldn’t get the XML file to parse.¬† (It turned out to be an issue with the XML file itself).

Not wanting to invest too much time writing a program I’d only use once, I came up with a different approach.¬† If I could extract the XML data into an Excel friendly format, I knew I’d be able to cobble together the Python code I needed for Indigo.¬† I found this awesome online XML to Excel conversion tool¬†but it threw an error when I tried to upload the XML file.

I opened the file with BBEdit and used the Tidy, Reflow Document from the Markup menu to make the unformatted XML more readable (you just gotta love BBEdit).¬† Something didn’t look right.¬† The node “Areas”¬†was defined twice.¬† WTF?¬† So I trimmed everything from the top of the file to just before the second “<Areas>” tag.¬† Then I jumped to the bottom of the file and trimmed everything after the first “</Areas>” tag.¬† Voila!¬† The file was processed by the online conversion tool and it returned a nicely formatted Excel workbook.

After a bit of further manipulation in Excel (and one string concatenation function that will make your head swim), I had all the Python code generated.

If you’d like to use this technique, take a look at the Excel Spreadsheet that I posted here.¬† It should be self-explanatory but give me a shout if you get stuck.¬† Copy the results from column H of the first tab and paste into Indigo’s scripting shell.¬† You should name all of your areas/rooms in Indigo’s devices tab prior to importing your devices.