This tutorial will walk you through the steps of using an ESP8266 and a PIR Sensor (Passive Infrared Sensor) to detect motion, and send you notifications using a Wia Flow.
What You Will Need- PIR Sensor (HC-SR501)
- ESP8266
- Female to Male Jumper Wires
- USB Cable
If you haven't done so already, visit our tutorial on getting started with the ESP8266. It details the correct environment to complete this tutorial.
Connecting the HardwareConnect your wires according to the chart:
PIR Sensor | ESP8266
- GND | GND
- OUT | D7
- VCC | 3.3V
The Pir sensor runs on an operating voltage range: DC 4.5-20V but there is a way to power the sensor from a 3.3V source. The H
pin on the side of the sensor as denoted by VCC
in the illustration above is connected to a voltage regulator which converts the 3.3V source.
In the Arduino IDE, go to Sketch > Include Libraries > Manage Libraries
. Install each of the following libraries by searching for their name in the search bar within the modal. A button will appear in the bottom right of the box that will allow you to install the library.
- ArduinoJson
- ESP8266WiFi
- ArduinoHttpClient
Due to recent changes in the library, be sure to install ArduinoJson 6.0 so that you are up to date.
If a library doesn't show up in the results when you search it, you may need to update your Arduino IDE version. You can download the latest version here.
CodeIn the Arduino IDE, copy and paste the code below.
Replace the following values in the code by placing your own values in between the quotation marks.
your-WiFi-ssid
(This is the name of your WiFi network)
your-WiFi-password
(Your WiFi network password)
your-device-secret-key
(Navigate to the Wia Dashboard > Devices and choose your device. The device_secret_key will be in the Configuration tab)
The code above publishes an Event to Wia every time it detects motion, with a name
value of motion
(you'll need this later on, when building your Flow), and a data
value equal to the reading from the motion sensor.
Make sure the correct board is selected - go to tools > board
and select NodeMCU 1.0 (ESP-12E Module)
.Make sure the correct port is selected - tools > port
.
To turn on your alarm, press the flash button on the Esp 8266. The blue LED should turn on. To turn off the alarm, press and hold the flash button for at least 2 seconds, the blue LED will turn off.
Now to set up your flow.
Create the FlowLets set up a flow that will send a notification to your phone when motion is detected by the PIR Motion Sensor.
Go to your Wia Dashboard and click Flows
in the left hand side menu. Create a new Flow with any name you like.
In Flow Studio, drag an Event
node from the Trigger section and:
- Enter
motion
as the event name
- Add your device
Now add a Check State
node from the Logic section
- Enter
enabled
as the Key
- Select 'Equal to` from the Condition dropdown
- Enter
true
as the Value
Next, drag over a Notification
node from the Action section.
- Enter "Motion Detected" as the Message.
Now Wia can send you a message to the Wia mobile app when your Security Sensor detects a motion.
Add a WidgetNext, head over to your Device's 'Overview' tab. Click 'Add a Widget'. Give the Widget a name Motion Detected
, choose Widget type 'text', and enter the name of the Event i.e. motion
.
The PIR Sensor allows you to change the Delay Time as well as the Sensitivty Level.
The PIR Sensor is a digital device. This means that it reads two values: HIGH and LOW. When the sensor detects movement, it reads HIGH. Otherwise, it reads LOW.
The Delay setting determines how long the PIR Sensor will read HIGH after it has detected a motion. The Sensitivity Sensor determines the proximity at which the PIR Sensor will register a movement. This sensor is more accurate when it is set to a lower proximity level. You can control these settings using the orange panels located directly across from the pins you used to wire your device.
If you are having trouble finding the correct board, refer back to our getting started with the ESP8266 tutorial.
If your port is not showing the USB option, try switching the USB cable and checking that the cable isn't "charge only". If this doesn't help, you may need to install a driver. This tutorial contains a section that will walk you through those steps.
If you are receiving the error StaticJsonDocument does not name a type
its due to a breaking change introduced into the ArduinoJson
library. Go to sketch > include library
and manage libraries to update to version 6+.
Comments