# Voice and temperature contolled AC using Node-RED, MQTT and Amazon Alexa
#### This project is written by Dnyaz Ali Ahmet (da222ze)
---
[TOC]
---
### Project description
This article is written and presented as part of the final project assignment for the Applied Internet of Things course (1DT305) at Linnaeus University.
The project covers the aspect of home automation based on both sensors and actuators, together with self-hosted containerized services such as Node-RED and a MQTT broker. As a bonus to it all, an Amazon Alexa is also added to the project to add voice controlled capabilites.
---
### Estimations
Before starting the project I had estimated about 24-32hours -- i.e. 3-4 working days (~2 weekends). The calculations are based on scrum estimations (S, M, L, XL...) and visualized with a GANT chart.
I spent in total about 3,5 working days, here's my total calculation:
| Task | Day 1 | Day 2 | Day 3 | Day 4 | <b>Total</b> |
| ------------- | -------- | ---------- | --------- | -------- | ------------ |
| Analysis | 1,5 | 0,5 | 0 | 0 | <b>2</b> |
| Testplan | 1 | 0 | 1 | 0 | <b>2</b> |
| Development | 1,5 | 3 | 6,5 | 3 | <b>14</b> |
| Verification | 0 | 0,5 | 3 | 3 | <b>6,5</b> |
| Documentation | 0 | 0 | 1 | 4 | <b>5</b> |
| <b>Total</b> | <b>4</b> | <b>4</b> |<b>11,5</b>|<b>10</b> | <b>29,5</b> |
---
### Objective
The objective of my project is a typical IoT scenario where I am not only to gathering sensor data but also automating events based on given criteras of the collected sensor data.
I am a lazy guy and I love automation -- specially home automation. All of my lamps are automated with voice control and virtual switches, and I have a couple of sensors around the appartment. With that said, the more I can enjoy automation the better. Therefore, for this course, I came up with a project idea which is quite benifical for me during these hot summer days and nights.
These last couple of weeks have been really hot here in Sweden <i>(it's summer, duh?)</i>, and during previous summers I would usually turn on a fan or an AC before I went to sleep. However, if the weather became cooler during the night, I would often wake up and feel stiff around the neck because I had been freezing. Thus, the objective of the project is to automate an AC or a fan to turn on/off based on outdoor climate data during my sleep.
In addition to having cooled sleepy nights I wanted to able to monitor and visualize realtime local climate data instead of relying on weather forcasts, which are usually off by a couple of degrees. And of course, as with the rest of my nodes at home, it needs to be voice controlled! 🤓
---
### List of material (BoM)
The project is based on the following:
| Device | Quantity | Description | Price / Link |
| ------------------------------------------------------------------- | -------- | ---------------------------------------------------------------------------------------------- |:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
| ESP32<br><br> | 2 | Sensor data collection and actuator control | 200kr<br>[Amazon](https://www.amazon.se/gp/product/B07VJ34N2Q/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&th=1) |
| Raspberry Pi 3b+<br> | 1 | Self-hosted containerized services; Node-RED and MQTT Broker (already at home) | 429kr<br>[Electrokit](https://www.electrokit.com/produkt/raspberry-pi-3-1gb-model-b/) |
| BME280<br> | 1 | Bosch sensor measuring relative humidity, barometric pressure and ambient temperature | 75kr<br>[Amazon](https://www.amazon.se/gp/product/B088R89TSX/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1) |
| 5V Relay Module<br> | 1 | Digital control circuit to control a high-current using a low-current signal (already at home) | 60kr<br>[Amazon](https://www.amazon.se/AZDelivery-KF-301-modul-l%C3%A5gniv%C3%A5utl%C3%B6sare-kompatibel-Raspberry/dp/B07V1YQQGL/ref=sr_1_7?crid=3RE8QVX51797T&keywords=5v+relay+module&qid=1656870757&sprefix=5v+relay+module%2Caps%2C95&sr=8-7) |
| Junction box<br> | 1 | Housing for ESP32 and BME280 to secure the electric circuit | 40kr<br>[Kjell](https://www.kjell.com/se/produkter/el-verktyg/elektronik/elektroniklador/ventilerad-inbyggnadslada-38x65-mm-p89041) |
| Junction box<br> | 1 | Housing for ESP32 and relay to secure the electric circuit | 70kr<br>[Kjell](https://www.kjell.com/se/produkter/el-verktyg/elinstallation/koppling-dosor-lister/schneider-electric-ytmonterad-kopplingsdosa-ip65-utan-plint-p67736) |
| Standard installation cable<br> | 1x (~10m) | Installation cable to control the mains circuit (already at home) | 55kr<br>[Biltema](https://www.biltema.se/bygg/elinstallationer/installationskablar/kabel-rund-2000017921?gclid=CjwKCAjw_ISWBhBkEiwAdqxb9gMyPal4_9VkCC8R-5dRyuhq-sS40-D8kE21CdSXwSyc349d1d6ycRoCT9YQAvD_BwE) |
| Amazon Alexa <br> | 1x | Optional; needed for voice capabilites (already at home) | 649kr<br>[Amazon](https://www.amazon.se/echo-dot-4-e-generationen-internationell-version/dp/B085K45C3S) |
---
### Computer setup
To setup a workflow/environment, you'll first be required to install a couple of applications and tools. Below are a set of described steps on how-to install the applications, dependencies and also how-to program the devices.
#### esptool
I've been using esptools to flash the devices with MicroPython. Downloading and installing might be a bit advanced for some users. I have taken the time to describe how-to do it as detailed as possible.
<b>NOTE:</b> Python 3.4 or newer is a dependency for esptool! If you haven't got Python installed on your computer, please do so first! I will not cover it in this article because there are already plenty of good guides out there (for example [this one](https://realpython.com/installing-python/) by Real Python).
<br>
1) When you've installed Python, open up a terminal of (cmd, Powershell or bash) and install esptool using pip:
```
pip install esptool
```
2) Once the installation is finished you can test to see if the command works properly by executing:
```
python -m esptool
```
It should give an output similar to the one in the image below:

3) Now that ESPtool is installed, we'll need the latest binary of MicroPython from their official page.
Here's a [direct link](https://micropython.org/download/), once you're on the page click on your board of choice -- mine is [ESP32](https://micropython.org/download/esp32/). Once you've found your board, scroll down to the 'Firmware' section and download the latest firmware as seem from the image below.

4) Now that you've got the binary downloaded, store it in a preferable place and go to back to your terminal.
In your terminal, navigate to where the MicroPython binary file is stored and flash your device using the following commands.
If it's the first time installing the MicroPython firmware on the board then you should first erase the entire flash with the command below.
<b>NOTE:</b> Replace "COMx" with your COM-port.
```
python -m esptool --chip esp32 --port COMx erase_flash
```
Once the flash is erased, install the MicroPython firmware with the following command:
<b>NOTE:</b> Replace "COMx" with your COM-port and esp32-firmware.bin with your binary file.
```
esptool.py --chip esp32 --port COMx --baud 460800 write_flash -z 0x1000 esp32-firmware.bin
```
Congratulations, you've now installed MicroPython on your device! 🥳🎉
#### Node.js, Atom and PyMakr
1) Installing Nodejs
In order to use the Atom IDE and PyMakr, we'll need to install nodejs as a dependency. Here's a [direct link to Nodejs](https://nodejs.org/en/).
Download the latest version from their page and follow the installation steps.
2) Installing Atom
The choice of IDE used is in this project is [Atom](https://atom.io/).
Download the latest version from their page and follow the installation steps.
3) Installing PyMakr as a plugin to Atom
Next we'll need to install PyMakr, which is a plugin used to be able to connect and upload the projects to our board of choice.
You can download [PyMakr](https://atom.io/packages/pymakr) from the link and it'll open up Atom automatically in order to install the plugin.
Download the latest version from their page and follow the installation steps. <b>Once done, restart Atom!</b>
4) Create a project directory
On a preferable place on your computer, create an empty directory where you'll be keeping your project and the code.
6) Select your COM port and start hacking!
Once Atom is started again, click on Setting on the right hand side. The snippet GIF below shows how!

7) Upload your code
When you're ready to upload your code, select your project as well as your COM port in the settings. The GIF below shows an example of how!

---
### Putting everything together
This section covers how each circuit is connected and wired.
#### [Device 1] -- IoT Weather Station
The first node acts as a IoT Weather Station that will be stationed on my balcony and secured in a junction box. The IoT Weather Station consits of an ESP32 which will be collecting climate data with the help of an Bosch BME280 sensor via the I2C serial communication protocol.
The wiring connections are descibed in the diagram below.

#### [Device 2] -- Remote controlled AC
The second node acts as a remote power-control as part of the rest of the home automation.The device consits of an ESP32 which will be constantly tuned (subscribed) to the data sent from the IoT Weather station, and based on the data and given criteria, it will act upon the messsage to either turn on or off the fan by controlling the 5V relay module. All the components of this device is also housed in a junction box. It's mobile, meaning it can be moved between different rooms. For now, it will be in my bedroom and control my fan to breeze me during the hot evenings/nights.
The wiring connections are descibed in the diagram below.

#### [Device 3] -- Raspberry Pi and the self-hosted containers
Hardware wise, this device is not as interessting as the previous ones because the magic happens on the software side where are the containers and services are hosted -- Node-RED and the MQTT broker. The coming platform chapter will cover this in more detail.
#### [Device 4] -- Amazon's Alexa
Alexa is Amazon's cloud-based voice assistant with services available for other Amazon devices but also third-party device manufacturers. With Alexa, you can design various voice experiences in a intuitive way to interact with the technology. Amazon is kind enough to provide us with a collection of tools, APIs, reference solutions, and documentation to make it easier to build for Alexa.
#### Electrical calculations
To understand how much each device is consuming, I have made some crude calculations as well as used my multi-meter to verify my calculations. You'll find the result below.
* <b>[Device 1] -- IoT Weather Station</b>
The IoT Weather Station consists of two main modules; the ESP32 and the BME280.
According to the datasheet a ESP32 will consume ~100-240mA depending on if it's RX/TX mode and a consumption of ~50-150 μA in deep sleep mode. The BME280 has an average consumption of 0.1 μA in sleep mode (i.e. while not measuring) -- which is REALLY an ideal sensor for IoT!
<br>With the figures from the datasheet, we could make hypothetical worst case scenario (WCS) measurements of maximum 240mA + 0.1μA = 240.0001mA while transmitting data and 150μA when in deep sleep mode. To confirm this I've measured using my multimeter and the result was within the range. The power consumption while measuring and transmitting data were about ~225mA and ~100 μA in deep sleep mode.
<br>Reference and datasheets:
[ESP32 Datasheet](https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf)
[BME280](https://www.bosch-sensortec.com/products/environmental-sensors/humidity-sensors-bme280/)
* <b>[Device 2] -- Remote controlled AC</b>
The Remote controlled AC consists of two main modules; the ESP32 and the 5V Relay module.
As previously mentioned, according to the datasheet a ESP32 will consume ~100-240mA depending on if it's RX/TX mode and a 5V relay module consumes ~75mA.
<br>With the figures from the datasheet, we could make hypothetical worst case scenario (WCS) measurements of maximum (240mA + 75mA = 315mA). To confirm this I've measured using my multimeter and the result was within the range in this case too. The power consumption in total were about ~335mA.
<br>Reference and datasheets:
[ESP32 Datasheet](https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf)
[5V Relay Module](https://www.hobbytronics.co.uk/5v-relay-module)
* <b>[Device 3] -- Raspberry Pi and the self-hosted containers</b>
There's no real usecase to measure the electrical consumption of the Raspberry Pi as it is on 24/7 and connected to a wall outlet. However, I made some investigations and found a fellow tinkerer who had been kind enough to measure the consumed amps in several scenarios. See below.

<i>Reference: [How Much Power Does Raspberry Pi 3B+ Use? Power Measurements](https://raspi.tv/2018/how-much-power-does-raspberry-pi-3b-use-power-measurements)</i>
With these measurements I looked into the current CPU load and found it to be about 2-6% over 15-30mins, i.e. it is mostly idle. Therefore we can outline that the Raspberry Pi is consuming about 400mA in idle mode.
Remote-monitoring information snapshot taken at 2022-07-03 19:44:
```
- UpTime:
Uptime: 523 days
- CPU:
Current CPU load: 3%
- RAM:
Total RAM: 926 MB
Used RAM: 234 MB
Cached RAM: 346 MB
Buffers: 60 MB
- Processes:
Number of processes (active/total): 4/203
```
---
### Platforms
Unfortunately, where I live there are way to few LoRa gateways, the heatmap on The Things Network (TTN) shows only 1 Gateway which is on the other side of town... Therefore I ordered a [Seeed The-Things-Indoor-Gateway](https://wiki.seeedstudio.com/The-Things-Indoor-Gateway/) and a couple of RFM95-chips last week to not only leverage from transmitting various sensor data over long range transmission but to also contribute with access to this awesome technology by putting up the 2nd LoRa Gateway in my town! 🤓
So for now, I will have to rely on WiFi and MQTT where the platform of choice is a self-hosted Node-RED and a self-hosted Mosquitto MQTT broker -- both of which as containerized services on my Raspberry Pi!<br>You can find my docker compose setup [here](https://github.com/dnyazali/applied_iot_1DT305/blob/main/docker/docker-compose.yaml)!
#### Node-RED
Node-RED is a powerful platform and programming tool which gives you capability for wiring together hardware devices through APIs and various other services.
I started using Node-RED a couple of years ago and the reason was very simple; there's a huge community of tinkerers using Node-RED, thus there are many plugins and helpful users!
Node-RED are most usually self-hosted but there are services out there which gives you possibility to use Node-RED without having to self-host it. For example, check out FRED. With [FRED](https://fred.sensetecnic.com/), you get a 24/7 Node-RED instance with up to 50 nodes (that's generous)!
As a cherry on top of the cake, I went ahead and added voice controlled capabilites. With the leverage of having a great API for the Alexa and a awesome comuinity around Node-RED, some nice people have developed [node-red-contrib-alexa-local](https://flows.nodered.org/node/node-red-contrib-alexa-local). The "contrib-alexa" node is simply a node which can be used to create "virtual devices" that the Alexa can exchange data with.
The gif below highlights how the Alexa interacts with a device on a high level.

1) The user initiates a command to the Alexa device.
2) Alexa transmits the data to the "virtual" Amazon device that are declared in Node-RED.
3) In Node-RED, the data/output from the "virtual" device gets recieved
4) The data gets processed and parsed/converted it into a MQTT payload.
5) The MQTT payload gets transmitted and the node (ESP32) acts upon the message.
Here's a picture of my nodes, the highlighted part are the nodes added for this project and the rest are used for other devices around my appartment.

Here's a zoomed-in version of my flow and a [direct link to the flow](https://github.com/dnyazali/applied_iot_1DT305/tree/main/node-red) if you want it!

#### Mosquitto MQTT
Alongside Node-RED is an instance of Mosquitto MQTT. But first, what is MQTT?
MQTT is an lighweight messaging protocol using a publish/subscribe model. The protocol was first developed the late 90s (source [Wikipedia](https://en.wikipedia.org/wiki/MQTT)) and is today is an OASIS standard messaging protocol for Internet of Things (IoT). MQTT is a great option for Internet of Things devices due to the lightweight & low power consumption.
Now back to Mosquitto MQTT! Mosquitto MQTT is an open source message broker with support for the MQTT protocol (v5.0, v3.1.1/3.1). Mosquitto is lightweight and suitable for low-end/low-power edge platforms, which makes it ideal to run on single board computers like the Raspberry Pi!
Choosing MQTT as a messaging protocol is a no-brainer; it's OASIS standard messaging protocol for Internet of Things (IoT) and Mosquitto is one of the most famous open source MQTT messaging broker platforms out there (alongside EMQX and HiveMQ). Although, having used HiveMQ before I want to rely on my own infrastructure. In addition to that, even if I lose internet connection, I can still rely on the WiFi network connectivity because no message goes out to the internet.
#### Docker
In order to execute and run the platforms and services smoothly, with easy access, deploy and management, I've decided to containerize Node-RED and Mosquitto MQTT!
Docker is an open source platform for developing, executing applications and services. Docker separates the applications and service from your infrastructure and Host OS. As a result, building, deploying and managing applications becomes streamlined, effective and "easy to move" to other servers and computers.
---
### The code
For simplicity, I decided to push the code to my github.
The code is stored at [https://github.com/dnyazali/applied_iot_1DT305](https://github.com/dnyazali/applied_iot_1DT305)
---
### Transmitting the data and connectivity
This section covers what protocols and methods that are used for connectivity and transmission.
#### Wireless protocols
The most ideal (and fun!) choice for this course would be LoRa! But unfortunately, and as previously mentioned, there was not enough TTN Gateways in my area (that's about to change! 😉). Instead, I went with the next ideal protocol; WiFi!
The router I am using has support for both 2.4GHz as well as 5GHz. Although one would natrually pick 5Ghz, it might not be the best choice for IoT.
With the 5GHz frequency, you'll gain a higher data rate while less prone to interference -- however, the downside is that you'll get a smaller raduis and coverage area, and worse penetration of solid objects (walls, doors, furnitures, etc.)
With the 2.4GHz frequency, you get the benifit of a larger coverage area with a better penetration of solid objects (walls, doors, furnitures, etc.). You'll not get as high data rates as 5GHz but that's not really what we're aiming for with IoT devices that are going to be sending a couple of bytes a few times per hour, and therefore not really a problem.
In addition to the above, WiFi is a secure (given that you're on a private network), stable and reliable choice of wireless transmission due to the various authentication and encryption methods. Today, WPA/WPA2-AES is the recommended choice of use (before WPA3 becomes widely popular).
#### Transmittion protocol
As already presented in the previous section, MQTT is naturally the choice of transmission. MQTT is a great option for Internet of Things devices due to the lightweight & low power consumption, apart from actually being a OASIS standard messaging protocol for Internet of Things (IoT).
Having an self-hosted MQTT broker is a great way to provide transmission, without having to access internet. One can rely on the network connectivity as long as there's electicity since no message goes out to the internet before it has to be processed by another device.
I've been using MQTT for a few years now and can confidently say that it is a reliable transmission protocol which has never failed me!
#### How often is data transmitting?
Majority of my IoT devices are passive, i.e. they subscribe and act upon a message -- however, the IoT Weather Station is active in the sense that it collects data and sends it over a given topic to be visualized and processed by other IoT nodes.
The IoT weather station collects datapoints for 48 times a day, which approximates to every half an hour. When the data is sent, then it goes back to deep sleep for 30 mins before it repeats the process.
The Remote switch which controlls the AC or fan is never transmitting data but rather waiting for instructions based on if it should turn on the mains power or not.
---
### Presenting the data
The data is saved when received over the MQTT protocol and the datapoints are presented via a Node-RED Dashboard. Here's a image of the data visualization.

I went with the Node-RED Dashboard because it has built-in dashboard features, it's very easy to use and you'll only have to deploy a one-stop service/platform. On top of that, you can easily automate triggers and events based on the collected data. In my system, the remote AC switch turns on/off based on the collected data.
---
### Finalizing the design
The final results of my project are two systems that are communicating over WiFi; one of which is acting as a IoT Weather Station and the other as a Remote Controled Switch for a fan, which can also be controled with voice capabilites. Each of these device/nodes either publishes or subscribes to a topic and transmitts data via a MQTT broker based on Mosquitto. Once the message reaches the broker, payload of the message is processed and visualized with the help of Node-RED. Both the Node-RED and the Mosquitto instances are containerized with the help of Docker.
I am happy with the project and it's results! It has been a great learning experience even though I have been into Home automation for quite some time now! I have learnt a lot from exchanging knowledge with the other students as well as the teachers and the TA's!
#### Reflections
<b>My first reflection</b>
I've already mentioned and discussed with several other students and TA's that I wanted to go with LoRa as the wireless communication method/protocol. But unfortunately, there are ONLY one TTN LoRa gateway in the city where I live... I could of course used Helium instead (there's 9 nodes in my area) but I am a FOSS and DIY enthusiast -- I believe in giving back to the community! Therefore I went ahead and ordered myself a [The-Things-Indoor-Gateway](https://wiki.seeedstudio.com/The-Things-Indoor-Gateway/) from Seeed along with several RFM95-chips and a LoRa development board. I even managed a colleague to go ahead and buy a LoRa gateway to provide the lovely city of Linköping with better connectivity to TTN.
Here's a heatmap from [ttnmapper.org](https://ttnmapper.org/heatmap/).

<b>My second reflection</b>
As you probably can tell by now, energy consumption was not a critical part of my project. However, it would surely be more beneficial if the IoT Weather station would use LoRa as a communication method instead due to the possibility to modulate data rate and by that converse energy consumption.
I didn't have enough time to buy a LoRa chip or a good LiPo battery to hook it up with the IoT Weather station but I will definately do it after the end of this course!
<b>My thrid reflection</b>
I am new to MicroPython (but a long-time Python user) and while it was easy to hack and code something fast... I might have done myself a favour by sticking to C++. Not only because I am more used to C/C++, but also to be aligned with the rest of my devices at home.
Please, dont get me wrong -- MicroPython is a great choice for people that are generally new to hardware and coding but you there's alot bigger community with C/C++ tinkerers. Hence, a lot more support and better maintained librarys. One might also argue that the Python interperter is great to have onboard, but I disagree since it takes up majority of the on-board memory.
Another important aspect is the size, speed and portability of the project. MicroPython is great for rapid prototyping, but you should without a doubt go for C/C++ for production.
This [short video](https://youtu.be/u9UfKTOcYNs) demonstrates the difference of MicroPython vs. C++.
Spoiler alert -- C++ are about ~400x faster than MicroPython.
#### The final product
<center><i>Housing the the Weather station for the first time!</i></center>
<br>
<center><i>The Weather station hanging out on my balcony, sensing and collecting data!</i></center>
<br>
<center><i>Housing the the Remote switch for the first time!</i></center>
<br>
<center><i>The Remote switch by the end of my bed, ready to breeze with cool air</i></center>
<br>
<center><i>The man in the middle (no pun intended...)! My Raspberry Pi, alongside my Orange Pi</i> 😊😍</center>
<br>
<iframe width="730" height="440" src="https://www.youtube.com/embed/YCorplnSVTA" title="Testing out the Remote AC Switch (1DT305 - Applied Internet of Things)" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<center><i>A short video of me testing out the Remote AC Switch</i> 🤓🔌🔌</center>