# IOT - REPORT ## Temprature by motion #### David Heinebäck - du222aa This small IoT project is a thermomenter that is activated by a motion sensor. Time estimate: 3-4 hours ### Objective I live in a aparment where the heat is a real struggle in the swedish summers and therefore I desided to create a simple thermometer that is activated by detecting nearby motion and send the current room temprature to pybytes. I can then provide the board of my housing association with the data as motion to install air conditioners in the apartments. ### List of Material In this project I have chosen to work with the Pycom LoPy4 device. All other necessary sensors and matial is listed below. | Type | Description | Picture | Approx Price | Bought at | | -------- | -------- | -------- |-------- | -------- | | LoPy4 | LoPy4 is Micropython-programmable quadruple bearer board. It works with LoRa, Sigfox, WiFi and Bluetooth. [Info on LoPy4](https://pycom.io/product/lopy4/)| ![](https://i.imgur.com/0zzwonD.png)| 1000kr / 100€ (Price includes all listed as part of bundle) | [Electrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/)| | Expansion board | Used to connect LoPy4 to for example breadboard, computer or batterypack | ![](https://i.imgur.com/ZQBdGGW.png) | Part of bundle | [Electrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) | | Breadboard | Makes it easier to connect multiple sensors to the expansion board | ![](https://i.imgur.com/pE1n9EL.jpg)| Part of bundle | [Electrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) | | Jumper wires | Connects expansion board with the breadboard and by extension the sensors. Only Male-Male in bundle but can convert to female by using tongs |![](https://i.imgur.com/wqxwoC4.png)| Part of bundle (but I bought som extra. 30SEK / 3€) | [Electrokit](https://www.electrokit.com/produkt/labbsladdar-100mm-hane-hane-30-pack/) | | Temprature sensor | Detects the surrounding temprature | ![](https://i.imgur.com/Z0xYoox.png) | Part of bundle | [Electrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) | | Battery case | Holds batteries to make device independent of usb connection | ![](https://i.imgur.com/xZwbcjt.png) | 20SEK / 2€ | [Electrokit](https://www.electrokit.com/produkt/batterihallare-3xaaa-med-strombrytare/) | | Motion sensor | Detecting nearby motion | ![](https://i.imgur.com/9XA2YeU.png) | 50SEK / 5€ | [Electrokit](https://www.electrokit.com/produkt/pir-rorelsedetektor-hc-sr501/) | ### Computer setup Sinse I have previous knowledge with Visual Studio Code I decided to use it as the IDE for this project. In VSC I installed the PyMakr plugin to help me upload the code to the device. PyMakr is dependent of Nodejs so make sure to download it before installing PyMakr. Links to the softwares is stated here: * [Visual Studio code](https://code.visualstudio.com/) * [Node js](https://nodejs.org/en/download/) * [PyMakr](https://marketplace.visualstudio.com/items?itemName=pycom.Pymakr) ### Putting everything together ![](https://i.imgur.com/Umfnm9N.png) This picture shows how the sensors is connectected via the breadboard to the expansion board. * The black wires are connected to GND port at the expansion board and the "-" on the breadboard. * The red wires are connected to the 3v3 port on the expansion board and connects with the "+" on the breadboard. * The yellow wires shows the sensordata pins and which pin that correspondes on the expansion board. * Pin 13 = Motion sensor * Pin 15 = Temprature sensor ### Platform The data is uploaded to a free account at https://pybytes.pycom.io/ so that I can access the data from wherever. It was easy to connect my the device to pybytes there is a great step by step guide in the [documenation](https://docs.pycom.io/pybytes/connect/#app) at pycom. ### The code The project is coded with MicroPython. ``` from machine import ADC import time from machine import Pin from machine import Timer #config motionDetected = 1 noMotionDetected = 0 hold_time_sec = 5 pir = Pin('P13',mode=Pin.IN) chrono = Timer.Chrono() chrono.start() adc = ADC() tempSignal = 2 print("Starting Detection") while True: if pir()==motionDetected: print(chrono.read(), "Motion Detected!") temprature = adc.channel(pin='P16') actualTemp = (temprature.voltage() - 500)/10 print(hold_time_sec) print('Current room temprature: ') print(actualTemp) pybytes.send_signal(tempSignal, actualTemp) if pir()==noMotionDetected: pass time.sleep(hold_time_sec) ``` When the LoPy4 is started the device is connected to my personal WiFi but first it also tries to connect to the Helium LORA network to be able to transmit the data to the pybytes account. The data is sent over MQTT protocol and is transmitted when the motion sensor is activated. After each activation of the motion sensor there is a hold time for five seconds to not send to many data points at the same time. On every transmition the data is saved to the pybytes account and the data is then presented both in a line chart and in i list view. ![](https://i.imgur.com/h9IAGeD.png) ![](https://i.imgur.com/pkWI5SS.png) ### Finalizing the design I cut some holes in the box where I got the material to make it look more nice. This could have been done even more nicely mayby by some 3D printed casing that is custom fitted for the devices. Another thing that I could have done differently is to ad a LED or program the LED on the LoPy4 to tell me when the device is connected to LORA or Wifi. I have this as text in the console but when using the batterypack this is not shown. | Picture shows | Picture | | -------- | -------- | | Outside | ![](https://i.imgur.com/pzAXggU.png) | | Inside | ![](https://i.imgur.com/eMAcKtf.jpg) |