IoT Wireless Temperature and Motion Sensor




Introduction: IoT Wireless Temperature and Motion Sensor

About: I am a Dad to two kids who are constantly need attentions. I am passionate about anything related to technology, Internet of Things, Arduino and Raspberry Pi, I am good at fixing things that is broken to red...

I was inspired by the many IoT projects that are in Instructables, so in the process of learning I am trying to combine some useful application that are relevant. As the extension to my previous Instructables related to IoT Temperature sensor, I now added more capabilities to the subsystem. The added functionality are:

- NTP connection to get the time

- LED that can be remotely controlled

- PIR sensor to detect motion

- Connected Raspberry PI running homekit to allow connection to iPhone "Home"

Step 1: The Concept, Connectivity and Components

The concept as shown above is to allow temperature monitoring remotely with the additional capability to detect motion if someone is at home, and allow notification via LED. The unit can be accessed locally in the LAN or remotely via webserver. You can also connect Raspberry pie (optional) with Homekit accessory installed to allow connection to the iPhone "Home" App.

The same as previous version the following components are required in this project, please note that the link below is an affiliate link, so if you don't want to contribute, just go directly.

- NodeMcu Lua ESP8266 dev board. I get mine from banggood.

- LM35 temperature sensor

- PIR sensor


- Prototype board

- Arduino IDE

- Working webserver with php server scripting enabled

- Raspberry pi (Optional)

Step 2: Get the Arduino IDE to Work

For details on this step, please see my earlier instructables Step 2. on IoT Temperature sensor with ESP8266.

Step 3: Connecting the Temperature Sensor, LED and PIR

The temperature sensor LM35 have 3 legs, the first leg is VCC, you can connect this to the 3.3V (ESP8266 board's output is 3.3V). The middle leg is Vout (where the temperature is read from, you can connect this to the analog input of the ESP8266 pin AD0, this is located at the top right hand side of the board as shown in picture. And the right leg should be connected to the ground.

PIR sensor consist of 3 legs as well, you can see a tiny marking of +,0,- on the PCB next to the leg. So connect the "+" to 3.3V,"-" to the ground, and middle pin "0" to pin D6 of ESP8266.

LED had only 2 legs, "+"(Anode), the longer leg connect this to pin D5 of ESP8266 and "-" (Cathode) the shorter legs should be connected to ground (GND).

Step 4: Setting Up the Cloud Webserver

There are a certain assumption for this step:

You already have a working webserver, hosted in a proper domain. And you are familiar with transferring files into your webserver via FTP using Filezilla or some other FTP program.

Upload the attached zip file to the root of your website. Let's assume for this exercise your website is ""

It is assume that all the file is located in the root of the webserver, if you have store it inside another folder, please adjust the file location accordingly in both the ArduinoData3.php file and the Arduino sketch. If you are not sure please let me know and I will try the best I can to help.

Step 5: Setting Up the Database to Hold the Temperature Data

we are using sqllite database for this exercise. Sqllite is the light file based database which doesn't require a server. The database is located locally in your webserver. If you are concern about the security then you should modify the code to use a proper database server like mysql or MSSQL.

Before you start you need to change the database password located in the phpliteadmin.php file. So open this file in your webserver and edit password information in line 91 to the password that you want.

Then point to phpliteadmin.php in your webserver. Using our example before you should point to

Because there is no database in the server you will be presented with the screen to create the database. Enter "temperature.db" in the new database input box and click on "Create" button. The database will then be created successfully. At this point the database is still empty so you will need the sql script to create the database table structure to host the data.

Step 6: Create the "temperature" Table

To create the table, click on the "SQL" tab and paste in the following sql query.


-- Table structure for temperature
      humidity            INT     NOT NULL,
      temperature         REAL,
      timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, 'heater' BOOLEAN, 'goaltemp' REAL);

Then click on the "Go" button at the bottom. The table should be created successfully.

If you refresh the page, you should now see the "temperature" table under the temperature.db database in the left hand side. If you click on the temperature table if still contain no data.

Now that we have the database created you can point to the following url

You would see the temperature dial showing dummy data, the motion sensor and the control panel to turn the LED. The bottom part of the graph will still be empty because there is no data yet.

Step 7: Upload the Temperature Sensor Sketch to Your ESP8266

Now copy all the attached file and open the "ESP8266TempPIRSensor.ino" the Arduino interface will create the folder for you. Move the rest of the files into the new folder that was created by the Arduino interface.

Modify the specified webserver and the location of the data_store3.php file if necessary. Then upload the sketch to the ESP8266.

If all goes well it should get uploaded successfully and the first time the ESP will goes into AP mode. You can use your laptop or mobile phone to connect to it. You should be able to find the AP by the name of "ESP-TEMP".
- Try to connect to ESP-TEMP using your laptop of mobile phone - Find out what is the IP address that you are being assigned to, by doing the "ipconfig" command in windows or "ifconfig" command in linux or mac. - If you are using iphone click on the i button next to ESP-TEMP that you are connected to - Open up your browser and point to the ESP-TEMP, if you are assigned with as your ip, the ESP-TEMP is having the ip of, so you can just go to and you should be presented with the setting page where you can enter your wifi router ssid and the psk key. once you entered that both and tick the "Update Wifi Config" check box, click on "update" to update the setting to your ESP8266.

If you want to turn on the debug to the Serial Monitor you will need to uncommented the

#define DEBUG

line in the clock.h and commented the

//#undef DEBUG

line. then click on Tools->Serial Monitor. The serial monitor window will show you the wifi connection progress and show the local IP address of the ESP8266. The internal blue LED will blink once when the temperature reading takes place. It will also turn on when there is a motion being detected.

Step 8: Accessing Your Temperature and Motion Sensor

You should now be able to point once again to the local webserver of the ESP8266. And this will show the time, temperature and the motion sensor.

Now you can also point to your external webserver, in this example is

You can slide the button under the control panel to toggle the LED. I use this to notify my kids when I am on the way back home from work.

The motion sensor is updated every seconds or so, so you will have to refresh the page more often to see whether there is a motion detected. At the moment the auto refresh is set to 60 seconds. The temperature will take reading every couple of minutes, but you can adjust this too to the time that suits you.

Congratulations if you made it this far !!, give yourself a pat at the back and enjoy your creation. The next step is optional, only if you would like to be able to control the LED and monitor the temperature as well as the motion sensor from Apple devices.

Step 9: Install HomeBridge for HomeKit in Raspberry Pi (Optional)

I was inspired by the instructables from GalenW1 which allows me to learn so much about HomeBridge.

To install HomeBridge for HomeKit on a Raspberry Pi you can use the instruction in the following

HomeBridge allows you to connect the Home App in Iphone to the sensors that you just build in the previous steps.

One you get the HomeBridge installed, you need to install a few plugins:

- Temperature sensor

- Motion Sensor

- Switch

sudo npm install -g homebridge-http-temperature
sudo npm install -g homebridge-MotionSensor
sudo npm install -g homebridge-http-simple-switch

Once the plugin is being installed you will need to configure the config.json file located below

sudo vi /home/pi/.homebridge/config.json

you can adjust the content of the config.json file as per below, please make sure that the url is pointing to the correct location.

"bridge": { "name": "Homebridge", "username": "CC:22:3D:E3:CE:30", "port": 51886, "pin": "031-45-154" }, "description": "This is HomeBridge Config file, add more accessories and platform .", "accessories": [ { "accessory": "HttpTemperature", "name": "Living Room Temp", "url": "", "http_method": "GET", "field_name": "" }, { "accessory": "Motion", "name": "Motion Sensor on my Desk", "url" : "", "http_method": "GET", "json_response": "motion" }, { "accessory": "SimpleHttpSwitch", "name": "Light Switch", "url": "http://arduinotestbed.como/setlight.php", "http_method": "POST", "default_state_off": true, "sendimmediately": "" } ] }

Step 10: Connecting Homebridge to Your Iphone

Now that all the accessories had been configured, you can run the homebridge using the following command


You should see the screen as above. You can follow the following step to add the Homebridge to your homekit.

- Now start your "Home" app in your Iphone

- Click on "Add Accessories" button

- you will be presented with the screen to scan the code, you can use your phone camera to scan the code from the Raspberry Pi screen or adding the code manually.

Please note that both Iphone and Raspberry Pi needs to be on the same wireless router to work.

- Once connected you will be prompted with the screen that says your accessories is not certified, click on "Add anyway" button to continue

- You will then have the option to configure each of the accessories, in this instance we have the light switch, the motion sensor and the temperature sensor.

- The final screen will show you all the accessories that is connected.

Once it is connected you can use Siri to check the motion sensor, the temperature and turn the light on and off.

Step 11: Get Your Homebridge to Run in the Background

Congratulations!! you have done it. As a bonus you can run homebridge at the background using the following command:

homebridge &

Now you can have some fun with Siri and enjoy your hardwork.

Thank you for following this until the end. If you like this, please leave some comments or vote for me.

Wireless Contest

Participated in the
Wireless Contest

Raspberry Pi Contest 2017

Participated in the
Raspberry Pi Contest 2017

Be the First to Share


    • Puzzles Speed Challenge

      Puzzles Speed Challenge
    • "Can't Touch This" Family Contest

      "Can't Touch This" Family Contest
    • CNC Contest 2020

      CNC Contest 2020

    2 Discussions


    2 years ago

    Thanks for this tour of Arduino's functionality with Apple, and for your succinct instructions! It's great to see what creative people are doing with these popular devices. A lot of the coding, PHP setup, and database creation should be automated with a Graphic User Interface for those of us who know what we want but don't like looking at code. You might be in a position to supercharge the Arduino GUI! This isn't criticism, it's a wish to further the sophistication of user interfaces: ThoughtSpot builds data visualizations as we type specs into a search bar, and this way of commanding computers is going to take over the world in the next few years. Voice command interfaces like Siri and Alexa are fine, but the typed word removes misunderstanding from the transaction until those apps are ready for prime time with voice recognition.


    Reply 2 years ago

    Hi Alstructable,

    Thank you for your constructive comments, surely you are right, we have come along way to simplify most of the underlying code library when dealing with Arduino, and certainly there are better ways to create database without having the need of learning the sql query. In this space Apple had created it's own GUI based database, but I certainly feel there are complexity when dealing with xcode on it's own, thus php flat file database bring the complexity down. But I will take this into consideration for the next revision.