Skip to content

Commit

Permalink
Moved from AppLayer.h/.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-bs committed May 31, 2024
1 parent e63996f commit f51c994
Show file tree
Hide file tree
Showing 2 changed files with 319 additions and 0 deletions.
179 changes: 179 additions & 0 deletions src/PayloadBLE.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
///////////////////////////////////////////////////////////////////////////////
// PayloadBLE.cpp
//
// Get BLE temperature/humidity sensor values and encode as LoRaWAN payload
//
// created: 05/2024
//
//
// MIT License
//
// Copyright (c) 2024 Matthias Prinke
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//
// History:
//
// 20240531 Moved from AppLayer.cpp
//
// ToDo:
// -
//
///////////////////////////////////////////////////////////////////////////////

#include "PayloadBLE.h"

#if defined(MITHERMOMETER_EN) || defined(THEENGSDECODER_EN)

void PayloadBLE::setBleAddr(uint8_t *bytes, uint8_t size)
{
appPrefs.begin("BWS-LW-APP", false);
appPrefs.putBytes("ble", bytes, size);
appPrefs.end();
}

uint8_t PayloadBLE::getBleAddr(uint8_t *payload)
{
appPrefs.begin("BWS-LW-APP", false);
uint8_t size = appPrefs.getBytesLength("ble");
appPrefs.getBytes("ble", payload, size);
appPrefs.end();

return size;
}

std::vector<std::string> PayloadBLE::getBleAddr(void)
{
std::vector<std::string> bleAddr;

appPrefs.begin("BWS-LW-APP", false);
uint8_t size = appPrefs.getBytesLength("ble");
uint8_t addrBytes[48];
appPrefs.getBytes("ble", addrBytes, size);
appPrefs.end();

if (size < 6)
{
// return empty list
return bleAddr;
}

uint8_t check = 0;
for (size_t i = 0; i < 6; i++)
{
check |= addrBytes[i];
}
if (check == 0)
{
// First address is 00:00:00:00:00:00, return empty list
return bleAddr;
}

for (size_t i = 0; i < size; i += 6)
{
char addr[18];
snprintf(addr, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
addrBytes[i], addrBytes[i + 1], addrBytes[i + 2], addrBytes[i + 3], addrBytes[i + 4], addrBytes[i + 5]);
bleAddr.push_back(addr);
}

return bleAddr;
}


/*
* Initialize list of known BLE addresses from defaults or Preferences
*/
void PayloadBLE::bleAddrInit(void)
{
knownBLEAddressesDef = KNOWN_BLE_ADDRESSES;
knownBLEAddresses = getBleAddr();
if (knownBLEAddresses.size() == 0)
{
// No addresses stored in Preferences, use default
knownBLEAddresses = knownBLEAddressesDef;
log_d("Using BLE addresses from BresserWeatherSensorLWCfg.h:");
}
else
{
log_d("Using BLE addresses from Preferences:");
}
bleSensors = BleSensors(knownBLEAddresses);

for (const std::string &s : knownBLEAddresses)
{
(void)s;
log_d("%s", s.c_str());
}
};

/*
* Encode BLE temperature/humidity sensor values for LoRaWAN transmission
*/
void PayloadBLE::encodeBLE(uint8_t *appPayloadCfg, LoraEncoder &encoder)
{
#if defined(MITHERMOMETER_EN) || defined(THEENGSDECODER_EN)
float indoor_temp_c;
float indoor_humidity;

// Set sensor data invalid
bleSensors.resetData();

appPrefs.begin("BWS-LW-APP", false);
uint8_t ble_active = appPrefs.getUChar("ble_active", BLE_SCAN_MODE);
uint8_t ble_scantime = appPrefs.getUChar("ble_scantime", BLE_SCAN_TIME);
log_d("Preferences: ble_active: %u", ble_active);
log_d("Preferences: ble_scantime: %u s", ble_scantime);
appPrefs.end();
// Get sensor data - run BLE scan for <bleScanTime>
bleSensors.getData(ble_scantime, ble_active);
#endif

// BLE Temperature/Humidity Sensors
#if defined(MITHERMOMETER_EN)
float div = 100.0;
#elif defined(THEENGSDECODER_EN)
float div = 1.0;
#endif

if (encoder.getLength() <= PAYLOAD_SIZE - 3)
{
if (bleSensors.data[0].valid)
{
indoor_temp_c = bleSensors.data[0].temperature / div;
indoor_humidity = bleSensors.data[0].humidity / div;
log_i("Indoor Air Temp.: % 3.1f °C", bleSensors.data[0].temperature / div);
log_i("Indoor Humidity: %3.1f %%", bleSensors.data[0].humidity / div);
encoder.writeTemperature(indoor_temp_c);
encoder.writeUint8(static_cast<uint8_t>(indoor_humidity + 0.5));
}
else
{
log_i("Indoor Air Temp.: --.- °C");
log_i("Indoor Humidity: -- %%");
encoder.writeTemperature(INV_TEMP);
encoder.writeUint8(INV_UINT8);
}
// BLE Temperature/Humidity Sensors: delete results fromBLEScan buffer to release memory
bleSensors.clearScanResults();
}
}

#endif
140 changes: 140 additions & 0 deletions src/PayloadBLE.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
///////////////////////////////////////////////////////////////////////////////
// PayloadBLE.h
//
// Get BLE temperature/humidity sensor values and encode as LoRaWAN payload
//
// created: 05/2024
//
//
// MIT License
//
// Copyright (c) 2024 Matthias Prinke
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//
// History:
//
// 20240531 Moved from AppLayer.h
//
// ToDo:
// -
//
///////////////////////////////////////////////////////////////////////////////

#if !defined(_PAYLOAD_BLE)
#define _PAYLOAD_BLE

#include "../BresserWeatherSensorLWCfg.h"

#if defined(MITHERMOMETER_EN) || defined(THEENGSDECODER_EN)

#include <Preferences.h>

#if defined(MITHERMOMETER_EN)
// BLE Temperature/Humidity Sensor
#include <ATC_MiThermometer.h>
#endif
#if defined(THEENGSDECODER_EN)
#include "BleSensors/BleSensors.h"
#endif

#include <LoraMessage.h>
#include "logging.h"

class PayloadBLE
{
private:
/// Preferences (stored in flash memory)
Preferences appPrefs;

/// Default BLE MAC addresses
std::vector<std::string> knownBLEAddressesDef;
/// Actual BLE MAC addresses; either from Preferences or from defaults
std::vector<std::string> knownBLEAddresses;

#ifdef MITHERMOMETER_EN
/// BLE Temperature/Humidity Sensors
ATC_MiThermometer bleSensors; //!< Mijia Bluetooth Low Energy Thermo-/Hygrometer
#endif
#ifdef THEENGSDECODER_EN
/// Bluetooth Low Energy sensors
BleSensors bleSensors;
#endif

public:
/*!
* \brief Constructor
*/
PayloadBLE(){};

/*!
* \brief BLE startup code
*/
void begin(void)
{
bleAddrInit();
};

/*!
* Set BLE addresses in Preferences and bleSensors object
*
* \param bytes MAC addresses (6 bytes per address)
* \param size size in bytes
*/
void setBleAddr(uint8_t *bytes, uint8_t size);

/*!
* Get BLE addresses from Preferences
*
* \param bytes buffer for addresses
*
* \returns number of bytes copied into buffer
*/
uint8_t getBleAddr(uint8_t *bytes);

/*!
* Get BLE addresses from Preferences
*
* \returns BLE addresses
*/
std::vector<std::string> getBleAddr(void);

/*!
* \brief Initialize list of known BLE addresses from defaults or Preferences
*
* If available, addresses from Preferences are used, otherwise defaults from
* BresserWeatherSensorLWCfg.h.
*
* BleSensors() requires Preferences, which uses the Flash FS,
* which is not available before the sketches' begin() is called -
* thus the following cannot be handled by the constructor!
*/
void bleAddrInit(void);

/*!
* \brief Encode BLE temperature/humidity sensor values for LoRaWAN transmission
*
* \param appPayloadCfg LoRaWAN payload configuration bitmaps
* \param encoder LoRaWAN payload encoder object
*/
void encodeBLE(uint8_t *appPayloadCfg, LoraEncoder &encoder);
};
#endif // defined(MITHERMOMETER_EN) || defined(THEENGSDECODER_EN)
#endif //_PAYLOAD_BLE

0 comments on commit f51c994

Please sign in to comment.