A low energy open door detector with radio signals and MQTT

This blog post describes how I have set up a door-open detector at home. It uses a small ATtiny85 board that broadcasts a 433 MHz signal every time the door is opened. The signal is fetched by a Raspberry Pi that in turn publishes an MQTT message that results in the event being stored in a database and also being re-published to a cloud service. The circuit is only active when the door is open (and in that state only consumes 10mA), so the whole setup can be driven by a battery pack that is bound to last for a very long time.


I have a reed switch that I originally planned to use for turning on the hall lights when opening the front door. I have not yet found the perfect light source for this project, so while my quest for the ultimate welcome-home-lamp continues I decided to use the reed switch for an experiment with registrations of door-open events at home. My first idea was to use an ESP8266 board wired to the reed sensor. But as the purpose is only to trigger a simple event and as the ESP8266 consumes at least 70mA when active, I opted for a simpler processor and board – an ATtiny85 processor on an USB board. This is a very small and inexpensive board (about $5), but it does not have WiFi capabilities. So how to communicate the events? No worries! With a 433 MHz transmitter ($2.5), the ATtiny85 board can send a radio signal to my outlet-powered Raspberry Pi that can transform the event to an IP-based message. The circuit in this setup uses about 10mA when active, but when the door is closed, the circuit draws no energy at all. As I hate re-charging batteries, this is an option that fits me.

The gear

The board is a Digispark USB clone and it can be programmed via the Arduino IDE (see below). The transmitter and receiver are the cheapest possible, but I have found these to be very reliable if wires are added as antennas.

Digispark board, RF transmitter and RF receiver

The door-open detector kit consist of a magnet and a reed switch with connections for Normally-Closed or Normally-Opened. I use Normally-Closed (NC) connection. This means that the reed switch is closed when no magnet is nearby (i.e. when the door is open). When the magnet is close, the switch is open (i.e. when the door is closed).

Reed switch with magnet to be mounted on a door or a window

Working with the Digispark USB board

ATtiny85 is an inexpensive 8-bit AVR-processor from Atmel. It has 8kB flash memory, 512B EEPROM and 512 B SRAM and can be powered with voltages between 2.7 and 5.5 V which gives a maximum operating frequency of 20 MHz. For easy programming via the Arduino IDE, you can use the chip mounted on a development board like the Digispark USB. Here are some useful links for getting started with the Digispark USB board:





The wiring is very simple. A battery pack provides power to the Digispark board and the RF transmitter, but the current goes through a reed switch that is “normally closed”, i.e. the circuit is only closed when the door magnet is far from the switch.


On my prototype board, the setup looks like this:

Prototype board with Digispark USB and RF transmitter

The RF receiver is connected to my Raspberry Pi 3. I use the same setup and the same Python script that I developed for this project:


The Arduino IDE sketch

The Arduino sketch for the Digispark USB board uses the RCSwitch library for sending a coded 32-bit 433MHz signal when the program starts. As there is no handshake with the receiver, I send the message three times to increase the likelihood that the receiver gets the message. For avoiding that a message is registered several times by the receiver, the message contains a rolling sequence number. This way, the receiver can detect duplicates. As the Digispark board is shutdown when the door is closed, I store the previously used sequence number in EEPROM so that it can be read the next time the board is booted (the next time the door is opened). I use a magic number (42) as first byte in the EEPROM memory to note that a sequence number is available.

Raspberry Pi programming

On the Raspberry Pi side, there is a 433 MHz receiver and Python script that uses the pi-switch library for listening to signals. It is the same script and protocol that I used in this post for radio communication between an Arduino and a Raspberry Pi:


except that, in this case, when receiving a door open signal, an MQTT message is constructed and published.

The MQTT message is fetched by the subscribers that I have in my home MQTT setup. See this post series for details:


One subscriber stores the data locally and  another one forwards the message to an external cloud service.

So with this setup in place and the reed switch and magnet mounted on the front door, I get a registration of every door open event in a MongoDB database (visualized on the local LAN with my chart app, https://larsbergqvist.wordpress.com/2016/07/10/a-web-app-for-iot-data-visualization/) and externally on Adafruit IO.

With the message stored in Adafruit IO, you can use IFTTT to generate a trigger like an email alert when the door is opened. Nice to have when away and wanting to have an eye on the door. But very irritating when the kids run in-and-out when you’re at home :-).


The code for this project can be cloned from GitHub:





2 thoughts on “A low energy open door detector with radio signals and MQTT

Add yours

  1. Hi.

    Does this listening process use a lot of cpu power? I have a automation system running on a RPi, an all inputs and outputs are Esp8266 modules, comunicating over mqtt (mosquitto mqtt running on the same Rpi), and I’m thinking about using some comercial 433.92 PIR Sensors and Reed switchs, sending data directly to my raspberry pi, but i’m concerned about high cpu usage…



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: