📜 ⬆️ ⬇️

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 is a Bluetooth Low Energy dosimeter for a smartphone and tablet with a Geiger SBM-20 counter. 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:
')


A service is a container that contains several attributes called characteristics. All services have unique identifiers UUID and HANDLE. For example, the dosimeter has 2 services:

1. Service for the user, which contains characteristics for reading measurement results and writing user settings (audio alarm settings).

2. A 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 This module has quite a lot of commands and 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, the stub implementations are declared 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's analyze the connection and data exchange algorithm with the dosimeter:

1. Opens port /dev/ttyACM0

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

3. Connect to the device by its address using ble_cmd_gap_connect_direct()

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

5. After the ble_evt_attclient_procedure_completed event, ble_evt_attclient_procedure_completed request the feature list with ble_cmd_attclient_find_information().

6. In the ble_evt_attclient_find_information_found event, ble_evt_attclient_find_information_found remember the HANDLs of the measurement characteristic and the Client Characteristic Configuration Descriptor (CCCD).

7. In the ble_evt_attclient_procedure_completed event, ble_evt_attclient_procedure_completed 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 ble_cmd_attclient_attribute_write() function. After disconnecting from the device, the value of this descriptor will be reset.

8. Now, when changing the values ​​of the device characteristics, the event ble_evt_attclient_attribute_value(const struct ble_msg_attclient_attribute_value_evt_t *msg) will be ble_evt_attclient_attribute_value(const struct ble_msg_attclient_attribute_value_evt_t *msg) . You can distinguish one characteristic from another by parameter:
msg->atthandle.

Define the structure of the values ​​of the measuring characteristics:

 typedef struct __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 order of bytes 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 in the time interval of 6 minutes. We will not 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 ble_evt_attclient_attribute_value() main.c. in main.c.

The readings are sent to port 8283 narodmon.ru via the tcp protocol. The response from the server is not verified. The 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 all this 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 device operation, interesting things appeared on the charts. 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 assess the thickness of snow in areas where there are hydroelectric power plants, in order to understand what floods 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 the absence of an Internet connection. The People’s Monitoring API allows you to send readings “in hindsight”. It is also worth connecting a raspberry pi to a bespereboinik. 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. Service national monitoring
4. Bluetooth LE, specifications
5. Source Code
Raspberry PI software
6. Application for Android service "People's Monitoring"

Source: https://habr.com/ru/post/399839/


All Articles