We think everyone of us knows this awkward situation when someone is entering the room and says: "Uff... it is a bit smelly in here! I think you could open the window from time to time". But the problem is that you don't really realize that the air quality drastically got worse in the last one or two hours - at least most of the times.
In order to avoid these situations we thought: "Let's add the "sense of smelling" to your smart home with our PAS CO2 Sensor!" and give you a graphical representation of the air quality inside the Home Assistant GUI.
And that is what we did! We took Infineon's new XENSIV™ connected sensor kit, made it talk to our Smart Home via a Home Assistant instance on a Raspberry Pi and created a neat interface to show the received sensor data. Don't worry the details will be explained further below - so let's get to it.
In order to get started we first have to install Infineon's ModusToolbox. It is required to change the code on the connected sensor kit. If you're interested to learn more about the ModusToolbox you can check out this link. You will find a bunch of information there and also an user manual and a getting started guide :)In this post we will refer to the ModusToolbox as MTB.
The second thing which has to be prepared is the Raspberry Pi. You need a running Home Assistant instance on it, in order to configure the GUI for the window. Furthermore, you also need a running MQTT-Broker.
Setup your Connected Sensor KitSo far so good! Since we prepared everything in the step above, we are now ready to directly jump into action. That means opening the MTB and start coding :) Don't worry, we will guide you through the necessary steps, because the ModusToolbox can be a bit intimidating in the beginning.
So the first thing you should see after the MTB started is this prompt here:
In this window you can define your MTB workspace. The standard workspace should be the one shown above. But in your case there should be your username instead of USER. You should also have a tick-box in order to set this path as the default workspace, but in our case we checked that box already. After you chose your desired workspace press Launch and the MTB IDE comes up.
So now you should see the following:
In order to get the right example we have to click on NewApplication, inside the upper, left box. See here:
Now you should see the Project Creator of the MTB IDE (Picture 5). Please enter "CYSBSYSKIT-DEV-01" into the search bar and select the shown board. After that you just have to click on Next and you should come to the application selection screen (see picture 6).
In the application section you have to search for MQTT and choose the example PAS CO2 Sensor MQTT Client, which can be found under Sensing, with the tick-box (see also picture 6). Click on Create and the IDE should start to import all required files for the example. Now would be a good time to get a coffee, this takes a moment ;)
After everything was downloaded you should see two folders in your Project Explorer, the mtb_shared folder and the PAS_CO2_Sensor_MQTT_Client folder. The mtb_shared contains different resources which can also be used for other examples, like common libraries and their different versions and a lot of other things. The other folder contains our application example. In order to see what it contains you have to double-click the folders name. Now you should see all its content:
Another thing you might recognize is the README.md file which is automatically opened if you load one of the examples of the MTB. This README contains a detailed explanation of the example and a lot more information that can help you to setup the chosen example. In our case you don't have to go through the whole file, since we will explain the necessary steps to get the board running :) But if you're interested feel free to go through the README.
Enter your WiFi credentialsEnough preperation - let's code! So the first thing we have to do, is to configure te boards WiFi settings. In case of the Smart Window Opener we're connecting to the local access-point of the Raspberry Pi, but you can connect to any WiFi in which your MQTT-Broker is running.
In order to do that open the wifi_config.h file in the folder configs:
In this file we just have to change the following lines (37 and 40):
#define WIFI_SSID "MY_WIFI_SSID"
and
#define WIFI_PASSWORD "MY_WIFI_PASSWORD"
Just delete the MY_WIFI_SSID and exchange with your WiFi-SSID. And do the same for MY_WIFI_PASSWORD with your WiFi's password. But please keep the quotation marks otherwise it will not be recognized as string anymore. So for example:
#define WIFI_SSID "MyAwesomeWiFi"
and
#define WIFI_PASSWORD "MyAwesomePassword"
If you have another security standard than the common WPA2 you have to change line 45 as well
#define WIFI_SECURITY CY_WCM_SECURITY_WPA2_AES_PSK
with one of the following options:
There a few more things to configure, but those are not mandatory to get the example running. But if you're interested feel free to explore the rest of the file :) That is it for the WiFi configuration!
Configure the local MQTT clientThe next thing that is required is the configuration of the MQTT-Client. For this we first have to open the file mqtt_client_config.h, which is in the same folder (configs). Then we have to define the address of the MQTT-Broker (line 36). For example:
#define MQTT_BROKER_ADDRESS "10.3.141.1"
#define MQTT_PORT 8883
Please be careful with the quotation marks. The standard port of the MQTT-Broker is usually 1883, but if you have chosen another port you have to change that as well.
If you have defined a user and a password for your MQTT-Broker, you have to provide the information as well in line 45:
#define MQTT_USERNAME "User"
and line 46:
#define MQTT_PASSWORD ""
Configure the CO2 "Topic"We have to create a topic to identify the CO2-values also on the Home Assistant side. If you want to change the topic names for the publisher and the subscriber you can do that in line 50 and 51. This step is optional and we kept the standard ones (pub - pasco2_status, sub - pasco2_config).
#define MQTT_PUB_TOPIC "pasco2_status"
#define MQTT_SUB_TOPIC "pasco2_config"
Now there is only one thing left to change and that is the format how the data is published. We are going to do that in order to be able to use the CO2-Value easier in Home Assistant. So let's get to it! First open the source folder and then the file pasco2_task.c:
Here, we have to scroll down until line 250 and change the line from
snprintf(publisher_q_data.data, sizeof(publisher_q_data.data), "{\"CO2 PPM Level\": \"%d\"}", ppm);
to
snprintf(publisher_q_data.data, sizeof(publisher_q_data.data), "%d", ppm);
This results in just publishing the raw CO2-Value of the sensor in ppm and makes it easier to process it in Home Assistant.
Flash the Connected Sensor KitNow we did all the necessary changes in the example code of the connected sensor kit and we are ready to flash the device. For this save all the files and build the project first (lower left corner of the IDE) to see if everything was done correctly:
If the build was successful you can upload the code with the KitProg3_MiniProg4-Flasher to the device:
If the flashing was successful as well we are done with setting up the connected sensor kit (CSK). But now you might wonder what is this example actually doing!? Don't worry, it is not too complicated, actually it is quite simple :)
First of all, the board will try to connect to your configured WiFi. If that was successful it will initialize all the necessary things to be able to talk to the CO2-Sensor. After that was successful as well the board will acquire data every 10 seconds and will publish that data on our defined topic to the MQTT-Broker. It also possible to change some configurations of the board via the MQTT interface, like the measuring period. If you're interested in that you can take a look in the README.md of the example.
If you have any problems with the CSK, just open a serial monitor and select the corresponding port of the CSK and change the baud rate to 115200. This shows you what is going on with the board, so for example if you have problems connecting to your MQTT-Broker or to your configured WiFi.
Let's configure Home AssistantThere is more the one way to configure Home Assistant, but we thought the easiest way is to provide the YAML-files that are necessary to get the setup shown in the beginning of this article. Home Assistant is using different YAML-files to describe, for example, devices, automations, scenes and a few other things. We will go into a bit more detail later. Let's copy the relevant content into our YAML-file first. For this you have to download the two attachments configuration.yaml and lovelace (which is a JSON) and either copy the whole files, or just their content to the right locations on your machine.
These locations are a bit depending on how you have installed the Home Assistant instance on your device. In our case we used a Docker-Container to install the Home Assistant and therefore the files are located in a folder called HAConf in home directory of the the Raspberry Pi, so:
/home/pi/HAConf
Inside this folder you will find the file configuration.yaml. As said you either can replace that file or edit its content. The lovelace-file can be found in the.storage folder, so under the following path in our case:
home/pi/HAConf/.storage
Be aware that this is a hidden folder, in order to see it in the file manager you have to enable hidden folders. If you have trouble finding your configuration and lovelace file this link might help you to find it.
If you have copied the files and don't see any changes in the Home Assistant web interface you probably have to the restart your device to make the changes visible. After that you should be able to see the following:
Here we have defined two different types of the lovelace GUI. A gauge type and a sensor type. The gauge is used for an easy, visual representation of the measured CO2-Sensor-Value. It defines three different areas in different colors. The first area is colored green, which means the CO2 level is good. The second one is yellow and tells you that you should consider to open the window. The red area means the CO2-Value reached a level where you should definitely open the window and get some fresh air into the room.
The three thresholds are chosen based on recommendations from different websites about air quality, but you can change them of course. If you want to do that you can open the lovelace file and change the values in line 20 to 22. These lines define the thresholds for green, yellow and red. See here:
...
"severity": {
"green": 0,
"yellow": 1000,
"red": 2000
},
...
The second element of the GUI is the sensor type. It is simply showing the value (with the corresponding unit) received from the sensor. But the advantage here is that it can you show a graph of the historical values over the past time. In this case over the last hour. This can of course be changed. For this open the lovelace file again and change line 33.
If you don't like to change things with the text editor you can also change the elements of the GUI (so called cards) also directly in the web-interface. For this click on the three dots in the upper right corner of the overview tab and choose Edit Dashboard.
Now you should see an Edit button below each GUI element.
If you click for example on the one from the gauge you should see the following:
Here you can see the things you're able to change and what else you can define. You can also see the three thresholds on the very bottom of this window, so feel free to use this option if you want to change them and don't like the text editor option. To save your changes press Save in bottom, right corner. You can do the same for the sensor type.
So that is it already! Now you have fully integrated the Connected Sensor Kit into your Home Assistant setup. Of course there are a lot more things that you can do with this sensor now in your system. For example trigger warnings on your smartphone if the CO2-Value exceeds a certain threshold. Or automatically open your window if the CO2 reaches a critical value. This could be done with the help of our smart window opener and is described in the next chapter.
But the last chapter can be skipped if you're just interested to bring the CSK into your smart home setup.
Example use case: Smart Window ExtensionIn order to keep it simple we will just provide the required YAML-files for the smart window extension and will explain a bit what they are doing and what you have to do in order to get the same setup.
First of all, we have to copy the content of the configuration YAML-File, the automations YAML-File and the lovelace-file in the ones create by Home Assistant (as we did in the previous chapter). You can either copy the whole content or just the things that were not in the YAML-File of the Smart Window Opener.
Of course you can also use your Home Assistant App to access the interface. But in our case we just visited the website. So let's quickly go through the things we're seeing here.
The gauge and sensor type card are explained in detail in the previous chapter and will be skipped here.
Right next to them you can find the buttons to manually control the window. We think they're self explaining :) But they were already in the available in the Smart Window Opener project, so they are not new.
Below them you can find a slider. You might wonder why we would need a slider? The reason why we put a slider there is that we wanted to easily control the threshold where the window is opened automatically. So in order to trigger the automation the CO2-Value has to rise above the set threshold of the slider and then the window will get the command to open.
EnjoySo that's should be it :) Ok let's be honest there a probably a million more things to do with that project. Like automatically closing the window again if the value is below the set threshold. Or you could open and close the window depending on the weather outside or control it in a certain time frame. We think there are endless possibilities so we are looking forward to see YOUR projects!
Comments