BresserWeatherSensorLW
Bresser 868 MHz Weather Sensor Radio Receiver based on ESP32/RP2040 and SX1262/SX1276/LR1121 - sends data to a LoRaWAN Network
Loading...
Searching...
No Matches
BleSensors.h
Go to the documentation of this file.
1
2// BleSensors.h
3//
4// Wrapper class for Theeengs Decoder (https://github.com/theengs/decoder)
5//
6// Intended for compatibility to the ATC_MiThermometer library
7// (https://github.com/matthias-bs/ATC_MiThermometer)
8//
9// created: 02/2023
10//
11//
12// MIT License
13//
14// Copyright (c) 2025 Matthias Prinke
15//
16// Permission is hereby granted, free of charge, to any person obtaining a copy
17// of this software and associated documentation files (the "Software"), to deal
18// in the Software without restriction, including without limitation the rights
19// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20// copies of the Software, and to permit persons to whom the Software is
21// furnished to do so, subject to the following conditions:
22//
23// The above copyright notice and this permission notice shall be included in all
24// copies or substantial portions of the Software.
25//
26// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32// SOFTWARE.
33//
34//
35// History:
36//
37// 20230211 Created
38// 20240417 Added additional constructor and method setAddresses()
39// 20240427 Added parameter activeScan to getData()
40// 20250121 Updated for NimBLE-Arduino v2.x
41// 20250808 Added specific logging macros in scan callback to avoid WDT reset
42// 20250926 Changed getData() to return number of known sensors found
43// 20251014 Added optional callback to abort scanning early
44//
45// ToDo:
46// -
47//
49
53
54#if !defined(BLE_SENSORS) && !defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2) \
55 && !defined(ARDUINO_ARCH_RP2040)
56#define BLE_SENSORS
57
58#include <Arduino.h>
59#include <NimBLEDevice.h>
60#include <decoder.h>
61
62// Extensive logging in the callback may lead to a watchdog reset
63// see
64// https://github.com/h2zero/NimBLE-Arduino/issues/329
65// https://github.com/h2zero/NimBLE-Arduino/issues/351
66
67//#define CB_LOGGING
68#if defined(CB_LOGGING)
69#define cb_log_i log_i
70#define cb_log_w log_w
71#define cb_log_d log_d
72#define cb_log_e log_e
73#define cb_log_v log_v
74#else
75#define cb_log_i {}
76#define cb_log_w {}
77#define cb_log_d {}
78#define cb_log_e {}
79#define cb_log_v {}
80#endif
81
85struct BleDataS {
86 bool valid;
88 float humidity;
89 uint8_t batt_level;
90 int rssi;
91};
92
93typedef struct BleDataS ble_sensors_t;
94
95
100 public:
106 BleSensors(std::vector<std::string> known_sensors, bool (*stopScanCb)() = nullptr) {
107 _known_sensors = known_sensors;
108 data.resize(known_sensors.size());
109 _stopScanCb = stopScanCb;
110 };
111
115 BleSensors(bool (*stopScanCb)() = nullptr) {
116 data.resize(0);
117 _stopScanCb = stopScanCb;
118 };
119
125 void setAddresses(std::vector<std::string> known_sensors) {
126 _known_sensors = known_sensors;
127 data.resize(known_sensors.size());
128 };
129
133 void begin(void) {
134 };
135
139 void clearScanResults(void);
140
149 unsigned getData(uint32_t duration, bool activeScan = true);
150
154 void resetData(void);
155
159 std::vector<ble_sensors_t> data;
160
161 protected:
162 std::vector<std::string> _known_sensors;
163 NimBLEScan* _pBLEScan;
164 bool (*_stopScanCb)();
165};
166#endif
struct BleDataS ble_sensors_t
Shortcut for struct BleDataS.
Definition BleSensors.h:93
void clearScanResults(void)
Delete results from BLEScan buffer to release memory.
Definition BleSensors.cpp:167
BleSensors(std::vector< std::string > known_sensors, bool(*stopScanCb)()=nullptr)
Constructor.
Definition BleSensors.h:106
std::vector< ble_sensors_t > data
Sensor data.
Definition BleSensors.h:159
void setAddresses(std::vector< std::string > known_sensors)
Set BLE MAC addresses of known sensors.
Definition BleSensors.h:125
void resetData(void)
Set sensor data invalid.
Definition BleSensors.cpp:173
BleSensors(bool(*stopScanCb)()=nullptr)
Constructor.
Definition BleSensors.h:115
void begin(void)
Initialization.
Definition BleSensors.h:133
unsigned getData(uint32_t duration, bool activeScan=true)
Get data from sensors by running a BLE scan.
Definition BleSensors.cpp:191
BLE sensor data.
Definition BleSensors.h:85
int rssi
RSSI in dBm.
Definition BleSensors.h:90
float temperature
temperature in degC
Definition BleSensors.h:87
bool valid
data valid
Definition BleSensors.h:86
uint8_t batt_level
battery level in %
Definition BleSensors.h:89
float humidity
humidity in %
Definition BleSensors.h:88