# 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"*

* In the search box type "Pymakr" and choose "Install"

# 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

* Add a new Wifi-network by clicking on the "ADD WIFI" button

* Define your Wifi network settings and hit "Save"

* Go back to the first landing page and click "Add Device"

* Next option is to choose from adding the device by USB or App. I choose USB

* Choose LOPY4 device in the list

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

* Give your device a cool and relevant name and klick "SAVE"

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

* 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

* Connect your LOPY4 device to USB on your computer
* Click on continue on the first guide page

* Make sure you have closed Atom and VS Code IDE software with plugins that talks to the LOPY4 board and click continue

* 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"

* Paste your configuration code you copied from Pybytes provisioning earlier

* Your device will now be updated with your pybytes firmware and your network configuration

* Hopefully the guide finishes with a successful results and you can now close this app

* Pybytes webpage for your device should soon report a successful connection

# Connecting PIR sensor
* The SR-HC505 has 3 pins

* 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

# 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"

* Create a new folder and give it a relevant name and press "Select Folder"

* Right click in the left pane below the name of your newly created folder and name it "main.py"

* 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*

*Connected*

* Press the button "Upload project to your device"

* 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".
* 
* 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.

# 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:

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.
