---
# System prepended metadata

title: Tutorial - Precense detector

---

# Tutorial - Precense detector
By: Mathias Bräck
Student ID: mb225sv

This projects goal is to construct a precense detector
*(Due to bad coverage of LoRa and Sigfox networks this tutorial uses Wifi to transfer data.)*

# Objective
To control properties like room temperature and light I needed a precense detector. To accomplish this I decided to build a precense detector based upon a PIR-device that reports to a MQTT Service if anything triggers it. 

# Material
* LOPY4
* Expansionboard 3.0 for LOPY4
* HC-SR505
* A MQTT Service - in this tutorial i will use Pybytes
* Wifi infrastructure



| Material | Purpose  | Information | Bought from | Cost |
| -------- | -------- | -------- | ------|---|
| LOPY4     |  Microcontroller with LoRa,Sigfox,BLE and Wifi    | https://pycom.io/product/lopy4/     | Electrokit | 950 kr|
| Expansoin board | Exposes connectors and a manages battery |https://pycom.io/product/expansion-board-3-0/ | Included in LOPY4 bundle above| |
| HC-SR505 | Mini PIR sensor | Datasheet - https://static.rapidonline.com/pdf/78-4110_v1.pdf | 4 kr| Aliexpress|



# Computer/Developement enviroment setup

* IDE for developement for Lopy4 on Windows 10
    * Atom (Developement IDE)
        * Download Atom IDE from https://atom.io/
        * Setup Atom by installing downloaded executable file with default options
    * PyMakr (Plugin for Atom to communicatie with Pycom devices)
        * Start Atom IDE software
        * Choose the option on the "Welcome Page" - "Install Package" and then "Open Installer"
        *Also found in the menu "File - Settings - Install"*
        ![](https://i.imgur.com/MdF6Zn8.png)
        * In the search box type "Pymakr" and choose "Install"
        ![](https://i.imgur.com/thbn8vy.png)

# Pybytes setup with LOPY4
Pybytes platform receives information from IoT devices and displays data in a basic matter. Data can then be sent to or collected with integrations.
Create an account on Pybytes: https://pybytes.pycom.io/

* Login and start by configure your network
![](https://i.imgur.com/XrDBcg7.png)

* Add a new Wifi-network by clicking on the "ADD WIFI" button
![](https://i.imgur.com/WLKJ6YZ.png)

* Define your Wifi network settings and hit "Save"
![](https://i.imgur.com/KVoJ02M.png)

* Go back to the first landing page and click "Add Device"
![](https://i.imgur.com/2XijZmx.png)

* Next option is to choose from adding the device by USB or App. I choose USB
![](https://i.imgur.com/O9UUmLw.png)

* Choose LOPY4 device in the list
![](https://i.imgur.com/nOS88Yv.png)

* Choose which networks you whant to use and hit next(This guid only covers WIFI as I don´t have any coverage of LoRa or Sigfox where I live)
![](https://i.imgur.com/3xm7IsR.png)

* Give your device a cool and relevant name and klick "SAVE"
![](https://i.imgur.com/kED0nUU.png)

* To provision (install firmware) to your device choose "OFFLINE FIRMWARE UPDATER" and copy the code show to you that I have marked down in this screenshot. (Note that your code is unique and don´t try to use the code in this guide)
![](https://i.imgur.com/FDrNFm7.png)

* Install Pycom Firmware updater by download and install the version that is appopriate for your system by clicking on the correct download link.(Shown in the picture above)

* Start the Pycom firmware updater app
![](https://i.imgur.com/pvjIVjN.png)

* Connect your LOPY4 device to USB on your computer

* Click on continue on the first guide page
![](https://i.imgur.com/wKnXg1S.png)

* Make sure you have closed Atom and VS Code IDE software with plugins that talks to the LOPY4 board and click continue
![](https://i.imgur.com/abTjgu6.png)

* Make sure that the first drop down list is populated with a relevant COM-port (connection port for your LOPY4). If not trouble shoot your device´s connection to your computer. Make sure "Type:" is set to Pybytes, "Force update Pybytes registration" and "Enable Pybytes / SmartConfig support"
![](https://i.imgur.com/kwaqCGB.png)

* Paste your configuration code you copied from Pybytes provisioning earlier
![](https://i.imgur.com/iquBZSI.png)

* Your device will now be updated with your pybytes firmware and your network configuration
![](https://i.imgur.com/oOPs5FJ.png)
 
* Hopefully the guide finishes with a successful results and you can now close this app
![](https://i.imgur.com/oBbaZpX.png)

* Pybytes webpage for your device should soon report a successful connection
![](https://i.imgur.com/Nyzm3kh.png)


# Connecting PIR sensor
* The SR-HC505 has 3 pins
![](https://i.imgur.com/tK9ZI68.png)

* From the components side read from left to right
GND
SIGNAL
VCC

* Connect GND on SR-HC505 to the Expansion Board
* Connect Signal to P18 on the Expansion Board
* Connect VCC to +3.3V on the Expansion Board
![](https://i.imgur.com/ujB4R4g.png)



# Upload code and handling the sensordata
Time to upload code to LOPY4 to activate PIR detection and send data to Pybytes
* Open Atom and press the button "Add folders"
![](https://i.imgur.com/2XIrVOq.png)

* Create a new folder and give it a relevant name and press "Select Folder"
![](https://i.imgur.com/UHlQKjx.png)

* Right click in the left pane below the name of your newly created folder and name it "main.py"
![](https://i.imgur.com/EoBT1II.png)

* Paste the code below into the file main.py
```
import machine
from machine import Pin
import time

#Define input pin
p18 = Pin('P18', Pin.IN)

#Continue forever to read PIR sensor and if detection - send signal to Pybytes
while True:
    #Read value from PIR sensor pin
    pir= p18.value()
    #Check if there is a detection
    if pir>0:
        #Debug output - Can be removed
        print("Detection")
        #Send signal to Pybytes
        pybytes.send_signal(1,pir)
    else:
        #Debug output
        print("No detection")
    #Wait for 5 seconds before next check
    time.sleep(5)

```
* Choose to savev your file (CTRL+S or FILE MENU and Save)

* Make sure your LOPY4 board is connected to USB and that COM port is shown in green. Otherwise press the red handle in the Pymakr addon

*Not connected*
![](https://i.imgur.com/LYukB8J.png)

*Connected*
![](https://i.imgur.com/cB77y5R.png)

* Press the button "Upload project to your device"
![](https://i.imgur.com/3NTADJj.png)

* After a few seconds the device will connect to your Wifi an send signals to Pybytes services. Return to Pybytes webpage and check your device page and the menu "Signals". 
* ![](https://i.imgur.com/urjDnyh.png)

* The signal is automatically configured for you but you can change the name of the signal by pressing the pen (edit) to give it a relevant name.
![](https://i.imgur.com/X1p4Epa.png)

# Conclusion
Detection data is sent through the internet to Pybytes through WIFI. My code checks precense every 5 seconds and then sends data if sensor is triggered. 

Data is presented in the graphical user interface of Pybytes:
![](https://i.imgur.com/9w8321L.png)

This i enough to use the data to trigger changes in room temperature. For lights 5 seconds is a bit slow and in the future I might have to change the behaviour for this. And next step is to create a API connection between Pybytes and my Home automation system to trigger changes in room climate. I also have to create a neat enclosure for the board and sensor. Important is to place the sensor so that it only triggers when it is supposed to.

![](https://i.imgur.com/6Ej9FsR.jpg)

