# Monitor room temperature and get alerted **Andreas Höglund(ah225nr)** In this project we are going to monitor the temperature on our balcony and also send an alert in case it gets too hot. To do this we are going to use a graph which will show us the temperature and if the temperature reaches a certain threshold you will get notified via mail. **Estimated time:** 10-15 hours. # Objective **Reason behind this project:** - **Professional reason:** I wanted to learn more about IOT while also making good use of the things that i learn from studying it. This project is very basic but still very usefull in any situation where you want to measure the temperature in a room in a more curious fashion. I also want to be able to see the data in a graph since it becomes more interesting that way and add alerts for more functionality - **Personal reason:** Since my dog spends alot of time on our boiling hot balcony i wanted to monitor the heat to know when it's time open a window or maybe start a fan. I also think this is a perfect first project in IOT and python. - **TL;DR:** My dog doesn't seem to notice when it's over 25 Celcius and i think IOT-stuff is cool. # Material All of the mentioned material is bought in a bundle on Electrokit. **The bundle(950 SEK):** https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/ 1. **Temperature sensor MCP9700(15sek):** This sensor measures the temperature. ![](https://i.imgur.com/Az2TwY4.jpg =100x100) 2. **LoPy4 with headers(350sek):** Connects to the expansion board and is used for Wi-fi, LTE etc.. ![](https://i.imgur.com/kY1YKOB.jpg =100x100) 3. **Expansion board(200sek):** Used to connect all the wires and send information too and from the LoPy4. ![](https://i.imgur.com/agIvJJB.jpg =100x100) 4. **Micro USB cable(50sek):** Connects the LoPy4 and expansion board to a computer. ![](https://i.imgur.com/v349A4a.jpg =100x100) 5. **Wires(50+ 22sek):** For this project you will not need more than 5 wires. ![](https://i.imgur.com/dO8UFqT.jpg =100x100) 6. **Breadboard(60sek):** Used for connecting the wires and holding the sensor in this project. ![](https://i.imgur.com/I5xGTi3.jpg =100x100) # Computer setup The IDE im using for this project is Atom. I chose Atom since it's the IDE that im most comfortable with. In order to use Atom with your pycom device you need to install Pymakr as a package in Atom. But before you do this you need to install [Node.js](https://nodejs.org/en/). In order to reach the package installer in atom, go here: **Settings -> Install**. **The install package page look like this:** ![](https://i.imgur.com/KJsA5u4.png) Pymakr runs with micropython and through pymakr you upload the code to the device which then in this case connects to pybytes. To upload the code you simply need to press upload: ![](https://i.imgur.com/G5t2wjE.png) **About the code:** While this project necessarily doesn't require alot of code, there are still alot of processes to go through in order to get it all running. The code is uploaded to the device which then sends the data gathered to the cloud in form of a signal that we then represent in a graph in Pybytes. In order to get the alert system going you need to import a library called **uMail** which you can find [here](https://github.com/shawwwn/uMail). # Putting everything togehter My setup was heavily inspired by another tutorial, but i found it to be the most usefull for this project. It's not mutch, but it's honest work. **Setup:** 1. Green cable(Output): Sends data to the device and is connected to pin P16 for analog to digital conversion([ADC](https://docs.pycom.io/firmwareapi/pycom/machine/adc/#app)). **D26 -> P16.** 2. Blue cables(Ground) are connected to GND. **A27 -> GND.** 3. Red cables(3V3) are connected to the positive voltage leg and connectes to 3V3 on the expansion board. **A25 -> 3V3.** 4. Temperature sensor: Is placed in slots 25, 26 and 27. Slot 25 is the positive voltage leg on the sensor, slot 26 is the output and slot 27 is ground. **Circuit diagram:** ![](https://i.imgur.com/vFp9FrC.png) **FYI:** The black lines connected to the temperature sensor is not part of the setup. It's just an easier representation of how it's connected to the breadboard. # Platform The most effective platform to represent the data is Pybytes. Pybytes can be used to recieve signals and to display the data from the sensor i used this function to send the temperature as signals, which i then put in a graph. Pybytes is a free cloud based platform. All you need is a LoPy device(which in itself costs money) and you are good to go. # The code Library i used: [uMail](https://github.com/shawwwn/uMail). This file runs the main program. Firstly we define what pin on the expansion board is used. After that we calculate the data recieved from the temperature sensor in order to then send it to Pybytes. Now the best part of this project is the alert functionality which starts with the if-statement. If the temperature goes over 25 celcius we then send a mail. If it's below 25 celcius the program just keeps sending data to Pybytes. **Main.py:** ```python= import machine import time import os import pycom import umail adc = machine.ADC() apin = adc.channel(pin='P16') while True: millivolts = apin.voltage() celsius = (millivolts - 500.0) / 10.0 print(celsius) pybytes.send_signal(1, celsius) time.sleep(1) if celsius > 25: smtp = umail.SMTP('smtp.gmail.com', 465, ssl=True) # Gmail's SSL port smtp.login('Sender mail', 'password') smtp.to('Reciever mail') smtp.write("From: TempAlert <Sender mail>\n") smtp.write("To: TempReciever <Reciever mail>\n") smtp.write("Subject: TempAlert\n\n") smtp.write("Temperature over 25C! \n") smtp.write("...\n") smtp.send() smtp.quit() ``` The boot-file is the first file that runs and is usually used for Wi-fi connection and such. This boot file is borrowed from another [tutorial](https://hackmd.io/@lnu-iot/r1bGPUOhu). **Boot.py:** ```python= from network import WLAN import machine wlan = WLAN(mode=WLAN.STA) nets = wlan.scan() for net in nets: if net.ssid == "SSID: print('Network found!') wlan.connect(net.ssid, auth=(net.sec, "NETWORK PASSWORD"), timeout = 5000) while not wlan.isconnected(): machine.idle() print('WLAN connection succeeded!') print(wlan.ifconfig()) break ``` The json file is used for username and password input for WI-fi and Pybytes. **pybytes_config.json:** ```json= {"network_preferences": ["wifi"], "device_id": "ea68f5fc-3a51-4650-bc18-bbc4ef1586ef", "server": "mqtt.pybytes.pycom.io", "username": "pybytes username", "ota_server": {"port": 443, "domain": "software.pycom.io"}, "pybytes_autostart": true, "wifi": {"ssid": "ssid here", "password": "network password here"}, "wlan_antenna": 0, "dump_ca": true, "ssl": true } ``` # Transmitting the data / Connectivity The data is transmitted over the internet to Pybytes and i use Wi-fi. The transport protocol i use is SMTP over SSL/TLS for mail. The data is sent every second except for a 1 second break when the temperature goes over 25 celcius. I chose to do this so that you can easily see in the terminal if an alert is sent. I did try to use the antenna, but since i wasn't able to connect to any of the recommended networks where i live, i ended up using Wi-fi. # Presenting the data This graph in Pybytes displays the temperature about every second, if an alert is sent there will be a 1 second delay. **Graph:** ![](https://i.imgur.com/et9VBlE.png) Since this is a live preview of the temperature the data is never stored in any database, but the alerts will be saved in the mailbox as long as the user wants. **This is my main dashboard in Pybytes:** ![](https://i.imgur.com/g7L6dZp.png) In Atom the data is represented like this: ![](https://i.imgur.com/yOu7nBT.png) # Finalizing the design Taking this summer course has been really fun and i loved every part of it. I am also very happy to finally try python out. I did try [Grafana](https://grafana.com/) at first, but that requires a more complicated process which is more fun, but for this project Pybytes is the preferred platform. I did give Grafana a go for data presentation since i was recommended to do so, but since i already had a functioning alternative on Pybytes i stuck with that. If i could expand the project even more i would have used more IOT-stuff, like smart plugs e.g starting a fan when it's too hot or something similar to that. Overall i think i did quite well and i'm happy with how my first IOT-project went. **Final result:** ![](https://i.imgur.com/bAI8NZW.jpg =300x300)