IoT Wireless Temperature and Motion Sensor

by snorlaxprime in Circuits > Arduino

8899 Views, 82 Favorites, 0 Comments

IoT Wireless Temperature and Motion Sensor

Cover.png

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"

The Concept, Connectivity and Components

ConceptArt.jpg

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

- LED

- Prototype board

- Arduino IDE

- Working webserver with php server scripting enabled

- Raspberry pi (Optional)

Get the Arduino IDE to Work

ArduinoBoardSelection1.png

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

Connecting the Temperature Sensor, LED and PIR

LM35.png
as312_top.jpg
LED-anatomy-1024x455.png
LEDConnectionToESP.jpg
FullConnection.jpg

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).

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 "http://arduinotestbed.com"

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.

Downloads

Setting Up the Database to Hold the Temperature Data

ConfiguringDatabase.png
DatabaseCreate.png
DatabaseCreateSuccess.png

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 http://arduinotestbed.com/phpliteadmin.php

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.

Create the "temperature" Table

DatabaseTemperatureTable.png
TemperatureTable.png

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

BEGIN TRANSACTION;

----
-- Table structure for temperature
----
CREATE TABLE 'temperature' ('ID' INTEGER PRIMARY KEY     NOT NULL,
      humidity            INT     NOT NULL,
      temperature         REAL,
      timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, 'heater' BOOLEAN, 'goaltemp' REAL);
COMMIT;

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

http://arduinotestbed.com/ArduinoData3.php

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.

Upload the Temperature Sensor Sketch to Your ESP8266

UploadTheSketch.png
DebuggingMode.png
ESP8266Settings.png

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 192.168.4.10 as your ip, the ESP-TEMP is having the ip of 192.168.4.1, so you can just go to http://192.168.4.1 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.

Accessing Your Temperature and Motion Sensor

ESP8266Settings.png
ArduinoPanel.png

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 http://arduinotestbed.com/ArduinoData3.php

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.

Install HomeBridge for HomeKit in Raspberry Pi (Optional)

Homekit.png

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

https://github.com/nfarina/homebridge

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://arduinotestbed.com/temp.txt", "http_method": "GET", "field_name": "" }, { "accessory": "Motion", "name": "Motion Sensor on my Desk", "url" : "http://arduinotestbed.com/motion_data.php?json=true", "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": "" } ] }

Connecting Homebridge to Your Iphone

HomeBridgeStarting.png
Home.PNG
Home01.PNG
Home02.PNG
Home03.PNG
Home04.PNG
Home05.PNG
Home06.PNG
Home07.PNG
Home08.PNG
Home09.PNG

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

homebridge

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.

Get Your Homebridge to Run in the Background

Siri01.PNG
Siri02.PNG
Siri03.PNG

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.