Home » Tutorials Connect PIR Motion Sensor and ESP32 to AskSensors over MQTT Tutorials Connect PIR Motion Sensor and ESP32 to AskSensors over MQTT 1826 Views Save Saved Removed 1

PIR sensors allow to detect motion based on the detection of infrared energy emitted by a moving body, it means that we can use it to know when someone enters and leaves a certain area.

This tutorial shows how to detect motion with AskSensors IoT platform using the ESP32 and PIR sensor. PIR motion detection is widely used to turn on the lights, trigger an alarm or many other applications.

1) Prerequisites :

Active AskSensors account: Create AskSensors account.

Create a new sensor. Follow the simple process described here.

Follow these steps to get familiar with AskSensors.

2) Required Hardware :

Computer running Arduino software (version 1.8.7 or higher).

ESP32 board.

PIR sensor

Jumper wires

1K Resistor

Breadboard

Micro USB cable.

The connection diagram of this PIR sensor to the ESP32 is quite simple. Three pins need to be connected:

PIR VCC to ESP32 dev board 5V

PIR GND to ESP32 GDN

PIR DATA to ESP32 GPIO through a 1K Resistor (D2 in this tutorial).

Note: The ESP32 GPIOs require 3V3 signals (not 5V tolerent). For quick hack, you can only add a serial resistor of 1K between the PIR DATA pin and the ESP32 GPIO pin to protect the ESP32 GPIO from damage. However, for production, a 5V/3V3 level shifter is needed to guarantee long term circuit reliability. You can check this page to get a 5V/3V3 level shifter module.

3) Install ESP32 in Arduino IDE:

If you haven’t already installed the ESP32 board in your Arduino IDE, follow these next instructions:

Install the latest version of the Arduino IDE software (1.8.7 or higher).

In your Arduino IDE, go to File> Preferences.

Go to the “Additional Board Manager URLs” field, Enter the following URL:

https://dl.espressif.com/dl/package_esp32_index.json

If you already have the ESP8266 boards URL, separate the URLs with a comma as shown below:

https://dl.espressif.com/dl/package_esp32_index.json,http://arduino.esp8266.com/stable/package_esp8266com_index.json

Now, open boards manager (Tools > Board > Boards Manager), search for ESP32 and click the install button for the “ESP32 by Espressif Systems”.

4) Software :

Install the MQTT PubSubClient Library for the Arduino IDE if you haven’t already.

Download this demo from the AskSensors Github page.

The provided code simply reads the status of the PIR sensor and send this information to AskSensors over MQTT. You need to modify the following variables: Your network credentials, so that ESP8266 can establish a connection with existing network The Api Key In and username to set the MQTT Topic:username/apiKeyIn



const char* ssid = "....."; // Wifi SSID const char* password = ".....";// Wifi Password const char* username = "................."; // my AskSensors username const char* pubTopic = "publish/..../....."; // publish/username/apiKeyIn const char* mqtt_server = "mqtt.asksensors.com";

5) Flash the code to your ESP32

Connect your ESP32 board to the computer via serial/USB and upload the code using the Arduino IDE.

Check your Sensor data stream on AskSensors.

You can cross-check the AskSensors graph readings with the values being printed on your ESP32 Serial Terminal.

You can customize a Binary graph to plot your data as showing by next pictures.

6) Source Code :

A basic source code is shown below. Please refer to the AskSensors Github page to get the latest version and updates.