owned this note
owned this note
Published
Linked with GitHub
# **Home temperature, humidity and air quality device**
###### tags: `Pycom` `Tutorials` `Raspberry Pi` `MQTT` `Node-RED` `Telegraf` `InFlux` `Grafana`
> Student name: Johan Mårtensson
> Student username: jm223cs
> July 2020
This project aims to show how to set up a home system for measuring vital parameters in a house and how to connect to a database through the use of a MQTT-broker and send alarm and display a realtime graph based on pycom and RPi.
The estimated time to implement this project is dependent and previous experience the set up may take 8-36 hours.
## Objective
The home environment is very important due to the amount of hours spent at home by adults and kids, especially now in the pandemia when many have been working from home for the last 4+ months. It may be interesting to know how air quality changes depending on room and time a day. For example, how a long days work or a night's sleep affects the quality. The system does also have the quality of a smoke and hazzardous materials detection system does warn and alarm for other extreme conditions.
The purpose of the system is both to show the quality of indoor air to enlighten the user on when a window may need to be opened or when to take breaks to alleviate or avoid tiredness as a conseqence. The other purpose is for a smoke detection system that sends emails and sounds an alarm in extreme cases.
The measured values that are stored can enable the user to plan to avoid less beneficial home-conditions through viewing charts in Grafana.
## Prerequisites
Before continuing
Get familiar with the Raspberry Pi – https://projects.raspberrypi.org/en/projects/raspberry-pi-getting-started
## Material
#### Hardware specifications
- Raspberry Pi 4b is used as a server takes and sends MQTT data and can also be used to store the database and other programs.
- Raspberry Pi Zero W is smaller formfactor SBC that is used as a camera.
- CCS811 sensor is used to measure CO2 and tVOC particles.
- DHT22/DHT11 sensor is used to measure temperature and humidity.
- Pycom Lopy4 hold the connection and memory to send data through Wifi to store data.
- Pycom Gpy
- Pycom Expansion Board 3.1
- Pycom Pysense
- Wires
- Breadboard
### Where to buy the components
| Part | € | Skr | Purchased |
| ----------------------------------------- | -----:| -------:| --------------------------------------------------------------------------------------------------------- |
| LoPy4 | 34.95 | 373.00 | https://pycom.io/product/lopy4/ |
| GPy Multipack (GPy, PySence) | 73.30 | 783.00 | https://pycom.io/product/gpy-multipack/ |
| Expansion Board 3.0 | 16.00 | 171.00 | https://pycom.io/product/expansion-board-3-0/ |
| Raspberry Pi 4 art nr 88059 | | 1399.00 | https://www.kjell.com/se/produkter/dator/raspberry-pi/okdo-starter-kit-enkortsdator-p88059|
|Raspberry Pi Zero W art nr 41015524||120| https://www.electrokit.com/produkt/raspberry-pi-zero-w-board/|
|RPi Zero official case art nr 41015525||69| https://www.electrokit.com/produkt/raspberry-pi-zero-official-case/|
|Kamerakort för Raspberry Pi Zero art nr 41016467||299| https://www.electrokit.com/produkt/kamerakort-for-raspberry-pi-zero/|
| Luxorparts Start-kit för Arduino | | 219.90 | https://www.kjell.com/se/produkter/el-verktyg/arduino/arduino-kit/luxorparts-start-kit-for-arduino-p87966 |
| Luftkvalitetssensor CCS811 | | 329.00 | https://www.electrokit.com/produkt/luftkvalitetssensor-ccs811-monterad-pa-kort/ |
| Sum | | 3763.00 |
### Software
In the project three different types of SW is used, tools, HW-related SW and product-lib's. Usually the tools and the HW related SW are free but we have to be more careful about the standalone products especially if we intend to build a commercial product. However the investigation is out of scope for this tutorial.
#### Tools
ATOM - the IDE and plugin's are free
pymakr - pycom plugin is free
#### HW related SW
Raspberry Pi OS is free
Pycom libraries under GLP v3
#### Standalone Products
Eclipse Mosquitto - is open source, EPL/EDL licensed, https://mosquitto.org/
Node-RED - a part of the JS Foundation, https://nodered.org/about/
Telegraf & InfluxDB - has a cemmercial payment plan, https://www.influxdata.com/influxdb-pricing/
Grafana - uses Apache 2.0 license, Plugins that are downloaded from Grafana.com are subject to "Terms of Service"
## Computer setup
#### Chosen IDE
I have used the Atom IDE for most of my development purposes but i have also tried VSCode. I ended up using Atom due to its more visual layout of the pymakr expansion.
#### How the code is uploaded to Pycom units
The device is connected to a computer which allows for code to be uploaded via USB-connection to Pymakr in the Atom IDE.
#### The steps needed to take
The programs/libraries needing to be installed for the Raspberry Pi 4 and Zero W are Node.js and an updated version of Node-Red, the latest version of Raspberry Pi OS. For the Pycom units latest version of drivers for the Lopy4 and Gpy, expansion board 3.1 and Pysense source code for the sensors used from the course libraries and a slight manipulation of these to fit the current setup, installation of docker as well as docker-compose, the mosquitto MQTT-broker, password and anonymous settings in the config file, the setup config files for the TIG-stack.
### Flashing Pycom Lopy4 and Gpy

I use these settings for my device during flashing:

I choose to erase old data:

### Flashing the Expansion board and the Pysense
These are similar processes but requires different files. The processes look different depending on what you use to do it. With a PC you will need to download Zadig with a downloaded .dfu file. This guide shows it: https://docs.pycom.io/pytrackpysense/installation/firmware/.
### Up-to-date Raspberry Pi
#### Make sure to update your Raspberry Pi
```bash
sudo apt-get update
sudo apt-get upgrade
```
#### Full upgrade of the firmware on the Raspberry Pi may be needed
```bash
sudo apt-get full-upgrade
```
### Node-Red RPi camera
The Raspberry Pi Zero W can take 4K photograhpies the node allows for you to chose yourself.
Nodemailer format is used to attach a file to an email via Node-Red. Source of format: https://nodemailer.com/message/attachments/
This email node was used for the CO2 alarm on the RPi 4. https://flows.nodered.org/node/node-red-node-email
```bash
npm install node-red-node-email
```
This camera node to snap a picture and send. https://flows.nodered.org/node/node-red-contrib-camerapi
```bash
npm install node-red-contrib-camerapi
```
### MQTT-broker guide
#### Based partly on: https://bitluni.net/simple-mqtt-broker-setup-on-a-raspberry-pi
#### Install Mosquitto MQTT-broker
```bash
sudo apt install mosquitto mosquitto-clients
```
#### Setting passwards on Mosquitto MQTT-broker
```bash
sudo mosquitto_passwd -c passswordfile <NAME>
```
#### Enable Mosquitto MQTT-broker to run in when Raspberry Pi background
```bash
sudo systemctl enable mosquitto
```
#### Checking Mosquitto MQTT-broker status
To see if it is running.
```bash
sudo systemctl status mosquitto
```
#### Edit Mosquitto log in
This is to make it so you are the only one who can logg on to the broker.
```bash
cd /etc/mosquitto
sudo nano mosquitto.conf
# Put the references below in the mosquitto.conf
allow_anonymous false
password_file /etc/mosquitto/passwordfile
```
### Installing Node.js
Newer versions may be needed in order to install nodes in Node_Red or other newer functionality needed in your projects.
```bash
sudo apt-get install -y nodejs
```
### Set up the docker and docker-compose on Raspberry Pi
#### Based on: https://docs.docker.com/engine/install/debian/, https://dev.to/rohansawant/installing-docker-and-docker-compose-on-the-raspberry-pi-in-5-simple-steps-3mgl, https://phoenixnap.com/kb/docker-on-raspberry-pi
#### docker installation
The best thing is to go through the docker documentations to how to install docker on Debian/Raspberry Pi OS.
```bash
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=armhf] https://download.docker.com/linux/debian \
$(lsb_release -cs) \
stable"
```
#### Installing the docker engine
```bash
sudo apt-get install docker-ce docker-ce-cli containerd.io
sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io
```
#### docker start
```bash
sudo service docker start
```
#### docker status
To see the status of docker.
```bash
sudo service docker status
```
#### docker add user to docker premissions
```bash
sudo usermod -aG docker username
```
#### docker-compose
```bash
sudo curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-uname -s-uname -m -o /usr/local/bin/docker-compose
```
#### To start up TIG-stack
Step in the course repository in TIG-stack folder. Repository: https://github.com/iot-lnu/applied-iot-20.
```bash
docker-compose up
```
Do not forget to add "user: "0:0"" to the .yml file otherwise grafana will not start on docker on the Raspberry Pi.

### Node-Red tutorial
#### Based on: https://nodered.org/docs/getting-started/raspberrypi
#### Version and installing Node-Red on debian (Raspberry Pi)
```bash
node -v
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
```
#### Run at start up
```bash
sudo systemctl enable nodered.service
sudo systemctl status nodered.service
```
#### Expansion nodes (in this case email notification):
Install and then restart Node-Red.
```bash
npm install node-red-node-email
node-red-restart
```
#### If you want to disable Node-Red:
If you want to turn off Nod-Red.
```bash
sudo systemctl disable nodered.service
```
## Putting everything together
### Sensors
The sensors as well as the buzzer are soldered to a board. This is so it would fit better in the modeled casing.

#### Circuit diagram

#### Connection guides
-- CCS811 https://learn.sparkfun.com/tutorials/ccs811-air-quality-breakout-hookup-guide/all
-- DHT11 https://learn.adafruit.com/dht/connecting-to-a-dhtxx-sensor
#### Power consumption
A quick calculation of the power consumption for the sensor unit shows that to be wireless, it needs a large battery or it must be in deep sleep a large proportion of the time, maybe both.
| Unit | | Active mA | Alarm mA | Deep Sleep uA | | | | |
| ------------------ | ----------- | --------- | -------- | ------------- | --- | ---------------------------------------------------------------------------------- | --- | ---------------------------------------------------------------------------------- |
| | | avg. | | | | | | |
| LoPy4 | | | | 50 | | | | |
| | WiFi Client | 107 | | | | https://docs.pycom.io/gitbook/assets/specsheets/Pycom_002_Specsheets_LoPy4_v2.pdf | | https://docs.pycom.io/gitbook/assets/specsheets/Pycom_002_Specsheets_LoPy4_v2.pdf |
| | LED | | | | | | | |
| Expansion Board v3 | | | | | | | | |
| CCS811 | | 30 | | | | https://www.electrokit.com/uploads/productfile/41015/CCS811_Datasheet-DS000459.pdf | | https://www.electrokit.com/uploads/productfile/41015/CCS811_Datasheet-DS000459.pdf |
| DHT11 | | 1 | | | max | https://www.electrokit.com/uploads/productfile/41015/DHT11.pdf | max | https://www.electrokit.com/uploads/productfile/41015/DHT11.pdf |
| Alarm | | | | | | | | |
| | LED (red) | | 4 | | | https://www.hobby-hour.com/electronics/ledcalc.php | | https://www.hobby-hour.com/electronics/ledcalc.php |
| | Buzzer | | 10 | | | https://arduino.stackexchange.com/questions/66771/current-consumption-of-a-buzzer | | https://arduino.stackexchange.com/questions/66771/current-consumption-of-a-buzzer |
| Sum | | 138 | 14 | 50 | | | | |
The power consumption for the LED is a bit difficult as the voltage drop for the blue LED I use is said to be 3.6 volts and I have a 3.3 volt supply, so there would be no current and the LED would not light. I have put a 330 Ohms resistor in series and the LED lights up with a pleasant blue light.
If a standard 800 mAh LiPo battery and deep sleep for the most part and operates only a few seconds every hour, the device must be charged every other time per year. But rechargeable batteries will drain more than regular battery so this needs to be proven. With a really large battery, we can expect much longer operating time.
| Conclusion | Active h | DeepSleep h | year |
|--------------------------|----------|-------------|------|
| LiPo battery I, 800 mAh | 6 | 16000 | 1,8 |
| LiPo battery II 5800 mAh | 42 | 116000 | 13 |
### Casing
Sketchup can be used to design the casing for the iot unit. I had a look at tinkercad but it did not satisfy my needs. Since I have some experience with Sketchup since before I thought of relearning and implementing my ideas there.
Making and designing the case I had to think about wall thickness where all the technology had to be located. I also had to think about how the 3D-printer works to get the plastic not to get printed in the air.
It is of paramount importance when using Sketchup for the purpose of drawing a 3D model that the object you wish to print has the status solid when seen in the group viewer. I learnt this the hard way.

It is also good to make sure the model is of the right dimensions because otherwise there is a risk the printer cannot print the models due to it being too large to fit.

The shell shell was made with a Cura 3D printer ans is called "Creality 3D CR-10S Pro V2". The plastic used is PLA which is less toxic than ABS and needs less heat.
## Platform
The system aims at collecting temperature and humidity as well as CO2 and tVOC data to send to my own MQTT broker run on a Raspberry Pi where I can add subscribers who then store in a timeseries database which I then can display in a service like Grafana or send to other programs.

#### Choice of platform
I chose to implement things using the mosquitto MQTT-broker, the TIG-stack with its setup of configuration files gave a clear way of viewing settings, I chose to use Node-Red due to it having lots of functionality and ease of use. I used a Raspberry Pi Zero W for this purpose. I used Raspberry Pi OS, a variant of the Debian distribution of Linux. Raspberry Pi OS may need to be up to date for most things to work and if not it will need to be updated. I mainly chose to do these things on my own machines to learn how to build and run everything on my own network and to try to handle as many aspects of development as possible. I also wanted to add to my knowledge of smaller devices like SBCs and it is a good opportunity get some hands-on with these technologies.
It will be easy to scale this system to use a cloud storage in Amazon IoT platform (MQTT-based), MS Azure or Google Cloud Platform however this may be free for limited version and time but have normally a payment plan. The advantage is automatic updates and patches for security and backup of data.

## The code
I used course repository which goes under GPL-3.0 License and so does Pycoms code.
I used modules of the course repository and spliced it with code from Pycom to create the functionality for the Pysense device.
The code used for the Expansionboard is mostly modified course repository code that has been added too. The main part are the callback functions that activate the alarms/buzzing.
Multiple threads are running on the Expansion board with Lopy4. One is used to send data and one is used to receive and run the callbacks but there is one for that is created when the alarm aswell too to be able to cycle it while the device continues its normal cycle. The alarm will blare until the level return to normal and still continue as usual.
My own modified version:
- https://github.com/ChattyCat/Home-monitoring-system?fbclid=IwAR3dahXNfpUYq3eAQJiq7gdQi3RDDGFb7JG7sb0dO7msT4USjREYPy33nE4
The course repository are available here:
- https://github.com/iot-lnu/applied-iot-20
Pycom´s repository:
- https://github.com/pycom/pycom-libraries
Node-Red is used to automate email alerts with pictures as well as add a buzzer that blares noise and when CO2 level reach higher levels higher levels.
(Picture)
Node-Red on the Raspberry Pi 4 has serveral nodes and paths. It collects the data from different topics and then parses them then performs a boolean calculation on them and sends a message either composed as an email alert or as a callback that trigger an alarm.
(Picture)
Node-Red on the Raspberry Pi Zero W has a fingular path that collect and parses as the picture above but when a certain value gets reached it takes a picture. The function named fuction then formats the email so that the picture can be sent as an attachment.
## Transmitting the data / connectivity
#### The frequency of published data
The device sends data about every 10th second during testing but can be adjusted for whatever purpose and a usb-cable to the wall is the power source.
#### The wireless protocols used
I am using Wifi due to my units being in my home and thus it is very stable.
#### The data transport protocols used
For transportation of data MQTT has a broker which clients are publishing and subscribing to.
#### On the design choices regarding data transmission and wireless protocols.
Since Lora does not work locally and I have not got hold of a pygate yet, the solution is based on WiFi in the home.
The gpy mounted on pysense is put in sleep mode regularly to test the possibility of making it easier to move.
## Presenting the data
My data is easily accesible and able to view at a glance in my Grafana browser.

#### How often is data saved in the database?
The InfluxDB database is updated every 15th second with data going through telegraf and its configurations pointing to the MQTT-broker which gets updated every 10 second but can be changed for more long term use to save energy.
#### The choice of database.
I chose InfluxDB timeseries functionality is good at handling the data in a timewise manner which is effect for the purposes of analysis and setting up in graphs and diagrams. It has also different ways of saving data long term which are useful if memory would be an issue. The database is also part of the TIG-stack´s docker-compose.yml which makes it easy to handle along with the other programs through docker. Another alternative would be MYSQL but then time stamps would not be added automatically.
#### What the data triggers.
The data triggers different nodes in the Node-Red which sends emails with notifications and even attached photos which are triggered by certain CO2 values.
A buzzer is used to signal to anyone present nearby that the CO2 levels are too high. The buzzer continues buzzing its very audiable alarm until values get down to normal levels. The buzzer shifts in frequency to make a more noticeable sound, this is achieved with a separate thread with runs the while-loop until the boolean turns False and then after the cycle finished it exits the loop and the thread. You can use any frequency an sound but I use this for testing and it is great: http://wiki.micropython.org/Play-Tone. A proper alarm can be tiring when testing.

This photo is of me triggering the Raspberry Pi Zero W to take a picture by blowing on the sensor which hightens the CO2 values and makes the camera snap a photo and then send it to my mailbox. This way one can see if its burning or not.
The Pysense can also trigger alarms on the Expansion board via the callback function. This gets triggered by very high temperatures. In this case 40 degree Celsius.
#### The email notifications

##### **PS: Displayed email account is used for this purpose only!**
## Final results
I really fell in love with this kind of hardware coding and systems development. It has opened my eyes to a new side of technology and how to get to understand more of all the aspects of server management, functionality and hardware thinking.
Something to remember is that you can save money by buying stuff in the right place or just what is needed for your purpose, though this is hard to know in as a beginner.
What I would like to develop further is the wireless, since Lora did not work for me and I had no real purpose for it at this stage I chose to continue with Wifi. I would in the future if possible want to develop this project further to have some longer range capabilities and to create a more weather proof casing. I could for example add more cameras to the existing network and use ESP32-CAM models instead for lower costs. There could also be a more interactive web client that allows for a more use friendly and nice interface for other viewer. I could also configure the Raspberry Pi Zero W to take video and stream it to this webpage.


## Sources/resources
Tillämpad IoT Introduktion - 1DT305:
https://www.youtube.com/channel/UCj70OvBUiWxO8Rj_qfUcHsQ/videos
Flashing the expansionboards and Pysense/Pytrack:
https://docs.pycom.io/pytrackpysense/installation/firmware/
Pycomlibraries
https://github.com/pycom/pycom-libraries
Node-Red installation guide: https://nodered.org/docs/getting-started/raspberrypi
Expansion node: node-red-node-email:
https://flows.nodered.org/node/node-red-node-email