Monitor CO2 levels in your house

When breathing, humans and animals increase the concentration if CO2 in the exhaled air. This is a normal biological process. In the outside this is not a problem as plants consume the additional CO2 (I’m not talking about the long-term effects of increasing CO2 concentration by burning fossil fuels).
However in closed living rooms without enough ventilation, CO2 levels can increase quite a lot. Especially modern buildings without a well-designed ventilation system can be a bit problematic. Therefore, monitoring CO2 levels in living rooms is a good idea as it gives you a good indication when you should increase the ventilation (e.g. by opening the windows for some time).

While outside CO2 levels are normally between 250 and 350ppm (parts per million), inside levels can be up to multiple 1000 ppm. You can see here that CO2 levels above 1000ppm can lead to drowsiness, poor concentration, loss of attention or increased heart rate.

An easy method to monitor CO2 levels are nondispersive infrared sensors. If you want to use a PC or Raspberry Pi a sensor with a serial interface is a good choice. The MH-Z19 features a serial interface and 3.3V power supply and IO levels. This makes is easy to connect it directly to a Raspberry Pi or to a PC (using an USB/serial converter).

Looking at the sensor from the bottom, you will see the following pins:

hmz19-pinout

The pins have a 2.54mm (0.1″) pitch, that makes it easy to solder a simple one-row pin header like this:

TODO: picture

After this, you can connect the sensor with jumper cables:

TODO: picture

Connecting to a Raspberry Pi

You can connect the sensor directly to the 40-pin GPIO interface:

Function Pin MH-Z19 Pin RPI
VCC 6 1
GND 7 6
RX 2 8
TX 3 10

Connecting to a PC

Reverse engineering the Mi flora plant sensor

The Xiaomi Mi plant sensor is a well-made plant sensor that does not only measure the humidity, but also fertility, temperature and environment light. Pretty cool for a small device like this. And even cooler is that it uses Bluetooth Low energy for communication with your mobile device. This should make it easily hackable.

Ok, let’s start and check what characteristics are available:

handle = 0x0002, char properties = 0x02, char value handle = 0x0003, uuid = 00002a00-0000-1000-8000-00805f9b34fb
handle = 0x0004, char properties = 0x02, char value handle = 0x0005, uuid = 00002a01-0000-1000-8000-00805f9b34fb
handle = 0x0006, char properties = 0x0a, char value handle = 0x0007, uuid = 00002a02-0000-1000-8000-00805f9b34fb
handle = 0x0008, char properties = 0x02, char value handle = 0x0009, uuid = 00002a04-0000-1000-8000-00805f9b34fb
handle = 0x000d, char properties = 0x22, char value handle = 0x000e, uuid = 00002a05-0000-1000-8000-00805f9b34fb
handle = 0x0011, char properties = 0x1a, char value handle = 0x0012, uuid = 00000001-0000-1000-8000-00805f9b34fb
handle = 0x0014, char properties = 0x02, char value handle = 0x0015, uuid = 00000002-0000-1000-8000-00805f9b34fb
handle = 0x0016, char properties = 0x12, char value handle = 0x0017, uuid = 00000004-0000-1000-8000-00805f9b34fb
handle = 0x0018, char properties = 0x08, char value handle = 0x0019, uuid = 00000007-0000-1000-8000-00805f9b34fb
handle = 0x001a, char properties = 0x08, char value handle = 0x001b, uuid = 00000010-0000-1000-8000-00805f9b34fb
handle = 0x001c, char properties = 0x0a, char value handle = 0x001d, uuid = 00000013-0000-1000-8000-00805f9b34fb
handle = 0x001e, char properties = 0x02, char value handle = 0x001f, uuid = 00000014-0000-1000-8000-00805f9b34fb
handle = 0x0020, char properties = 0x10, char value handle = 0x0021, uuid = 00001001-0000-1000-8000-00805f9b34fb
handle = 0x0024, char properties = 0x0a, char value handle = 0x0025, uuid = 8082caa8-41a6-4021-91c6-56f9b954cc34
handle = 0x0026, char properties = 0x0a, char value handle = 0x0027, uuid = 724249f0-5ec3-4b5f-8804-42345af08651
handle = 0x0028, char properties = 0x02, char value handle = 0x0029, uuid = 6c53db25-47a1-45fe-a022-7c92fb334fd4
handle = 0x002a, char properties = 0x0a, char value handle = 0x002b, uuid = 9d84b9a3-000c-49d8-9183-855b673fda31
handle = 0x002c, char properties = 0x0e, char value handle = 0x002d, uuid = 457871e8-d516-4ca1-9116-57d0b17b9cb2
handle = 0x002e, char properties = 0x12, char value handle = 0x002f, uuid = 5f78df94-798c-46f5-990a-b3eb6a065c88
handle = 0x0032, char properties = 0x0a, char value handle = 0x0033, uuid = 00001a00-0000-1000-8000-00805f9b34fb
handle = 0x0034, char properties = 0x1a, char value handle = 0x0035, uuid = 00001a01-0000-1000-8000-00805f9b34fb
handle = 0x0037, char properties = 0x02, char value handle = 0x0038, uuid = 00001a02-0000-1000-8000-00805f9b34fb
handle = 0x003b, char properties = 0x02, char value handle = 0x003c, uuid = 00001a11-0000-1000-8000-00805f9b34fb
handle = 0x003d, char properties = 0x1a, char value handle = 0x003e, uuid = 00001a10-0000-1000-8000-00805f9b34fb
handle = 0x0040, char properties = 0x02, char value handle = 0x0041, uuid = 00001a12-0000-1000-8000-00805f9b34fb

Looks like some standard characteristics (the uuid’s starting with 00002) and a lot of non-standard. Let’s check the standard characteristics first. UUID 00002a00-0000-1000-8000-00805f9b34fb should contain the device name:

# gatttool --device=C4:7C:8D:60:8F:E6 --char-read -a 0x03
Characteristic value/descriptor: 46 6c 6f 77 65 72 20 6d 61 74 65 

“46 6c 6f 77 65 72 20 6d 61 74 65” is “Flower mate” in ASCII. Cool. Seems that Xiaomi is using the standards – not something we see from every Chinese company.

How do you know that this UUID contains the name? Check out the list of GATT characteristics on the Bluetooth web site.

Ok, let’s have a look at all characteristics that return values:

0x03 : 160823-164329 : 46 6c 6f 77 65 72 20 6d 61 74 65
0x05 : 160823-164329 : 00 00
0x07 : 160823-164329 : 00
0x09 : 160823-164329 : 0a 00 14 00 00 00 f4 01
0x0e : 160823-164329 : 01 00 ff ff
0x12 : 160823-164329 : c3 c8 fd 30
0x15 : 160823-164329 : 98 00
0x17 : 160823-164329 : 1f 8e 8e 13 86 dd a0 d8 52 66
0x1d : 160823-164329 : 4c cc ca 13 fc fa f7 a6 51 50 c6 85 ee 61 85 47 7f 3d d6 6b
0x1f : 160823-164329 : 2e a1 bc 3e b3 87 94 e9 68 6f ff b7
0x2f : 160823-164329 : 00
0x33 : 160823-164329 : aa bb
0x35 : 160823-164329 : f5 00 00 00 00 00 00 10 61 00 00 00 00 00 00 00
0x38 : 160823-164329 : 64 10 32 2e 36 2e 32
0x3c : 160823-164329 : aa bb cc dd ee ff 99 88 77 66 55 44 33 22 11 10
0x3e : 160823-164329 : aa bb cc
0x41 : 160823-164329 : 81 8d 23 00

Hmm, look pretty wild. Looking at the data you have no idea what they might encode.

Let’s try something different and dump all values every 10 minutes.

We see a lot of values that do not change at all like this:

0x38 : 160823-073844 : 64 10 32 2e 36 2e 32
0x38 : 160823-074852 : 64 10 32 2e 36 2e 32
0x38 : 160823-075856 : 64 10 32 2e 36 2e 32
0x38 : 160823-080859 : 64 10 32 2e 36 2e 32
0x38 : 160823-081909 : 64 10 32 2e 36 2e 32
0x38 : 160823-082914 : 64 10 32 2e 36 2e 32
0x38 : 160823-083918 : 64 10 32 2e 36 2e 32
0x38 : 160823-084922 : 64 10 32 2e 36 2e 32
0x38 : 160823-085925 : 64 10 32 2e 36 2e 32
0x38 : 160823-090931 : 64 10 32 2e 36 2e 32
0x38 : 160823-091936 : 64 10 32 2e 36 2e 32
0x38 : 160823-092940 : 64 10 32 2e 36 2e 32
0x38 : 160823-093945 : 64 10 32 2e 36 2e 32
0x38 : 160823-094951 : 64 10 32 2e 36 2e 32
0x38 : 160823-100006 : 64 10 32 2e 36 2e 32
0x38 : 160823-101010 : 64 10 32 2e 36 2e 32
0x38 : 160823-102014 : 64 10 32 2e 36 2e 32
0x38 : 160823-103018 : 64 10 32 2e 36 2e 32
0x38 : 160823-104022 : 64 10 32 2e 36 2e 32

Others seem to change randomly like this

0x41 : 160823-073844 : d4 0d 23 00
0x41 : 160823-074852 : 31 10 23 00
0x41 : 160823-075856 : 8e 12 23 00
0x41 : 160823-080859 : f0 14 23 00
0x41 : 160823-081909 : 4d 17 23 00
0x41 : 160823-082914 : aa 19 23 00
0x41 : 160823-083918 : 02 1c 23 00
0x41 : 160823-084922 : 5f 1e 23 00
0x41 : 160823-085925 : bc 20 23 00
0x41 : 160823-090931 : 19 23 23 00
0x41 : 160823-091936 : 76 25 23 00
0x41 : 160823-092940 : d3 27 23 00
0x41 : 160823-093945 : 30 2a 23 00
0x41 : 160823-094951 : 97 2c 23 00
0x41 : 160823-100006 : f4 2e 23 00
0x41 : 160823-101010 : 51 31 23 00
0x41 : 160823-102014 : ae 33 23 00
0x41 : 160823-103018 : 06 36 23 00
0x41 : 160823-104022 : 63 38 23 00
0x41 : 160823-105025 : c0 3a 23 00

But then there is one characteristic with very controlled changes:

0x35 : 160823-073844 : f2 00 00 68 00 00 00 10 66 00 00 00 00 00 00 00
0x35 : 160823-074852 : f2 00 00 54 00 00 00 10 66 00 00 00 00 00 00 00
0x35 : 160823-075856 : f1 00 00 54 00 00 00 10 66 00 00 00 00 00 00 00
0x35 : 160823-080859 : f2 00 00 54 00 00 00 10 66 00 00 00 00 00 00 00
0x35 : 160823-081909 : f2 00 00 68 00 00 00 10 66 00 00 00 00 00 00 00
0x35 : 160823-082914 : f2 00 00 54 00 00 00 10 66 00 00 00 00 00 00 00
0x35 : 160823-083918 : f2 00 00 68 00 00 00 10 65 00 00 00 00 00 00 00
0x35 : 160823-084922 : f2 00 00 68 00 00 00 10 66 00 00 00 00 00 00 00
0x35 : 160823-085925 : f2 00 00 57 00 00 00 10 66 00 00 00 00 00 00 00
0x35 : 160823-090931 : f2 00 00 57 00 00 00 10 65 00 00 00 00 00 00 00
0x35 : 160823-091936 : f2 00 00 57 00 00 00 10 65 00 00 00 00 00 00 00
0x35 : 160823-092940 : f2 00 00 57 00 00 00 10 65 00 00 00 00 00 00 00
0x35 : 160823-093945 : f2 00 00 57 00 00 00 10 65 00 00 00 00 00 00 00
0x35 : 160823-094951 : f2 00 00 6b 00 00 00 10 65 00 00 00 00 00 00 00
0x35 : 160823-100006 : f2 00 00 6b 00 00 00 10 63 00 00 00 00 00 00 00
0x35 : 160823-101010 : f2 00 00 6b 00 00 00 10 63 00 00 00 00 00 00 00
0x35 : 160823-102014 : f2 00 00 79 00 00 00 10 65 00 00 00 00 00 00 00
0x35 : 160823-103018 : f2 00 00 79 00 00 00 10 63 00 00 00 00 00 00 00
0x35 : 160823-104022 : f2 00 00 6b 00 00 00 10 65 00 00 00 00 00 00 00
0x35 : 160823-105025 : f2 00 00 79 00 00 00 10 65 00 00 00 00 00 00 00

Seems like there are 4 bytes of values and a lot of zeros. Could these 4 byte be the sensor values? They are!
Now let’s check the values in the Mi app on the iPhone:

  • 24.2 degree celcius
  • 121 Lux
  • 16% moisture
  • 101 us/cm fertility (whatever this means)

Except for the temperature these values map nicely to the data:

  • 16 = 0x10 – byte 8
  • 121 = 0x79 – byte 4
  • 101 = 0x65 – byte 9

The only thing that isn’t yet clear is the encoding of the temperature.

Also the sensor should be able to measure up to 10000 lux. This won’t work with a single byte. So, let’s put another plant it into the bright sunlight:

25 01 00 f7 26 00 00 28 0e 01 00 00 00 00 00 00
  • Fertility is now 270, which is bytes 9 and 10 (MSB byte 9, LSB byte 10)
  • Sunlight is 9975 lux which is stored in bytes 4 and 4 (MSB byte 4, LSB byte 5)
  • And now it is also getting clear how the temperature is encoded. It is in 0.1 degree Celcius steps in bytes 1 and 2, which gives us 29.3 degree celcius in this example (which fits the data displayed by the app).

Environment measurements with the ESP8266

If you want to measure just a few environment value and send them to a central server, the ESP8266 is a great platform. And it is even cooler when I tell you that you can do it without even programming. Check out the ESP Easy firmware. This firmware allows to configure sensors and actuators connected to the ESP8266 without the need to write your own program. You can just configure the sensors in the web interface:ESPEasy1

 

Selecting the right BLE module for your project

Bluetooth Low Energy is becoming more and more popular for sensor applications.  It not only needs much less energy than normal Bluetooth interfaces, even the transmission rage is longer. It isn’t design for huge data transfers, but that’s often not needed.

Unfortunately almost none of the popular development platforms (Arduino, ESP8266) comes with BLE onboard. This isn’t a big problem as there are many BLE modules available.

However, make sure you read the documentation of the module before ordering it.

hm13

I just received this small and inexpensive HM13 module. I wanted to use it to read data from a BLE plant sensor. Unfortunately when I checked the documentation I noticed that the firmware on this board is designed for serial data transmissions over BLE. It seems you can’t use it as a general purpose BLE interface.

Therefore check the documentation of a module before ordering to make sure the module supports the features you need.

 

Monitoring your laundry

You might know the problem that you put laundry in your washing machine or the dryer and forget about it.  While the dryer is usually less critical, it’s no fun to leave the laundry in the washing machine for hours.

Nolan Gilley wrote a nice article how to use accelerator sensors to monitor your laundry with Home Assistant. Check it out.

protoboard

I will further look into this. It would be very cool to use BLE for this and use energy harvesting from the vibrations to power the circuit. But unfortunately piezo elements for energy harvesting are quite expensive.

Fantastic or fantasy? Testing LiIon battery fakes

In some Chinese shopping sites you will find LiIon batteries with fantastic capacities. You can find 18650-type batteries from UltraFire with a capacity of almost 10Ah.

fake_batteries

That doesn’t sound too good to be true? It is.

Let’s do some fact checking first and look at the UltraFire web site. You will see that the UltraFire 18650 LiIon batteries have a capacity of 2500mAh. Here it is clear that these batteries must be fakes. Can somebody build a battery of the same size with almost 4 times the capacity? No! This just isn’t possible. These batteries are fakes and the capacity isn’t specified correctly!

But do they provide at least the same capacity? Let’s check. The setup for the capacity check is simple. The battery will be fully charged and then discharges with a constant current of 500mAh (this value had been chosen to limit the stress on the batterie, but also make sure that the measurement doesn’t take too long). You should not discharge a LiIon cell below 2.7V, therefore this is the point where the battery is “fully discharged”. Now you only have to measure how long it takes from full charge until the cell voltage is down to 2.7V. Then the capacity is very easy to calculate as time x current.

This measurement is easy for me as my electronic load that is used for power supply tests has a battery mode that not only turns off the load at a given end voltage, but also automatically calculates the capacity.

Measurement alternatives
You can setup a measurement like this even with a simple resistor, but the current won’t be constant. However, for a rough approximation, you can just assume that the cell voltage is constant at 3.7V the whole time. The real capacity will be a bit lower. Just make sure you turn off the current when the cell voltage is at 2.7V. Fully discharging the cell will most likely damage it and make it unusable.

Results

Now comes the funny part. I did not expect that the capacity is even close to the specific value, but have a look at these values of this “9800mAh” battery:

LiIon capacity test

Really? Around 1100mAh? That’s 11% of the advertised capacity. It also means that this cell performs worse than standard NiMh cells. It is hard to understand how you can build a LiIon battery that performs that bad.

If you wonder, why the display shows 3.143V: the battery voltage recovers when turning off the load. However, if you turn on the load again, it will drop again to 2.7V very fast.

I’ve tested two 18650 batteries. The perform both similarly bad.

I’ve also tested two 16340 batteries that are advertised as “2300mAh”. Their real capacity was also around 10-11% of the advertised capacity (230-240mAh).

Conclusion

While there are some cheap offers of LiIon batteries on Chinese shopping sites, don’t even try these things. They’re not worth any money. Also note that this test doens’t say anything about real UltraFire batteries. I will try to find some original batteries and perform tests with them in the future.

Low costs temperature & humidity sensors

The first thing you usually start when you implement environment measurements is temperature and humidity. These are not only important for the personal comfort, but also easy to measure. Most people use DHT sensors for this. They are cheap and easy to connect to an Arduino, ESP8266 or Raspberry Pi. There are also tons of software snippets and libraries around, which makes integration very easy. How do they differ? Are there alternatives?

DHT11

This sensor is the smallest of the 3 DHT versions. It has a blue color and it is the smallest of the 3. It is also the cheapest. Humidity and temperature resolutions is only full degree/%.
It can be directly soldered onto a PCB or simply plugged into a breadboard, which makes it great for initial prototypes.

DHT22

This one looks similar to the DHT11, but is larger and white. Its resolution is 0.1°/0.1%. It also can be directly soldered onto a PCB or plugged into a breadboard.

AM2301 (DHT21)

This is the largest version. It comes with cables. This makes solder-free prototyping a bit challenging, but if you already have soldering equipment, this shouldn’t be an issue. The bulky design and cables makes it a good choice for bigger designs. It is especially useful if your circuit uses a lot of energy and heats up its environment.

Accuracy

How accurate are these sensors? As the DHT11 has only 1°/1% resolution, you might expect a lower accuracy. However, don’t mistake resolution for accuracy. We could also read the data sheets. However, I recommend checking out Robert’s page. He did a lot of measurements and found out that the DHT11’s performance is similar to the DHT22.
However, I would still recommend the AM2301 or DHT22 for another reason. While the absolute accuracy might not be perfect, the increases resolution still is helpful. There isn’t a lot of visible noise on the measurement. This helps you to understand trends a bit better. Especially in modern buildings, temperature changes very slowly. Even if you turn off the heating, it might take hours until the temperature is down by a single degree. With a 0.1% temperature resolution you see these trends much better than with 1%.

Have a look at these measurements:
trend
It is easy to see there there is a slight downward trend in the 0.1% sensor data (green line), but it is hard to figure out if there is any trend in the 1% sensor data (orange line).

Defectives

I’ve read some remarks on the internet about defective sensors. For a long time I never had issues, but when I moved a prototype on a breadboard to another room, the measurements looked like this:
Screenshot 2016-08-01 09.58.35
This is clearly not correct. The new measurements show so much noise that they are basically useless. It seems that the sensor had been damaged somehow. I’m not sure, if the reason was ESD or anything else happened.

Recommendation

I would not recommend the DHT11 for any real-world measurements in home automation as the 1°/1% resolution is problematic. Depending on your use case, choose between the DHT21 and DHT22. Make sure you mount the sensor away from any part that dissipates heat. Otherwise your measurements will be completely wrong!

Teardown: Mi Thermometer/humidity sensor

Many people know Xiaomi for their smartphones. They are becoming more and more popular also in Europe. However, in their home market in China they sell also smarthome components. As many of these systems they use a central hub that connects to and controls sensors and actuators. One sensor that I personally found very interesting is the Mi temperature/humidity sensor. With it’s diameter of only 36mm, you can install it almost everywhere.

One thing, you will notice is that the build quality is very good. The case is plastics, but still feels very well made.

Is this sensor locked to the Xiaomi smart hub or can you use it also in DIY environments? Let’s open it and see how it works.

P7260896

The device is powered by a small CR2032 cell. Let’s hope this one lasts at least a year. But let’s check the main board which will tell us more about how this thing works:

mi-therm-board

It also looks very well-designed and assembled. The main processor is a JN5169 from NXP. It is a Zigbee controller. Do you remember Zigbee? While today most new designs use Bluetooth LE, Zigbee is still there. And it works quite well, so why not use it. The only thing that makes it a bit harder to use is the fact that no PC comes with an integrated Zigbee interface. That means you need to have an additional Zigbee controller connected to your PC, Raspberry Pi or Arduino to read data from this sensor. Is it easy to integrate? We don’t know yet, but we will look into this in the future.

Home Assistant: Logging to a central server

As the Raspberry Pi is relatively cheap, you might think: Why not having multiple Home Assistant installations running in my house? This gives you some advantages:

  • You might monitor temperature and humidity in different rooms.
  • If one instance fails, the other can still do the same job.

However, even with multiple HA instances, it might make sense to have the sensor data logged to a central instance. This makes data analytics much easier.

I use InfluxDB to store the sensor data. With our latest patch that will be included in Home Assistant 0.25, it is now possible to distinguish between the data from the different HA instances.

The logging configuration on the two HA instances would then look like this:

influxdb:
  host: 192.168.1.199
  tags:
    instance: living_room
influxdb:
  host: 192.168.1.199
  tags:
    instance: balcony

Teardown: Broadlink RM Mini 3

You might have read already, that I don’t like the Broadlink products as they do not offer any open APIs. But maybe, they are at least hackable?

Let’s check the RM Mini 3 Infrared sender/receiver.

Taking of the cap shows us the infrared LEDs and a single IR receiver:

rm3-1

This is what I would have expected. Are there any surprises on the main board?

rm3-2

Not really. The main processor is an Marvel 88MC200. This is a simple Cortex M3 based micro-controller that is clocked at 200 MHz. There is no need for a more powerful processor in this device. As it doesn’t have a WiFi controller integrated, there is a second controller on the board: the 88W8801 is a Marvel WiFi chip.

While in theory it might be possible to upload a different software onto the system, it will be quite complicated. You don’t want to fiddle around multiple days to re-use a 20$ device? The easiest way to re-use it is removing the whole controller, replace it with an ESP8266 and put your own firmware onto it. However, for this you don’t need to buy this device first. You can just directly connect some IR LEDs to an ESP8266 chip. This will be cheaper than the RM Mini 3.