We connect a wireless radiation dosimeter to the “People's monitoring” service via Raspberry PI

    Introduction


    In this article I will explain how to connect the AtomTag dosimeter via the Bluetooth module Bluegiga BLED112 to the Raspberry PI in order to transfer the measurement results to the “ People Monitoring ” service.

    AtomTag - Bluetooth Low Energy dosimeter for smartphone and tablet with Geiger meter SBM-20. The device will transmit to the server: dose rate, statistical error and battery charge. At the end of the article we will see how dosimeter readings are related to weather phenomena.

    People's monitoring (narodmon.ru) is a SaaS geoinformation service for displaying on the world map and control (on PCs, smartphones and other gadgets) sensor readings of its participants (temperature, humidity, atmospheric pressure, wind speed and direction, radiation, power consumption and many others) as well as private and city webcams for public or private (private) viewing.

    Generic Attribute Profile (GATT)


    AtomTag dosimeter supports GATT profile. Bluetooth terminology, a profile is a set of features or capabilities available for a particular Bluetooth device.

    The GATT profile defines a hierarchical data storage structure. The structure is shown in the figure:



    Service is a container containing several attributes, which are called characteristics. All services have unique identifiers UUID and HANDLE. For example, a dosimeter has 2 services:

    1. A service for a user, which contains characteristics for reading measurement results and recording user settings (audio alarm settings).

    2A service that contains characteristics for storing factory settings: calibration coefficients, device name, etc.

    Characteristic - consists of:

    1. Values ​​(usually no more than 20 bytes).

    2. Descriptor - this describes the purpose of the characteristic, the type of stored data, and the settings of the characteristic.

    3. Unique UUID and HANDLE.
    The data we are interested in: the number of registered pulses and the charge of the battery are stored in the characteristics.

    BLED112



    The module is a USB-CDC device, which is defined in the system as / dev / ttyACM0 and does not require installation of any drivers on Raspbian Jessie Lite with kernel version 4.4. Data exchange with the module is also performed as with a serial port. The exchange protocol is binary. We will not write the protocol parser ourselves, since There are a lot of commands for this module and we will take the C language SDK from the manufacturer. The link to the SDK will be at the end of the article.

    Raspberry PI Software


    We will need the following files from the SDK:

    1. cmd_defs.c, cmd_defs.h
    2. apitypes.h
    3. commands.c
    4. uart.c, uart.c The

    API is callback-based. In the commands.c file, implementations are declared “stubs” for unused callbacks. The SDK defines 2 types of messages that may come from the module: the event and the result of the operation. In our program, we will receive data from the dosimeter using alerts that are sent by the dosimeter every 2 seconds when the measurement characteristic changes. As a result, once in 2 seconds an event from the module will come and the corresponding callback will be called.

    Let us analyze the connection and data exchange algorithm with the dosimeter:

    1. Opens port /dev/ttyACM0

    2.We reboot the Bluetooth module using function api ble_cmd_system_reset();

    3. We connect to the device by its address with ble_cmd_gap_connect_direct()

    4. We request the list of services of the device and the ranges of values ​​in which the HANDLs of the characteristics in these services are located ble_cmd_attclient_read_by_group_type()

    5. After the event we ble_evt_attclient_procedure_completedrequest the list of characteristics with the help ble_cmd_attclient_find_information().

    6. In the event we ble_evt_attclient_find_information_foundremember HANDL'y measuring characteristics and Client Characteristic Configuration Descriptor (CCCD).

    7. In the event ble_evt_attclient_procedure_completedinclude alerts. In the previous paragraph, we learned the HANDLE of the CCCD descriptor and can read or write it. To enable alerts, you need to set the "notifications enabled" flag in the CCCD descriptor using the functionble_cmd_attclient_attribute_write(). After disconnecting from the device, the value of this descriptor will be reset.

    8. Now, when changing the values ​​of the device characteristics, an event will be triggered ble_evt_attclient_attribute_value(const struct ble_msg_attclient_attribute_value_evt_t *msg). It is possible to distinguish one characteristic from another by parameter: We define the structure of the value of the measuring characteristic:
    msg->atthandle.



    typedefstruct __attribute__((__packed__)){
    uint8 status_flags;
    float dose; //накопленная доза с момента включенияfloat doserate_search; //мощность дозы в режиме поиска
    uint16 pulses_last2sec; //кол-во импульсов со счетчика Гейгера за последние 2 секунды
    uint8 battery; //заряд батареи в процентах от 0 до 100
    uint8 temperature; //температура в градусах
    } atomtag_measurement_t;
    atomtag_measurement_t measurement_char;

    Since the byte order in the value of the measuring characteristic is little endian, it is enough to copy all these bytes into the packed structure:

    memcpy((uint8 *)&measurement_char, msg->value.data, msg->value.len);

    The dose rate will be calculated on a time interval of 6 minutes. We will send testimony to the server narodmon.ru no more than once every 6 minutes. In addition to the dose rate, we will send the battery charge and statistical error. All calculations take place in the callback ble_evt_attclient_attribute_value()in the file. main.c.

    Indications are sent to port 8283 narodmon.ru using the tcp protocol. The response from the server is not verified. Text protocol:

    #00:00:00:00:00:00\n#R1#10.5#err = 10%, batt = 100%\n##

    First comes the MAC address of the device (6 bytes). The source MAC address is hammered for example, do not forget to change it! Further, where 10.5 is the dose rate in μR / h, err is the statistical error.
    Compiled it all with gcc:

    gcc -std=gnu99 -lm  main.c cmd_def.c commands.c uart.c web.c -o narodmon-bin

    In order for the program to start automatically after the OS is loaded, I added the following lines before exit 0 to the rc.local file:

    cd /
    ./home/pi/narodmon/narodmon-bin /dev/ttyACM0 5c:31:3e:da:e8:9c

    5c: 31: 3e: da: e8: 9c - the address of the Bluetooth device, which you can find out if you run this program with the scan parameter:

    ./narodmon-bin /dev/ttyACM0 scan

    results


    After a couple of days of operation of the device, interesting things appeared on the graphs. Here you can see how the dose rate changed during the snowfall. November 11 at ~ 17: 00–18: 00 o'clock the freezing rain stopped and it began to snow. As the thickness of the snow cover increases, the average dose rate decreases. The dosimeter is installed at a height of 2 meters from the ground.

    The reduction in dose rate is due to the fact that the snow layer partially shields the natural gamma radiation at the surface of the earth. Also, the snow layer blocks the access of radon gas to the surface, the decay products of which can be detected with a conventional dosimeter.

    Now we can estimate the thickness of the snow :) In those days, about 8-10 centimeters of snow fell. In the world, aerogamming is practiced to estimate the thickness of snow in areas where there are hydroelectric power plants, in order to understand what kind of flooding to expect in spring. Only there are used gamma spectrometers with scintillation detectors because of their greater sensitivity.

    A similar picture is observed on the other dosimeters of the service, national monitoring, although not all of them give data so often and it is not clear which dose rate calculation algorithms are used there.

    The program can be improved by adding a buffer to which dosimeter readings will be saved during moments when there is no internet connection. The People’s Monitoring API allows you to send readings “in hindsight”. It is also worth connecting raspberry pi to a bespereboynik. Despite the simplicity of the design managed to get uptime about thirty days. A dosimeter on a national monitoring map can be found here. Please do not kick - I'm new to linux.




    Links


    1. AtomTag dosimeter
    2. Bluegiga BLED112 SDK and documentation
    3. People’s monitoring service
    4. Bluetooth LE, specifications
    5. Source code
    for Raspberry PI

    6. Android application for the People’s Monitoring service

    Also popular now: