Receive a message from Facebook when PIR Sensor Detects Motion

February 4th, 2019 by

Today, we will learn how to send a Facebook Chat when our PIR sensor detects motion. This tutorial is helpful if you want to create a security system which alerts you when someone is entering your house. You can also include this project in one of your home automation system.


Video Demonstration



PIR Sensor
NodeMcu ESP12 Lolin V3
Jumper Wires
Led Light
220Ohm Resistor
Facebook Page
Facebook Developer Account


Circuit Connection

Setting up Arduino IDE

If you already set up your NodeMcu Board, skip this section.

We need to add esp8266 to our Arduino IDE. Open up your IDE then go to “File -> Preferences” or simply hit “Ctrl + comma”

Paste in additional board manager URLs.

Go to “Tools -> Board -> Boards Manager” search and install esp8266

Close and restart your Arduino IDE

Plugin your NodeMcu to your computer.


Selecting Port and Board

Go to Tools -> Port and select the COM Port of your NodeMCU. If you are not sure what port your NodeMcu is plugged in, go to Device Manager -> Ports (COM & LPT)

Now select NodeMcu 1.0 (ESP-12E Module) by clicking Tools -> Board. Scroll down until you find it.


Testing the Connection

To test the connection, upload this code to your NodeMcu Lolin V3

The LED should light up when motion is detected. If it doesn’t light up, consider adjusting the delay and sensitivity knob. Note that the code above is only for testing the connection.


Setting up our Local Web Server

You need to install XAMPP. You can download XAMPP here. We will put our PHP codes to XAMPP htdocs folder. After installing, Open XAMPP Control Panel and start up Apache


Exposing your local Web Server

We need to expose our local web server so we can add it later on Facebook as a webhook. If you do not have NgRok, you can download it on their website. Register an account. Follow this tutorial on how to start an HTTP tunnel on port 80

You can also host the PHP files on your own web server but make sure it has an SSL certificate.


Setting up a Facebook Developer Account

First, we need to create a Facebook Page for our Facebook Chat App.  After creating a Facebook Page, Go to Facebook Developers.
Create a Facebook app. In the sidebar, add a product and select Messenger.

Select your page and Copy the Page Access Token. We will need it in our PHP Scripts.

Copy the URL in your NgRok and add /senderId.php (Depends on where you put the PHP Scripts). Tick the message checkbox.

Take note of Callback URL and Verify Token. When you are done, click Verify and Save. (If verification doesn’t work, make sure you are using HTTPS and the PHP code is deployed already). If you have not deployed your code yet and doesn’t know how to get your sender id, follow the “Getting your SenderId” section below.


Getting your SenderId

We need to know what our sender id is. We need to do this step in order to specify where the message will go. Change the hubVerifyToken and accessToken.

Save the file as senderId.php.

Go back to Facebook Developer and point the webhook URL to your senderId.php file

Send a message to your Facebook Page. You should be able to see a message containing “Your sender id is (NUMBERS)”


Modifying the Codes for Motion

We will change the code a little bit. Notice that the code is somewhat the same. We only need to change the verify and access token, as well as the sender id. Save the file as bot.php

Go back to Facebook Developer and point your webhook to bot.php. 

The code above will send a message to our Facebook Account when triggered by our NodeMCU. Our NodeMcu module will send an HTTP Post request to our webhook URL.

Getting Certificate Thumbprint

Visit your URL and click the lock button beside it. Click Certificates. A window will pop up. Click Details and scroll down until you find the thumbprint.

Copy the Thumbprint


Upload the PIR Sensor Code

Before uploading the code to our NodeMcu ESP 12, we need to change first the SSID or your Wifi Name and wifi password. You also need to change this line of code.

http.begin(“”, “yourcertificatethumbprint”);

The first parameter is your webhook URL and the second one is your certificate thumbprint.


After uploading the code, test it out! You should be able to receive a message from your Facebook Page including the date and time when the motion is detected.


That’s it! Hope it works. If you need assistance, feel free to comment down below.

Please like and share this tutorial by clicking the share button below.

Spread the love


Leave a Reply

Your email address will not be published.