# Simple heat monitoring, with lights - Applied IoT # Introduction **Love Andrén - la223xs** This project aims at creating a simple solution for heat monitoring, using IoT solutions, when brewing different beverages, such as kombucha or ginger ale. Most homebrewing requires a certain temperature for the right bacterial and yeast cultures to grow. A too low or too high tempature could cause unwanted bacterias or even mold to start forming. In most cases this is a hard thing to monitor and mostly done by trial and error. This solution aims at creating an easy way to see if the tempature is between the right degrees celcius by using a heat sensor and three LED lights. At the same time also allowing the brewer to look at what the tempatures during the day using PyBytes. Estimated price: **75.63€** Estimated hours: **4** # Objective The following points are the objective of the proejct: 1. Getting a better understanding of IoT. 2. Helping homebrewers with tempature monitoring. 3. Saving data for further analysis and a better understanding for how the tempature has been at that spot during the day. 4. Insight into how to visualize data in a way to could be understand easily. 5. Insight into further developing other IoT solutions or making a more advanced version of this. # Material **Note**: Prices might vary since this is mostly taken from a swedish website and the current converstion rate SEK-EUR. | Item | Price | Link | Notes | | --------------- | ------ | ------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------- | | LoPy4 | 38.45€ | [Pycom](https://pycom.io/product/lopy4/) | Our micropython board, great choiche for IoT projects and solutions. | | Expansionboard | 17.60€ | [Pycom](https://pycom.io/product/expansion-board-3-0/) | General purpose expansionboard for Pycom boards | | MCP9700 | 0.78€ | [Electrokit](https://www.electrokit.com/produkt/mcp9700-e-to-to-92-temperaturgivare/) | Heat sensor. Alternative would be TMP36 since the installation is identical. | | Micro USB cable | 5.75€ | [Electrokit](https://www.electrokit.com/en/product/usb-cable-a-male-micro-b-male-5m/) | For connecting the Lopy to a computer. You might need a longer depending on where you want to place your device | | Breadboard | 5.75€ | [Electrokit](https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/) | Connection between expansionboard and sensors | | Jumper wire | 3.80€ | [Electrokit](https://www.electrokit.com/en/product/hook-up-wires-w-pins-for-breadboard-smooth-65-pcs/) | Standard wires for connection between breadboard and extensionboard | | LED Orange | 0.50€ | [Electrokit](https://www.electrokit.com/en/product/led-5mm-orange-diffus-700mcd/) | Basic orange LED light | | LED Green | 0.50€ | [Electrokit](https://www.electrokit.com/en/product/led-5mm-gron-diffus-80mcd/) | Basic green LED light | | LED Red | 0.50€ | [Electrokit](https://www.electrokit.com/en/product/led-5mm-rod-diffus-1500mcd/) | Basic red LED light | | Resitors 330ohm | 2.00€ | [Electrokit](https://www.electrokit.com/produkt/motstand-metallfilm-0-6w-1-330ohm-330r/) | Resistors for the LED lights. Need three in total. | # Computer Setup ## Firmware To start you might need the firmware on your expansionboard, so to be on the safe side this is something that should at least be checked. Start off by connecting the LoPy to the expansion board as seen on the picture below left image. ![](https://i.imgur.com/YbYsOCv.png) <p style="text-align: center;"> <a href="https://docs.pycom.io/gitbook/assets/expansion3-pinout.pdf">Source</a> </p> Afterwards download the Pybytes firmware update tool [here](https://docs.pycom.io/updatefirmware/device/) and install it. Afterwards connect your USB-cabel to the expansionboard, remeber to be careful and not use to much force. Then finally plug it into an USB-port on your computer and run the update tool. The tool should automatically detect which port your device is connected to, if not make sure that the USB-port your using regonizes the device. For windows user this is done through the device manager. Otherwise select **Pybytes** as the type you want to update and follow the instruction in the tool. ## Software and IDE Before being able to run code and upload code to your device there are some softwares that need to be installed. Note that parts of this could be skipped if you already have NodeJS installed and VScode/Atom. For the latter just remember to install the Pymakr plugin. ## NodeJS NodeJSFirst of go to [NodeJS website](https://nodejs.org/en/) and download the latest LTS version(green big button on the homescreen). When the download is done follow the installation steps. One of the steps will be namned **Tools for native Modules**, this does not need to be installed for this project. Once it is done restart your computer. ## VSCode The IDE of choice for this project will be VSCode. Reason behind it is because it runs on most operating systems (Windows, Linux and macOS) and that is is easy to use. Alternative would be Atom since most thing are similar. To get started download the installer from the [VSCode website](https://code.visualstudio.com/Download), once it is downloaded run it and follow the steps in it for installation. I recommend leaving most of the selection as it is, and in the step for additional task I recommend the following: * Add open with code (both options) * Add to PATH Once the installation is finished run VSCode. Before we are able to start programing we need to install the Pymakr plugin. On the left sidebar in VSCode press the **Extensions** icon and in the search field that pops up after ward search for **Pymakr**. There should be one results, press download on it, see picture below . Once it is installed VSCode asks for a reboot to finish the process. ![](https://i.imgur.com/NvOQa6X.png) To end this section I recommend creating a project folder so that it is already done. To do this click `File->Open folder...` or just `Ctrl+K Ctrl+O`, this will open your file explorer where you can navigate to where you want to save the project and create a new folder. I suggest naming the folder something related to the project. Then create the two following files by using `File->New file` or `Ctrl+N`: * `main.py`. Here the main code of the project will be stored. In this project we will only be using this file. * `boot.py`. Here we will store code that could clutter upp the main file. This code is run when the device boots. If you are having problems with network connections there is a code you can paste in here further down. You could also create a folder namne `lib`, if you are planing to further develop this and use other sensor that requires libraries to run. Afterwards your workspace should look somehting like this! ![](https://i.imgur.com/N4qTlOf.png) One more thing to do is adding your serial port your device is connecting to as default for all projects. This is done by using `Ctrl+Shift+P` and searching for "Pymakr>Global settings". In this settings file change the value next to address to the port you are using, for me it looks like this: ![](https://i.imgur.com/TErLjRf.png) ## Pybytes Our main way to represent our data will be using Pybytes online dashboard as the platform. This is a cloud based plattform that is available to use for anyone to use with a Pycom device. It offers an easy way to keep track of your device, help with visualization of data and is free. Here we will also configure our communication protocol, in this case we will be using WLAN(WiFi) and we will be using MQTT to send data to Pybytes (this is the default when using WiFi). The reasoning behind this is that it is easy to configure thanks to Pybytes and also the most smooth way of handling communcation. The free version of Pybytes offers a storage of 5MB, this is plenty for this project since we will be sending 30B-40B each hour. Quick maths reveals that we can send data about 125 000 times before using all storage. To get started create an account at https://pybytes.pycom.io/. Then follow the instructions on how to add your device and connect to your network [here!](https://docs.pycom.io/pybytes/gettingstarted/). **Note**: In my case I was not able to activate my device through the firmware tool and instead did it through the VSCode with the pymakr plugin. You can find the instructions under `Devices->"Your device name"->"Provisioning"->With pymakr plugin`, see picture below. ![](https://i.imgur.com/Jv8DC18.png) If you want to know if your device is connected to your network try this code snippet in the pymakr console in VSCode: `pybytes.send_signal(1, "hello world")` and if all works it should appear in the "Signals" tab on Pybytes. Another sign of success is that when connection your device to your computer there should be a similar message to this in the terminal in VSCode: **BILD** # Hardware Setup Before starting to connecting sensor and cables make sure that your device is **not connected** to anything. The following picture is the scheme created in Fritzing. ![](https://i.imgur.com/ySglSBA.png) A few notes: * The sensor should be connected to **P16**. * The three lamps are connected to **P22**, **P21** and **P20**. * Red is for voltage output and blue is for grounding. Red should be connected to the **3V3 pin** and blue to the **GND pin**. * Make sure the sensor is connected correctly, refer to the [sheet for MCP9700](https://www.electrokit.com/uploads/productfile/41011/21942e-2.pdf). If you are unsure where theese are please refer to the [pinout diagram for the LoPy4](#Sources). # Code . This following code should be copy-pasted into your`main.py` ```python= import time import machine from machine import Pin adc = machine.ADC(bits=10) #Important that this pin is the same one as the sensor is conected to tempPin = adc.channel(pin='P16') #Same as above, remember pin numbers you used for the led lights ledOrange = Pin('P22', Pin.OUT, pull = Pin.PULL_DOWN) ledGreen = Pin('P21', Pin.OUT, pull = Pin.PULL_DOWN) ledRed = Pin('P20', Pin.OUT, pull = Pin.PULL_DOWN) while True: millivolts = tempPin.voltage() celsius = (millivolts - 500.0) / 10.0 if celsius < 23: ledOrange.value(1) ledGreen.value(0) ledRed.value(0) elif celsius > 28: ledOrange.value(0) ledGreen.value(0) ledRed.value(1) else: ledOrange.value(0) ledGreen.value(1) ledRed.value(0) pybytes.send_signal(1, celsius) print("Tempature sent = {}".format(celsius)) #Send every 30 minutes time.sleep(1800) ``` Some quick notes: * If you are using different pins for the sensor or LED lights remember to change theese in the code according to your own. * The `time.sleep()`function uses seconds and can be changed if you want to send data to Pybytes at a lesser or more frequent rate. I have it at 1800 seconds as base. * The `if-else` parameters could be changed if you want another inbetween tempature. In this case 28°C is the highest I want and 23°C the lowest. * At line 33 when the data is getting sent to pybytes we are using singal 1. This could as well be changed to be another signal. The code first sets variables for the sensor and LED lights. Afterwards it enters an endless loop. In this loop it first calculates the tempature with the help of the current milivolt on that pin (calculation taken from [here](https://hackmd.io/@lnu-iot/BJnuGmJh_#Basic-Analog-sensors)). Afterwards it checks if the tempature is higher or lower than the set paramters and lights the according light. Finally it sends the tempature to Pybytes. Other than that you should be able to run the code by pressing the "Run" button at the bottom panel in VSCode. I prefer to upload the code directly to the device, this is done by just pressing the "upload" button right next to the run button. This will make sure that the code runs when the device boots. # Visualization To visualize the data we are using the Pybytes dashboard as earlier mentioned. Our main concern is to see how the tempature has been during the day when where are not present. Therefore a simple graph that displays the tempature works fine for this. Start by following the guide provided [here by Pycom](https://docs.pycom.io/pybytes/dashboard/). For the grapth I have setup, the following settings are used and this is how it looks like: ![](https://i.imgur.com/dKcwFJR.png) The pull size of the data could be changed according to your own needs, if you are sending data at a more frequent rate you might want a larger pull size and vice versa. # Finalizing the design ## Summary In the end this project went pretty well with som minor hickups. To start of I almost had an accident that could have ended badly. For a period of time when I was not home and the device was running it started reporting extremly high tempatures. This was caused by two non insulated wire touching eachother. Other than that almost everything went supringsly smoothly. Right now it looks like this when up and running: ![](https://i.imgur.com/Zd62Fts.jpg) Right now it is placed on my shelf right next to my jar and is connected to a power outlet with the help of 5m USB cabel. Even if this was a quite simple project I am happy with the results and that it went so smoothly. Since this was my first time doing a IoT project I decided to do something easy but in retrospect there was more that could have been done. ## Further work There are already some things that I want to add in the future. One is creating a box or something to place the devices in so they are not out in the open all the time. The other two main things that would improve the functionallity is adding a sensor for humidity and with that a fan next to the device that activates if the humidity is to high. ## What could be done better The main thing that could have been done better would be the putting togheter the the sensor and lights on the breadboard. Instead of doing a propper sketch I started doing it directly on the board. This ment I spent a lot of time just trying out things to see what worked. Finally I came to my senses and made a sketch and put it togheter after that. Luckly I did not damage any components during this but that might happen if you are unlucky. # Potential problems If you are having problems getting this to work there are plenty of resources online, such as the Pycom docs, that could help. Otherwise try a quick search on your problem and their should be answers. The rest of this is covering some problems that I had and how they where solved. ## Hardware Make sure none of the unisolatedcables (LED lamps, resistors) are touching eachother. Update your firmware even if you just bought the latest verision of the LoPy4 and expansionboard. ## Connection If you are not able to connect your device through the Pybytes dashboard tutorial, try [this code](https://docs.pycom.io/tutorials/networks/wlan/#:~:text=Scan%20for%20WiFi%20networks) from the Pycom docs and paste into your `boot.py`. Remember to change the **WiFi ssid** and **password** to your networks. # Changelog ### 2021-07-30 First published ### 2021-08-12 # Sources * [Expansionboard pinout diagram](https://docs.pycom.io/gitbook/assets/expansion3-pinout.pdf) * [LoPy4 pinout diagram](https://docs.pycom.io/gitbook/assets/lopy4-pinout.pdf) * [Update pycom device](https://docs.pycom.io/updatefirmware/device/) * [Code on GitHub](https://github.com/LoveAndren/Kombucha-Heat-Monitor) * [WiFi examples pycom](https://docs.pycom.io/tutorials/networks/wlan/) * [Pycom docs](https://docs.pycom.io/)