Room ventilation system based on “raspberry” and “CO2 detector”

    The fashion for smart homes has swept the entire Internet, everyone now wants to make a light bulb that can turn off via the Internet or flip a fan in the toilet. My experience was quite simple: I wanted to make a system that can automatically ventilate a room, no more, no less.

    After considering the principle of the system, I came to some conclusions, which I decided to implement. As planned, the entire functionality of the system is divided into three components: the "brains" that control everything, the ventilation system for opening the window / or turning on the fan, and the sensor itself that monitors the air quality in the room. I’ll tell you more about the air quality sensor, plus a little touch on the topic of integrating it with the “brains” of a smart home.

    Device Overview


    I decided to control the quality of “freshness” of air by the concentration of carbon dioxide. Future plans include launching sensors for ammonia, propane / butane (gas leaks), dust concentrations in air, etc.

    It was decided to place the ventilation system in some living room where a person spends as much time as possible. Probably, such a room is a bedroom, and it is there that an automatic ventilation system will be very useful during a night's sleep.



    It’s no secret that the concentration of carbon dioxide in the air strongly affects not only the productivity of the human brain, human performance, but also the ability to take a good rest. The higher the concentration of CO2, the worse.



    The carbon dioxide detector of the company Dadget was used as a device for measuring carbon dioxide concentration.

    This device can receive power from the USB port of a computer or laptop. On the front panel - LCD indicator, which displays information on the concentration of carbon dioxide and ambient temperature. There are also 3 LEDs that give the user a visual representation of the content. Everything is quite simple - turn on the device. And everything works.

    I was attracted to the fact that there is software that works with this device. There is software for both Windows and Linux. The source code of the program for Linux is on github and seems to be written by our compatriot. And if you have the source code, you can think of something, modify it, "sharpen" the program for yourself.

    Workflow Overview


    As practice and google showed, the device is seen by the computer as a HID device. Therefore, in the program for Windows there is a dll called HIDApi. To work with Linux, you need the HIDApi library of the same name. I finalized the source code for linux software and wrote a simple squeak.

    It’s clear that using a Linux computer to read data from the sensor and transferring it to a smart home controller somewhere is blasphemy. Therefore, it was decided to use Raspbery pi with pure Debian installed. In the future, it is planned to do everything on Arduino and transmit via wireless channels.

    The scheme of my hardware-software complex is as follows:



    A knowledgeable reader and an avid geek might think that it is not very sporty to use HUB in this scheme. That's right, you can directly connect a CO2 monitor and enjoy life. Using such a scheme, I solve several problems at once:
    ● most often the brains of a smart home are not where the sensor is;
    ● using raspberry pi makes it possible to connect additional sensors;
    ● placing the sensor separately, we get two methods of indication, the first on the sensor screen, the second on the phone or through a web page, since the data is transmitted to the controller.

    A MicasaVerde device is used as a smart home controller, the device can collect data from sensors and make decisions according to a given program, which are called “scenes” in its terminology.



    In the device, you can create virtual sensors and update their information using http requests, which I did with raspberry pi.
    Example request:
    ip_address : 3480 / data_request? Id = variableset & DeviceNum = 6 & serviceId = urn: micasaverde-com: serviceId: DoorLock1 & Variable = Status & Value = 1
    That is, Malinka reads data from the carbon dioxide detector, and then transmits the data http request to Vera. The scheme is simple but working.

    Software revision



    So there is a "raspberry", no matter what version, the main thing is that it has a network interface.

    Next, you need to install support for hid devices. This library is taken from git, so git should be installed on the “malink”:

    apt-get install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev

    Next, download and

    install the mkdir hidapi
    cd hidapi /
    git init
    git clone
    library itself github.com/signal11/hidapi.git

    Additional libraries
    sudo apt-get install libudev-dev libusb-1.0-0-dev libfox-1.6-dev autotools-dev autoconf automake libtool

    Install
    cd hidapi /
    ./bootstrap
    ./configure
    make
    sudo make install


    After that, download the source code for the CO2 monitor program
    mkdir co2monitor
    cd co2monitor
    git init
    git clone github.com/dmage/co2mon.git


    Next, I tweaked the program code a bit, in the initial version it reads two parameters (temperature and co2) and sends it to the console in an endless loop . This option didn’t suit me, the goal was to run it once and get the value once, so I changed two lines in the main.c.

    We are looking for lines

    printf ("CntR \ t% s \ n", buf);
    fflush (stdout);


    And change to

    printf (“% s \ n", buf);
    fflush (stdout);
    exit (1);


    The first line is responsible for outputting the value, after the change, only the digit will be displayed, and after the output, the program will close, the exit (1) line will respond.

    Compile
    cmake ...
    make


    Then a simple script was written that starts the program, assigns the output values ​​of the program compiled above to a variable, and then passes this value using curl to MicasaVerde. After that, add the udev rule so that the system has access to the sensor without root.

    SUBSYSTEM == "usb", ATTR {idVendor} == "04d9", ATTR {idProduct} == "a052", MODE = "0666"

    And we will add a polling and transmission script to cron, every minute.

    crontab -e
    And add the line
    * * * * * /home/pi/co2sender.sh


    The result of the script

    Of course, you still have to configure MicasaVerde files so that there are adequate records everywhere. But it works, fast and easy.

    Conclusion



    As a result, we have a system based on the CO2 Detector , which operates as usual, analyzes and reports on changes in carbon dioxide concentration. At the same time, Malinka reads data from the sensor once a minute and transfers it to the smart home controller, which can turn on the ventilation system if necessary.

    Also popular now: