# Indoor Temperature monitoring ## *The Easiest Way to Get Into IoT World* ###### Author: Assad Mohammad Tahir - am224hs ###### tags: `pycom` `TMP36` `wifi` `Pybyte` `Indoor Temperature` ![](https://i.imgur.com/efF697F.jpg) ### Table of Content [ToC] ## Project Description Indoor temperature monitoring device to be used in small offices and rooms. :office: #### Time to complete this tutorial The project consists of 2 parts, `the IoT device` and the `Backend-server` that connects to the device. - `The IoT device` 5 to 10 hours, depending on your programming and reading comprehensive. - `Backend-server` 3 to 5 hours, since we will follow a very easy and clear manner. ## Objective In Sweden, indoor temperature can vary extremely between summer and winter. During the summer the optimum indoor temperature is betweeen 14-16 °C; however, people prefere 20 °C at least during the winter. Thus indoor temperature is important to be monitored. In this project, we intend to monitor indoor temperature as well as display the data in a pleasing manner. Among the useful implications of this project is that it provides an indication of whether to turn on the heater or air conditioner at certain times of the year, to ensure that residents and employees are comfortable indoors. ## List of Material - **LoPy4** Basic bundle from Elektrokit [849 SEK](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-basic-bundle/) A great option for an IoT project that works with LoRa, Sigfox, **WiFi**, and Bluetooth. ![](https://i.imgur.com/xXO71jq.png) - **Expansion Board 3.1** bought in 2021; I see it's not available at Elektrokit website. You may look at another shops or websites like [Kjell & Company](https://www.kjell.com/se) ![](https://i.imgur.com/730kJJK.png) - **Breadboard**(400 Connections) [59 SEK](https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/) This makes it simple for you to connect your sensors, jumper wires, etc. It facilitates connections and highly useful. ![](https://i.imgur.com/61BYurA.png) - **Temperature Sensor(TMP36)** [46 SEK](https://www.electrokit.com/produkt/tmp36-to-92-temperaturgivare/) ![](https://i.imgur.com/PnHv8ja.png) - **Power Bank** from Clas Ohlson [200 SEK](https://www.clasohlson.com/se/Powerbank-6700-mAh,-Clas-Ohlson/p/38-9060-3) To be used as rechargable energy source, can last for 24 hours ![](https://i.imgur.com/U6w5NEf.png) - **Wi-Fi Hotspot** from Kjell & Company [499 SEK](https://www.kjell.com/se/produkter/natverk/routrar/4g-router/d-link-dwr-932-4g-router-med-modem-n300-p61672) For indoor measurements, wifi is the best solution. It is difficult to connect your device to a public wifi, though. My laptop's internet speed was getting too slow as I was setting up a hotspot for my device while I was seated in the university. Thus, I decided to buy a separate wifi hotspot to make it easy for myself and future uses. ![](https://i.imgur.com/5BtvhfP.png) Please note, the **antenna** isn't needed if you use wifi; I connected it in the beginning when I was planning to use LoRa. It's shown in the first image after the title. ## Computer Setup ### Installing **Atom** IDE for Windows In case you use Mac or Linux, go through this [tutorial](https://hackmd.io/@lnu-iot/SydH7MTcw) For Windows follow these stips: - Download and install [Node js](https://nodejs.org/en/). - Download and install [Atom](https://atom.io/) - **Open** Atom and the package manager from: **File** >> **Settings** >> **Install**. - Look for **Pymakr** and **Install** it. (It takes a while; wait until shows it is successfully installed). ![](https://i.imgur.com/OAh2oUl.png) ### Firmware Update (PyCom Boards) With all PyCom development boards (**Lopy4** in this tutorial) we need PyCom Firmware Update to run our Codes. Follow this [tutorial](https://hackmd.io/@lnu-iot/SJ91R_jSO) ### Running The Code - Open **Atom**, disconnect from your board, then reconnect. If Atom isn't recognized immediately, use Connect Device to access REPL. - On the REPL, try print ("Hello from PyCom") to see if the message is successfully displayed. ![](https://i.imgur.com/eEkn9ri.jpg) ### project Folder: - Create a folder (on your desctop for example); name it `First Project`. - In **Atom** under **file** click on `Add Folder` select your `First Project` folder. - Right-click on your `First Project` folder from the left panel and choose `New File` to create a new file, in the opened box enter `main.py` and press enter. - Create another file by the sameway in the previous step and name it `boot.py` - You may also crate another file for Library but we don't need it for this sensor; so it's not created. ***Now We are ready for the Hardware*** :smile: ## Putting Everything Together You will need all the material described above with **three jumper wires** and a **USB Cable**. #### Breadboard Connection - Connect the TMP 36 sensor to the breadboard (see the image below) on the **e** row (Pins **25**,**26**,**27**). - Connect the Red jumper wire to the breadboard on row **d** pin **25**. - Connect the Blue jumper wire to the breadboard on row **d** pin **27**. - Connect the Green jumper wire to the breadboard on row **c** pin **26**. ![](https://i.imgur.com/PEt17j1.jpg) ###### `Breadboard Connection` #### Lopy 4 & Expansion board 3.1 Connetion - First connect the Lopy4 to the Expansion board (Be sure about the connection direction - see the image please) - Then The other part of the jumper wires should be connected to the Expansion board 3.1 as described and shown in the image below. - Connect the Red jumper wire to **3V3** - Connect the Blue jumper wire to **GND** - Connect the Green jumper wire to **P16** - Connect the USB Cable to your pycom ![](https://i.imgur.com/R95Zxro.jpg) ###### `Lopy 4 & Expansion board 3.1 Connetion` ## Platform ([Pybytes](https://pybytes.pycom.io/)) Since this tutorial is a guide to get into IoT world with an easiest way. (**Pybytes**) PyCom's device management platform is designed to help you get the most out of your Pycom boards. It provides data visualization, notifications (such as battery level), and geo-location, and it is completely free to use. **PyBytes** only keeps your data for a month, so if you want it to last longer, you'll need to integrate your project with third-party services or IoT platforms. PyBytes supports AWS IoT, Microsoft Azure, Google Cloud IoT, and Web Hooks integration. All you need is: - Create an account on Pybytes - Register your device To Do this follow [Pybytes (PyCom MQTT)](https://hackmd.io/@lnu-iot/rk8uGYo_c) ## The Code `main.py` will be used to write your code that run your sensor. ```python= import machine import time adc = machine.ADC() apin = adc.channel(pin='P16') while True: millivolts = apin.voltage() degC = (millivolts - 500.0) / 10.0 degF = ((degC * 9.0) / 5.0) + 32.0 pybytes.send_signal(1, degC) pybytes.send_signal(2, degF) pybytes.send_signal(3, millivolts) print(millivolts) print(degC) print(degF) time.sleep(5) ``` Some notes about the code: - line 5 : (pin='P16') means read the voltage from this pin in the expanboard. - lines 9 & 10 : Converts the voltage to the sensed temperature in (Degree Celsius °C) and (Degree Fehrenhite °F) using some equations. - lines 11-13 : sends the data to Pybytes platform to be presented. - lines 15-17 : print sensed temperature and show it in Atom. - lines 19 : will let the sensor take a reading each 5 seconds. `boot.py` will be used to write your code that connect your device to the wifi. ```python= def do_connect(): from network import WLAN import time import pycom import machine pycom.wifi_mode_on_boot(WLAN.STA) # choose station mode on boot wlan = WLAN() # get current object, without changing the mode # Set STA on soft rest if machine.reset_cause() != machine.SOFT_RESET: wlan.init(mode=WLAN.STA) # Put modem on Station mode if not wlan.isconnected(): # Check if already connected print("Connecting to WiFi...") # Connect with your WiFi Credential wlan.connect('**********', auth=(WLAN.WPA2, '***********')) # Check if it is connected otherwise wait while not wlan.isconnected(): pass print("Connected to Wifi") time.sleep_ms(500) # Print the IP assigned by router print('network config:', wlan.ifconfig(id=0)) def http_get(url = 'http://detectportal.firefox.com/'): import socket # Used by HTML get request import time # Used for delay _, _, host, path = url.split('/', 3) # Separate URL request addr = socket.getaddrinfo(host, 80)[0][-1] # Get IP address of host s = socket.socket() # Initialise the socket s.connect(addr) # Try connecting to host address # Send HTTP request to the host with specific path s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8')) time.sleep(1) # Sleep for a second rec_bytes = s.recv(10000) # Receve response print(rec_bytes) # Print the response s.close() # Close connection # WiFi Connection do_connect() # HTTP request http_get() ``` The important note here is : Change the stars in line 14 to your **wifi hotspot SSID** and the **password**. `Save` your project. ## Transmitting The Data (Connectivity) - The device sleeps for 5 seconds after transmitting data; this can be changed in the attached code; and I recommend 10 minutes for indoor use. - WiFi is the wireless protocol that is used. Because the project is intended to be used internally, it makes sense to simply connect to the office or home wifi. In addition WiFi uses less energy.Thus, I used the power bank. - MQTT **Pybytes.sendsignal(signalnumber, value)** function from the PyBytes API, used to send signals to PyBytes. ## Presenting The Data With the addition of a widget, Pybytes offers attractive ways to present your data in the form of tables, graphs, line charts, and symbols. Additionally, it can be organized according on your preferences. Here I presented the temperature in degree Celsius ℃ and degree Fahrenheit °F. You may also change the data duration to be presented by seconds, minutes, hours, ...etc. ![](https://i.imgur.com/R0jvt7c.png) ![](https://i.imgur.com/JKrfl6r.png) ## Finalizing The Design Overall, this is a useful project that might help you enter the Internet of Things (IoT) world. Some thoughts: - Reading the documentation thoroughly helps cut down on debugging time. - It can be very time-efficient to plan how data will be handled before the data is really present. ## Future Plans I've previously planned how to use IoT in my research field, therefore this is the beginning point to **study and monitor the Climate Change in our planet**. ## References [IoT Road Map (22ST - 1DT305 - Tillämpad Internet of Things, Introduktion - 7,5 hp course)](https://lucid.app/lucidspark/b50f4c16-332a-4454-ab1e-af07eac2dc33/edit?invitationId=inv_5cb561af-cb1f-4b3b-bdc2-be6348928f3c#)