The program scans the BLE environment and monitors only the appearance of any devices (tags). If several tags are detected, the program selects the tag with the strongest RSSI signal level.
In the device configuration, on the Bluetooth tab, Bluetooth module operation must be enabled.
The program contains two main variables for monitoring tags: id and fixed_id.
id - the current nearest tag (the value may change dynamically depending on the data in the BLE environment)
fixed_id - the currently fixed tag (the tag that the device considers the primary one, stores, and transmits to the server)
There is also a global fix status. The status is set to true when a value is written to fixed_id (a tag is found) and is set to false if the tag in fixed_id is no longer the strongest one or is absent from the BLE environment for longer than the time specified by the LOSS_T variable.
The main conditions used to detect tag loss, detection, and replacement are described below.
_first_fix - triggered if (fixed_id == 0) && (id != 0).0 is transmitted to the server, and then the first tag appears.fix is set to true;id is written to fixed_id;id is written to CE User Parameter No. 1;rssi is written to CE User Parameter No. 2;41046 is generated._loss_id - triggered if (fixed_id != 0) && (id != fixed_id).fix is set to false;fixed_id is not changed;0 is written to CE User Parameter No. 1;-128 is written to CE User Parameter No. 2;41047 is generated._second_fix - triggered if (fixed_id != 0) && (id != 0) && (fix == false).fix is set to true;id is written to fixed_id;id is written to CE User Parameter No. 1;rssi is written to CE User Parameter No. 2;41046 is generated.![]() |
| Diagram. Part 1 |
At the very beginning of the program, in the "+" branch of the on_start condition, a one-time recalculation of one of the system variables is performed. This block could also be used for any one-time calculations or actions that need to be performed only after the program starts.
Next, in the scan ble block, current tag data is obtained. At this stage, the program receives information indicating that new data is available for processing.
Function block highlighted by the "OPTIONAL" frame:
The simplest identification method is by MAC address. No additional calculations are required for this. The MAC address is already available in the scan ble block.
However, if an ID needs to be extracted from the data transmitted by the sensor, this structure should be added.
In the check condition, the required ID is checked and obtained. If the ID is obtained, it is updated in the update id block. If the ID is not obtained, the new data flag previously set in the scan ble block is reset in the unset new_data block.
This approach is convenient when working with iBeacon.
![]() |
| Diagram. Part 2 |
The new_data condition allows a function block to be connected for processing new information from the tag.
The purpose of this function block is to select the tag with the strongest signal level.
In the compare id block, the program checks whether the received information comes from the current strongest tag. If the new data comes from the current strongest tag, the _update_rssi flag is set.
In the need init condition, the program checks whether this is the first pass through these functions after startup. If the program enters this block for the first time after startup, it proceeds directly to saving the current ID and signal level without further analysis.
In the update rssi condition, the state of the _update_rssi flag is checked. If it is active, the program proceeds directly to saving the current signal level without further analysis.
In the (rssi_tmp > rssi) condition, the signal strength of the new tag is compared with that of the current strongest tag. If the new tag has a stronger signal, the strongest tag is replaced. Otherwise, data analysis is stopped.
The set id action updates the tag ID, signal strength, and activity state.
Note the activity state: every time tag data is updated, the _id_inactive flag is reset.
![]() |
| Diagram. Part 3 |
The update activity timer condition periodically starts the tag activity check procedure.
The clean inactive id action performs the following:
_id_inactive is equal to True_id_inactive flagThe fix conditions block monitors the _first_fix, _loss_id, and _second_fix conditions. These conditions were described earlier in this article.
The fix timers block controls the delays for triggering the _first_fix, _loss_id, and _second_fix conditions. The variables used to configure these delays are described later in this article in the "Additional Information" section.
The on fix condition performs the following actions:
(all variables were described earlier in this article)
fix is set to true;id is written to fixed_id;id is written to CE User Parameter No. 1;rssi is written to CE User Parameter No. 2;41046 is generated.The on loss condition performs the following actions:
(all variables were described earlier in this article)
fix is set to false;fixed_id is not changed;0 is written to CE User Parameter No. 1;-128 is written to CE User Parameter No. 2;41047 is generated.![]() |
| Diagram. Part 4 |
In the update rssi condition, the state of the _update_rssi flag is checked. If it is active, the current tag signal strength is updated.
The set rssi param action updates the current signal strength in CE User Parameter No. 2.
Variables used for configuration:
FIRST_FIX_T - timeout for the _first_fix condition (first tag acquisition after startup).It is not recommended to set values lower than the default, as this increases the probability of fixing a tag that is not the nearest one.
SECOND_FIX_T - timeout for the _second_fix condition (identifier change).It is not recommended to set values lower than the default, as this is required for correct processing of identifier change events on the server. The device must sequentially send
0and then the new identifier so that the server can register the change.
LOSS_T - timeout for the _loss_id condition (identifier reset).It is not recommended to set a value lower than 30000 ms.
CE User Parameter No. 1 - the last 4 bytes of the MAC address of the fixed tag (or other contents of the fixed_id variable).
CE User Parameter No. 2 - RSSI signal level from the tag in dBm.
Always a negative value. The higher the value, the stronger the signal.
| Code | Description |
|---|---|
41046 |
New tag acquisition event |
41047 |
Tag loss event |