NodeMCU based PIR motion sensor supporting WiFi and MQTT

19 Mar 2020 - tsp

This is a really really short tutorial on how to use the NodeMCU Amica and an cheap PIR sensor board to build an WiFi capable motion sensor that publishes motion status via MQTT. This tutorial will also use the Arduino framework for the firmware because of it’s really low entry barrier. This is really an entry level project.

Basically all you need is (note: all links are Amazon affilate links, this pages author profits from qualified purchases):

The software also uses the Adafruit MQTT library that can easily be installed via the Arduino framework.


The assembly is pretty simple. Connect the Vcc and GND pins of the PIR module (on the linked HC-SR501 these are the outermost pins) to the 3V3 and GND pins on your NodeMCU. Then select any GPIO pin like the D7 (used in this sample) and connect the output pin of your module (middle pin). That’s all that’s required for your hardware setup.


This is basically a simplified version of the firmware I’m previously described for the Sonoff T4EU1C

Basically the structure of this code is pretty similar:

As usual for small Arduino programs I’ve used a bunch of definitions that can be automatically filled in by your deployment system (like Jenkins, etc.) that can also automate your OTA updates - or manually in case of a small amount of nodes:

Note that all secrets can be extracted from the firmware when someone gets access to the device. This includes access to WiFi PSK, MQTT users and password and OTA secrets. Because of this one should:

The code also supports the following configuration options:

Basically the code just initializes the I/O ports, in debug mode also the serial port and dumps some information. Then it configures WiFi and tries to establish a connection (which will be reconnected automatically again after network loss). Storage of WiFi configuration in flash has been disabled since the credentials are already contained in the firmware (one might extend that in own versions - it’s necessary when using EAP to be capable of exchanging certificates on expiry). In case OTA hasn’t been disabled OTA will be initialized. It’s a really good idea to support OTA as long as the keys as unique and secure.

In the main loop the application tries to reconnect to MQTT after an WiFi connection has been established whenever MQTT is not active. Then it handles transfer of MQTT packets periodically and verifies pin changes on the GPIO pin. In case of a pin change or a timeout for periodic reporting the mqttSendStatus function is invoked which assembles the JSON message and passes it to the MQTT client for transmission.

The JSON message contains two fields:

The code

The code is available as a GitHub GIST

Some ideas for improvement (possible exercises)

This article is tagged: Electronics, DIY, Home automation, ESP8266, Programming

Dipl.-Ing. Thomas Spielauer, Wien (

This webpage is also available via TOR at http://jugujbrirx3irwyx.onion/

Valid HTML 4.01 Strict Powered by FreeBSD IPv6 support