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:

co2sensor_pins

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

co2sensor_cables

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

To connect the sensor to a PC or Mac, you need an USBserial converter like this:

usbser1

These converters have different pinout. Check the board that you’re using and connect VCC to 3.3V, TX to RX, RX to TX and GND to GND.

Software

While it is quite easy to read out data from this sensor with your own software, you don’t have to be a programmer to use it.
The easiest way to use it is Home Assistant. We’ve created a small module for this sensor that you only have to activate in the configuration:

sensor 3:
  platform: mhz19
  serial_device: /dev/serial0

If you want to write your own program, you can use our pmsensor library that now also supports this CO2 sensor.

MP1584 buck converter module

This small module MP1584 buck-converter module seems to be a good solution to power small circuits from higher voltages. Especially cool with this chip is that it accepts input voltages up to 29V. This makes it a perfect candidate for additional circuits that connect to a KNX bus. But it’s not limited to KNX buses. If you want to build a WiFi interface for your Roomba, you also have to down-regulate the 15V battery voltage to 5V or even 3.3V. You want to something to power a circuit from a car battery? This seems to be a perfect circuit for there use cases.

mp1584With it’s tiny dimensions of 17 x 22mm it’s size is around the size of 2 MicroSD cards (and much smaller than a single SD card).

The output voltage of the module is controlled by a tiny potentiometer. You will need a multimeter to check the output voltage before connecting it to a circuit.

While the plain voltage range data seems to be quite good, how does the circuit behave on different loads? Some Chinese dealers claim that similar modules should handle 3A load. If you just look at the size of the inductor you might already start thinking that this most likely isn’t true. However, DC resistance of the inductor is approximately 10mOhm (I miss my Kelvin probes and therefore could only measure this very roughly).

Let’s start with 29V input voltage – this will bring everything to the limit. Output voltage is fixed to 5V in this experiment. Without any current drawn, it still looks a bit rough:

29_0

However, a voltage swing of (Vpp) 98mV is no problem at all. Between 0.1A and 0.9A the circuit behaves quite well. Vpp is around 0.2V (a bit lower on low currents, higher in higher currents)

29_0.5

We reach the end at 1A. Now the regulator doesn’t provide a stable output voltage anymore:
29_1

But what about lower input voltages? Let’s go the the other “extreme”: 9V (it will work with even less, but let’s give it a bit headroom).

Almost perfectly clean with no load:
9_0

A bit more stable than at 29V for currents between 0.1A and 0.7A:

9_0.4

And at 0.7A we reach the end of the useable current range:

9_0.7

Conclusion: This module works fine for almost every embedded use case: Arduino, ESP8266 WiFi modules and even a Raspberry Pi without any additional USB devices plugged in.

Here are 2 animation that shows the output voltage under different loads:

29V input, 5V output, 0-1A in 0.1A steps:

29v_animated

9V input, %V output, 0-0.7A in 0.1A steps:

9v_animated

Cool sensors for your next automation project

Home automation isn’t about controlling a lamp with your smartphone. It is called “automation”. A home automation software should do things automatically. Do do this, it needs to have an idea what’s going on in your apartment (or outside). For this, you need sensors. While some sensor types are well-known you might not know what is available already on the market for a small budget. The following sensors can be integrated easily with your Arduino, EPS8266 or Raspberry Pi.

Temperature

35040-img_0674Almost everybody already did experiments with temperature sensors. The DS18B20 is a 1-Wire sensor that can be connected easily to many platforms. You will find a lot of code snippets for this sensor already. It has a 0.5 degree celsius accuracy which is more than enough for most use cases. If you only want to monitor the temperature, go for this one.

Humidity

dht-11-1The DHT-11 and DHT-22 are two sensors that combine a temperature sensor with a humidity sensor. Both are more expensive than a simple temperature sensor. However, humidity informations might help you to do better automatisations. One use case could be turning on an air humidifier when the humidity is too low. Another would be opening windows, when humidity is too high. While these sensors also use a 1-Wire-type protocol, it is different from the 18B20 sensor. You can’t connect both sensors to the same GPIO. However as the DHT-11 has the temperature sensor already included, there is no need for an additional 18B20.

Soil moisture

13995201090You’re plant are dying regularly because you forget to water them? Another use case for a sensor. Soil moisture sensors are available in 2 variants: the cheap sensors just measure the soil resistance. The problem with this sensor type is corrosion (over a longer lifetime). Capacitive sensors are becoming more popular and don’t show this problem as the electrodes are isolated. However, they are more expensive and a bit less sensitive. You need to experiment a bit to find the correct threshold for alarming.

Motion detection

pir-motion-sensor-536x408You want to know if somebody is home? PIR motion sensors are also available for a few bucks and will give you information about people (or pets) moving. If somebody is just sitting or sleeping, the motion sensor won’t detect this. However, for many use cases this is just fine.

Sound pressure

grove-Sound-SensorYou’re not at home, but your teenage son. What do you think will happen? A party! Your neighbors will tell you tomorrow. Wouldn’t it be cool to automatically reduce the volume of your stereo system when the volume exceeds a specific level? This level might be higher at 5pm than at 11pm. A sound pressure sensor will do the job – at least until your son finds our where it is located and puts some damping onto it.

Small particles

Particle sensorSmall particles in the air can be a problem for some people – especially if you suffer from asthma. These emissions can come from various sources. Measuring the number of small particles in the air might give you an idea what might be the source and do something against it. These sensors are based on optical measurements. While this might sound complicated, sensors like this are not very expensive anymore.

433MHz window sensor

On eBay, Alibaba or other web sites, you can find cheap Window sensors that will send a data packet to inform you about the state of your windows. They often costs less than 10$ per unit, which makes it easy to test them.

They look like this:

Newest-High-quality-1Pcs-433-MHZ-Wireless-Home-Security-Door-Window-Sensor-Detector-with-Battery-for

But what’s inside? Let’s have a look. The part that is mounted on the window itself is basically just a magnet. You might already know what will be on the other side: a reed relay. But what else:

The electronic circuit is based on the HS1527. The nice thing about this chip is the 1uA standby current. Even with a small battery, the battery life will be relatively high. It is powered by a small 12V battery – I haven’t seen this battery type before.

The good thing about the chip is that you can use a lot of cheap 433MHz receivers connected to a Raspberry Pi or Arduino to receive the signals.

However, there is one major downside: This sensor sends only a data packet when you open the window, not when you close it. This is ok if you want to use it to detect break-ins, but it won’t work if you want to detect if a window is open (e.g. if you want to turn off the heating).

Therefore I can’t recommend this sensor type for home automation use.

Raspberry Pi as a Home Automation Server

The Raspberry Pi is a credit card-sized Linux computer on a single circuit board that runs on an SD card. It is powered off a 5V supply – so it can run all day it uses very little electricity and only costs around £25 – £45. The Raspberry Pi has a graphical output with a HDMI plug and a good standard of audio processing. It has networking capabilities, so you can plug it into your home network if you want to and configure it to automatically connect in the future, or you can plug it into a USB Wi-Fi dongle. As it is wireless, you can then place this server anywhere in your house because it is so portable (in a cupboard, below your desk, in the garage – even outside). The server is low cost, high performing and as of 2015, Raspberry Pi had sold over 8 million computers after launching in just 2012). The latest model is the Raspberry Pi 3 – modified to include wireless LAN and Bluetooth; making IoT and mobile projects more accessible. They were first designed to teach children how computers are made/ work, but are increasingly being used for a variety of other projects.

Moving your home automation code and programs over to the Raspberry Pi makes is simplistic in nature. You can connect low-level electronics to it, so you can read from sensors, but at the same time run Python scripts and interface with other devices on your network such as a Philips Hue hub – as well as pulling data from the internet. You also have the ability to create a graphical display if you want to; you can plug it into a monitor and plug in a keyboard and mouse and use it like a fully graphical-interfaced computer to do the programming. And because it is a Linux server too, you can connect to it via SSH on your home network.

The Raspberry Pi Foundation is committed to helping people learn about computers and how to solve problems in the digital world, so the company’s profits go straight into funding the training of teachers to help people use the technology they offer. They are a forward-thinking company, solving the modern-day shortage of programmers and coders by educating, training and enabling people to build their own HA servers in their own homes.

Home Security with Raspberry Pi and a Webcam

Home security has never been easier. Though the terms “front end development” and “open source” may be daunting to some, Raspberry Pi hardware is a cost-effective and efficient solution that makes Home Automation a DIY commodity.

For $39.99, Raspberry Pi, is paired with a Mirco SD card (at least 2GB), USB Hub and a compatible webcam, to create a simple means of home security.

The Raspberry Pi is a self-powered motherboard, that either remotely or connected to a monitor and keyboard via its own USB ports. Having inserted a formatted SD card into the slot of the Motherboard, you can begin set up your Home Automation system. Your Webcam needs to be connected to a USB hub which is then connected to the motherboard in order to supply power to your camera.

First, Raspberry Pi needs to install an operating system to its motherboard. Whereas you can access your OS’s terminal directly, use of NOOBS’ Raspbian integrated OS, is the most convenient set up for beginners. Note that this option can be booted directly onto your Raspberry Pi, through a preloaded SD card.

Having inserted your SD card into your computer’s card reader, you will first need to format it to FAT-32. Following this, you can then download NOOBS and its integrated Raspbian software. Upon installing Raspbian on your computer, Windows Clients will need to download Win32 in order to burn this OS to an SD card, whereas Mac and Linux clients can do so by opening the Disk Utility and Terminal.

Once this is done, your SD card can be inserted into the Raspberry Pi.

For the convenience of simply controlling your electronics; such as a webcam, Raspberry Pi can then be controlled remotely from another device over a local network using a Secure Shell (SSH).

For remote access, you will need to download a free IP Scanner client and an SSH client such as Putty.  This will identify your Raspberry Pi from your OS. Taking note of your hardware’s IP address in the scan, you can remotely configure your Pi through the SSH client.

To install the camera connected to your USB hub, you will need to configure your Raspberry Pi’s terminal to enable it, and create a webcam server. Over a remote control server, the webcam can then be accessed from using the Pi’s IP address. There is also configuration available online to access your webcam stream from an internet browser. Alternatively, you can connect a camera module to Raspberry Pi’s CSI port, enabling your Raspberry Pi to become its own IP webcam.

Bluetooth problems on the Raspberry Pi 3

The Bluetooth tracker in Home Assistant is a cool device as it allows you to track if people are home based on their mobile device. With the Raspberry Pi 3’s onboard bluetooth chip, this is a great features as it does not even require additional hardware. However, I’ve had major problems getting it running. Bluetooth was enabled on the mobile phone, but Home Assistant just did not find any device.

Ok, checking from command line:

[email protected]:~# hcitool scan
Scanning ...

No bluetooth devices, even if there are bluetooth enabled devices available. Let’s try with “blutoothctl”:

[email protected]:~# sudo bluetoothctl
[NEW] Controller B8:27:EB:76:70:7C raspberrypi [default]
[bluetooth]# devices
[bluetooth]# scan on
Discovery started
[CHG] Controller B8:27:EB:76:70:7C Discovering: yes
[NEW] Device 04:69:F8:xx:xx:xx 04-69-F8-xx-xx-xx
[NEW] Device 88:C6:26:xx:xx:xx 88-C6-26-xx-xx-xx
[bluetooth]# quit
[DEL] Controller B8:27:EB:76:70:7C raspberrypi [default]

Clearly, blutoothctl sees other devices. Maybe a daemon isn’t running?

[email protected]:~# systemctl status bluetooth
● bluetooth.service - Bluetooth service
   Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled)
   Active: active (running) since Mon 2016-06-13 15:53:36 UTC; 1h 54min ago
     Docs: man:bluetoothd(8)
 Main PID: 729 (bluetoothd)
   Status: "Running"
   CGroup: /system.slice/bluetooth.service
           └─729 /usr/lib/bluetooth/bluetoothd

Jun 13 15:53:36 raspberrypi systemd[1]: Starting Bluetooth service...
Jun 13 15:53:36 raspberrypi bluetoothd[729]: Bluetooth daemon 5.23
Jun 13 15:53:36 raspberrypi systemd[1]: Started Bluetooth service.
Jun 13 15:53:36 raspberrypi bluetoothd[729]: Starting SDP server
Jun 13 15:53:36 raspberrypi bluetoothd[729]: Bluetooth management interface 1.10 initialized
Jun 13 15:53:36 raspberrypi bluetoothd[729]: Sap driver initialization failed.
Jun 13 15:53:36 raspberrypi bluetoothd[729]: sap-server: Operation not permitted (1)

Ok, this looks strange. While bluetoothd is running, there is an error message from sap-server. Looking for the error message, I found a long thread on raspberypi.org. The solution: add a command line option to the Bluetooth daemon

Edit the file /etc/systemd/system/bluetooth.target.wants/bluetooth.service and add the option “–noplugin=sap” to the ExecStart line:

[Unit]
Description=Bluetooth service
Documentation=man:bluetoothd(8)

[Service]
Type=dbus
BusName=org.bluez
ExecStart=/usr/lib/bluetooth/bluetoothd --noplugin=sap
NotifyAccess=main
#WatchdogSec=10
#Restart=on-failure
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
LimitNPROC=1

[Install]
WantedBy=bluetooth.target
Alias=dbus-org.bluez.service

Ok, let’s check the daemon again:

[email protected]:~# systemctl status bluetooth
● bluetooth.service - Bluetooth service
   Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled)
   Active: active (running) since Mon 2016-06-13 17:56:35 UTC; 9s ago
     Docs: man:bluetoothd(8)
 Main PID: 19106 (bluetoothd)
   Status: "Running"
   CGroup: /system.slice/bluetooth.service
           └─19106 /usr/lib/bluetooth/bluetoothd --noplugin=sap

Jun 13 17:56:35 raspberrypi bluetoothd[19106]: Bluetooth daemon 5.23
Jun 13 17:56:35 raspberrypi bluetoothd[19106]: Starting SDP server
Jun 13 17:56:35 raspberrypi bluetoothd[19106]: Excluding (cli) sap
Jun 13 17:56:35 raspberrypi bluetoothd[19106]: Bluetooth management interface 1.10 initialized
Jun 13 17:56:35 raspberrypi systemd[1]: Started Bluetooth service.

This looks better. However, even after a reboot,

hcitool scan

still does not show any Bluetooth device.

To be continued …

Using Home Assistant to control your home (and more)

If you’re looking for an home automation software, you will find a lot of projects. There is a huge interest in this area and a lot of developers working on different projects. Many focus on the visualisation. While it is important to have a good user interface, the main focus of an automation system should be it’s backend.
One project I really like is Home Assistant. It uses a very clean backend/frontend separation.You can easily add your own user interface by just using its web API. It is written in Python. That makes it easier for me to add missing functionalities.

An architecture based on Home assistant could look like this:

ha-architecture

KNX isn’t supported out-of-the box in the current Home Assistant release (June 2016). However, I already did some programming and will work on integrating it into the official Home Assistant release.

Install an MQTT broker on your Raspberry Pi

Not only for home automation purposes, a central MQTT broker can be a good system that connects sensors and control applications. MQTT has been designed especially to be very lightweight. Therefore, it will also work very well on a Raspberry Pi. Installing it is very simple:
sudo apt-get install -y mosquitto mosquitto-clients

Your Mosquitto server should be already running now. You can test this easily.
mosquitto_sub -h localhost -v -t test

This starts a MQTT subscriber waiting for incmoming messages on the “test” channel.

Now open a new connection to your Raspberry Pi and type the following:
mosquitto_pub -h localhost -t test -m "Hello world, Mosquitto"

The first process should now display the “Hello world” message.