owned this note
owned this note
Published
Linked with GitHub
# Tutorial on how to build an air quality and temperature sensor
#### By Fritiof Andersen Ekvall, fa222xh
###### tags: `IoT,LNU,LoPy4,Pycom`
- Table of Content
[ToC]
## :memo: Project overview
This project describes how to easily create a sensor that monitors the air quality and temperature of your home. This involves some light tinkering and just a pinch of programming.
Following this guide will probably take about 2-3 hours but someone with experience could complete the project in under an hour.
### Purpose of project
I chose this project because I was curious how the air quality and temperature changes during a nights sleep, does open windows equal less CO2? Does the construction site next to my apartment building whey in? What is the peak temperature when trying to sleep during a heatwave?
### Material used
- LoPy4: Micropython-programmable microcontroller
- Expansion Board: Allows the LoPy4 to connect with USB and pins
- Breadboard: Base for connections
- Jumpwire: Connect different items/pins
- MCP9700 TO-92: Measure temperature (-40°C to 125°C)
- Joy-it CCS811 Sensor: Measure CO2 and VOC levels
| Material | Where to buy |
| ----------------- |:----------------------- |
| LNU IoT bundle 1024kr | [:link:][Electrokit] |
| CSS811 sensor 181,09kr | [:link:][CDON] |
[Electrokit]: https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/
[CDON]: https://b2c.cdon.se/hushallsapparater/joy-it-sen-ccs811v1-sensormodul-p47786925?
[Book-mode]: https://hackmd.io/c/tutorials/%2Fs%2Fhow-to-create-book
[Slide-mode]: https://hackmd.io/c/tutorials/%2Fs%2Fhow-to-create-slide-deck
[Share-Publish]: https://hackmd.io/c/tutorials/%2Fs%2Fhow-to-publish-note
The LoPy4 has been the base for this project as one of the recommended boards for this course it has a lot of capabilities, especially useful for IoT implementations.
### Computer setup
As someone without that much experience with Python I went with Atom as my IDE (since it's the same our teacher uses). Installing the pymakr package was really easy, just go to Packages-> search for pymakr-> Click install-> Done, now you have a pybytes REPL in Atom! There also needs to be a installation of python on your computer. I had previously installed Python 3.8 which can be found here: [:link:][Python]. Another alternative would be to install node.js which can be found here: [:link:][NodeJS]
Pycom provides an easy tool to change firmware of your LoPy4 here: [:link:][Firmware] download the installation file for your OS, install and run! Now you choose between several firmwares and if you want to do an erase of the system before (recommended) under advanced settings.
To run code with Atom and your LoPy4 connected to the computer you have to make sure that your latest/current project is selected and press the :arrow_forward: button. A great alternative is to setup your device on the pybytes website, especially if you want to communicate with the LoPy4 without it being connected to your computer. [:link:][Pybytes]Here you can sign up, set up a network connection over WiFi, LoRa, Sigfox or LTE (if you have a FiPy). It also let's you execute code over an online pybytes REPL.
[Python]: https://www.python.org/downloads/
[NodeJS]:https://nodejs.org/en/
[Firmware]:https://docs.pycom.io/gettingstarted/installation/firmwaretool/
[Pybytes]: https://pybytes.pycom.io/
### Putting the sensor setup together

Both sensors used here like 3.3V so no resistors needed.
This is not the most space effective way to do this setup, there is a possiblity to optimize several connections with male-female cables, really the only ones that doesn't have directly one counterpart is the 3.3V and the ground.
A rough estimate of the power consumption:
LoPy4's 3.3V pin can give out a maximum of 1.2A
| Component | Power draw |
| ----------------- |:----------------------- |
| CCS811* | 30mA |
| MCP9700 TO-92 | 6 μA |
| Total | 30,06mA |
**The CCS811 power draw is from a different brand of CCS811* [:link:](https://www.electrokit.com/produkt/luftkvalitetssensor-ccs811-monterad-pa-kort/)
We're nowhere close to the LoPy4's maximum.
If you want to test something with a regular LED, a resistor is a good idea otherwise they may get burnt. If we use the 3.3V pin and want to use a LED with a regular current of 25mA the calculation goes like this:
$$
{V \over I}=R
$$
$$
{3.3 \over 0.025}=132 Ω
$$
You want to have atleast a 132Ω resistor connected aswell.
### Platform
Pybytes online platform let's me keep everything in the cloud, whilst having quickly readable results and an edit is just a click away.
It's ease of integration from the Atom pybytes is a big plus (it works the same way), it's free of charge and works fast.
### The Code
For the CCS811 sensor I'm using the course provided library: [:link:][CCS811]. For the temperature sensor I searched around a bit and found this great guide on how to get it started: [:link:][TMP]
[CCS811]: https://github.com/iot-lnu/applied-iot-20/tree/master/sensor-examples/ccs811-air-quality
[TMP]:https://www.losant.com/blog/how-to-read-the-tmp36-temperature-sensor-with-pycom-and-sigfox
- Code:
```python=1
import pycom
import machine
from machine import I2C
import time
#...
#CCS811 library
#CCS811 library
#CCS811 library
#CCS811 library
#...
ccs = CCS811(i2c=i2c,addr=90) # Import values from the CCS811 lib
time.sleep(1) # Just to get it some slack starting up
adc = machine.ADC() # Get the raw data from the temperature sensor
apin = adc.channel(pin='P16')
while True:
ccs.data_ready() # Make a reading
time.sleep(30) # Dictates how often readings are done and sent
co2 = ccs.eCO2
voc = ccs.tVOC
millivolts = apin.voltage()
degC = (millivolts - 500.0) / 10.0 # Calculate the temperature
pybytes.send_signal(1, degC) # Send the data to pybytes
pybytes.send_signal(2, co2)
pybytes.send_signal(3, voc)
```
The first part of my main file is the CCS811 library. It's also possible to import the library from a separate file.
### Connectivity
The data is sent every 30 seconds over WiFi.
Pybytes.send_signal is used for transmitting one signal for each type of measurment with the MQTT protocol.
The LoPy4 is plugged in the wall with an old android charger in my bedroom, neither power nor range is a problem so I opted for a bit more precision with more data points sent (every 30 seconds).
WiFi is a more power hungry alternative but the close proximity to my home router ensures a good signal, while keeping the setup stationary (and connected to a wall outlet) the power doesn't really matter and a LoRa/Sigfox signal might be hampered by apartement buildings close by aswell as being in the outskirts of town.
### Presenting data

The pybytes database is easily implemented when using their platform. The data is saved for one month and it's saved as often as it's sent: every 30 seconds.
They offer several different options for the dashboard, for each signal you can create different windows, different kinds of graphs, choose size of pull (how many points of data should be used) and it's all free!
Triggers can be setup, I have made it so I get a notification if no data is received for 30 minutes.
### Finalizing the design

The little sensor array is hidden under my old rocking chair in my bedroom. This isn't really a permanent solution (gathering dust and might be knocked around by something) but it works as a prototype. When I have access to a 3D printer a case might be in order!

*Pybytes offer a function **Limit** that increases readability and also makes averages of the data points available. This will be used but for comparison the exact temperature data points is shown here above.*

*Here is a look at 25 hours of data*

*Here is a look at several days of data.*
The aggressive spikes in the data seem unlikely to represent real world data very well, it's possible energy fluctuations from the grid might cause some of this. The CCS811 is also not the best CO2 sensor out there, for more precise numbers a sensor upgrade might help.
I think this worked out quite well considering when this project started out I had no clue what Fredrick referred to when he said that we were going to choose a platform (my mind were thinking of game platforms and platform games). But I did have some experience of both programming and tinkering with SBCs (single-board computers).
I did start a webhooks page because I wanted to explore more automation and trigger possibilities, for their custom action (based on incoming data) they wanted a fee but I wanted to try this without recurring expenses. Using grafana and docker seemed to be the stylish option but wasn't on the cloud which would require my desktop to act as a server 24/7 (not a practial choice).
I have thought of several options for the dashboard and such. Ubidots might be a good free alternative. The idea of running a raspberry pi as the server for a docker/grafana solution is also interesting. To automate something more than a notification such as a fan or a warning for high CO2 might also be a good addition in the future. The CCS811 is also not the best CO2 sensor out there, for more precise numbers such an upgrade might help aswell.
This project leaves me with a feeling of eagerness to explore the seemingly endless possibilites with IoT!