Skip to content

Commit

Permalink
support BLE thermometer & humidiy sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
vitotai committed Sep 16, 2024
1 parent df230da commit 394abfd
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 108 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ lib_deps_external_esp32 = ${common_env_data.lib_deps_external}
esp8266_framework = [email protected]
esp8266_lib = ./lib-esp8266

esp32_framework = espressif32@~6.3.2
esp32_framework = espressif32@~6.8.1
esp32_lib = ./lib-esp32

[env:esp32-dev]
Expand Down
4 changes: 2 additions & 2 deletions src/BPLSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ void BPLSettings::defaultSystemConfiguration(void){
stringNcopy(syscfg->hostnetworkname,DEFAULT_HOSTNAME,32);
stringNcopy(syscfg->username,DEFAULT_USERNAME,32);
stringNcopy(syscfg->password,DEFAULT_PASSWORD,32);

syscfg->port = 80;
syscfg->port = 80;
syscfg->passwordLcd = false;
syscfg->wifiMode = WIFI_AP_STA;
syscfg->backlite = 0;
Expand Down
58 changes: 0 additions & 58 deletions src/BleBTHomeListener.h

This file was deleted.

9 changes: 6 additions & 3 deletions src/BleListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

BleScanner bleScanner;


void BleScanner::onResult(NimBLEAdvertisedDevice* advertisedDevice) {

//DBG_PRINTF("***OnResult:%s\n",advertisedDevice->getAddress().toString().c_str());
Expand Down Expand Up @@ -34,14 +35,15 @@ void BleScanner::onResult(NimBLEAdvertisedDevice* advertisedDevice) {
NimBLEAddress address = advertisedDevice->getAddress();

// service data
NimBLEUUID BTHomeServiceUUID((uint16_t)0xFCD2);
std::string strSvrData=advertisedDevice->getServiceData(BTHomeServiceUUID);
for(int si=0;si<advertisedDevice->getServiceDataCount();si++){
NimBLEUUID uuid =advertisedDevice->getServiceDataUUID(si);
std::string strSvrData=advertisedDevice->getServiceData(uuid);
if(strSvrData.length()>0){

std::string devName= advertisedDevice->getName();
DBG_PRINTF(" Dev: %s, %d ",devName.empty()? devName.c_str():"unknown",advertisedDevice->getRSSI());
DBG_PRINTF("\t %s ",address.toString().c_str());
DBG_PRINTF("Service data count:%d ",advertisedDevice->getServiceDataCount());
DBG_PRINTF(" UUID:%s ",uuid.toString().c_str());
for(int si=0;si<advertisedDevice->getServiceDataCount();si++){
NimBLEUUID suuid=advertisedDevice->getServiceDataUUID(si);
DBG_PRINTF("\n\t %d: UUID:%s :\n",si,suuid.toString().c_str());
Expand All @@ -56,6 +58,7 @@ void BleScanner::onResult(NimBLEAdvertisedDevice* advertisedDevice) {
DBG_PRINTF("\n");
}
}
}
#endif

if(_bleDeviceListeners.size()){
Expand Down
133 changes: 101 additions & 32 deletions src/BleBTHomeListener.cpp → src/BleSensorListener.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#if SupportBTHomeSensor

#include "BleBTHomeListener.h"
#include "BleSensorListener.h"
#include <string>
#include "TimeKeeper.h"
#include "OneWireTempSensor.h"
Expand All @@ -12,6 +12,7 @@
*/

static const NimBLEUUID BTHomeServiceUUID((uint16_t)0xFCD2);
static const NimBLEUUID ATCServiceUUID((uint16_t)0x181A);

typedef struct _BTHomeDataObject{
uint8_t objectId;
Expand Down Expand Up @@ -99,16 +100,14 @@ static const BTHomeDataObject BTHomeDataObjectMap[]={
{0x53, 255} //"text","string",
};


void BTHomeEnvironmentSensor::begin(void){
void BleSensorListener::begin(void){
startListen();
}

void BTHomeEnvironmentSensor::stop(void){
void BleSensorListener::stop(void){
stopListen();
}

bool BTHomeEnvironmentSensor::onDeviceFound(NimBLEAdvertisedDevice* device){
bool BleSensorListener::onDeviceFound(NimBLEAdvertisedDevice* device){
//device->isAdvertisingService() doesn't work?
const uint8_t *amac=device->getAddress().getNative();
if(memcmp(_macAddress,amac,6) ==0 ){
Expand Down Expand Up @@ -182,37 +181,83 @@ static bool parseBTHomeSensorData(NimBLEAdvertisedDevice* device, float& tempera
return gotData;
}

bool BTHomeEnvironmentSensor::_getData(NimBLEAdvertisedDevice* device){
//ATC1441 :length 13
// 00 00 00 00 00 00 TT TT HH BB VV VV C
// 0 6 7 8 9 10 11 12
// MAC(6) Temp Hum Bat Volt Count
// Temp * 0.1, volt in mV
// PvvX Custom: length 15
// 00 00 00 00 00 00 TT TT HH HH VV VV LL C F
// 0 6 7 8 9 10 11 12 13 14
// MAC(6) Temp Humidity Volt Levl Count Flgs
// Temp * 0.01 humidity * 0.01% mV, level: 0-100%,
// flags:
// bit 0: read swith
// 1: GPIO_TR
// 2: GPIO_TRG
// 3: Temp trigger event
// 4: Humidity trigger event

#define ATC_TEMP_POS 6
#define ATC_HUM_POS 8



static bool parseAtcData(NimBLEAdvertisedDevice* device, float& temperature,uint8_t& humidity,BleSensorType& type){
std::string strSvrData=device->getServiceData(ATCServiceUUID);
uint8_t data[16];

if(strSvrData.length()==13){
strSvrData.copy((char *)data,13, 0);
// ATC
type = BleSensorTypeAtc;
temperature = ((data[ATC_TEMP_POS] << 8) | data[ATC_TEMP_POS +1]) * 0.1;
humidity = data[ATC_HUM_POS];
DBG_PRINTF("ATC temp: %d, humidity:%d\n",(int16_t)(temperature*100),humidity);
return true;
}

if(strSvrData.length()==15){
// pvvx
type = BleSensorTypePvvx;
strSvrData.copy((char *)data,15, 0);
temperature = ((data[ATC_TEMP_POS+1] << 8) | data[ATC_TEMP_POS ]) * 0.01;
humidity = (uint8_t)(((data[ATC_HUM_POS +1] << 8) | data[ATC_HUM_POS]) * 0.01);
DBG_PRINTF("Pvvx temp: %d, humidity:%d\n",(int16_t)(temperature*100),humidity);

return true;
}
return false;
}

bool BleSensorListener::_getData(NimBLEAdvertisedDevice* device){
// haveServieceData() doesn't work as expected
// copy to "data" doesn't include length information?

BleSensorType type;
if(parseBTHomeSensorData(device,_temperature,_humidity)){
DBG_PRINTF("\t humidity:%d, temperature:%d, seen before %lu\n", _humidity,(int)(_temperature*100), (millis()-_lastUpdate)/1000);
_lastUpdate = millis();
return true;
}else if(parseAtcData(device,_temperature,_humidity,type)){
DBG_PRINTF("\t humidity:%d, temperature:%d, seen before %lu\n", _humidity,(int)(_temperature*100), (millis()-_lastUpdate)/1000);
_lastUpdate = millis();
return true;
}
return false;
}


bool BTHomeEnvironmentSensor::isConnected(){
bool BleSensorListener::isConnected(){
if(millis() - _lastUpdate > MaximumReportPeriod) return false;
return true;
}

unsigned char BTHomeEnvironmentSensor::humidity(){
if(!isConnected()) return 0xFF;
return (unsigned char)(_humidity + _hCal);
}

float BTHomeEnvironmentSensor::readTemperature(){
if(isConnected()) return _temperature;
return -1000.0;
}
bool BTHomeEnvironmentSensor::sameDevice(const uint8_t mac[6]){
bool BleSensorListener::sameDevice(const uint8_t mac[6]){
return memcmp(mac,_macAddress,6) ==0;
}

int BTHomeEnvironmentSensor::scanForDevice(BTHomeDevicdFoundFunc foundCb){
int BleSensorListener::scanForDevice(BTHomeDevicdFoundFunc foundCb){

BLEScanResults result=bleScanner.scan(ScanDeviceTime);
int found=0;
Expand All @@ -222,64 +267,88 @@ int BTHomeEnvironmentSensor::scanForDevice(BTHomeDevicdFoundFunc foundCb){

temp=-1000;
humidity=0xFF;

BleSensorType type;
if(parseBTHomeSensorData(*it,temp,humidity)){
found++;
foundCb(((NimBLEAdvertisedDevice*)(*it))->getAddress().getNative(),temp,humidity);
foundCb(BleSensorTypeBTHome,((NimBLEAdvertisedDevice*)(*it))->getAddress().getNative(),temp,humidity);
}else if(parseAtcData(*it,temp,humidity,type)){
found++;
foundCb(type,((NimBLEAdvertisedDevice*)(*it))->getAddress().getNative(),temp,humidity);
}
}
DBG_PRINTF("BTHome device found:%d\n",found);
return found;
}
std::list<BTHomeEnvironmentSensor*> BTHomeEnvironmentSensor::allSensors;
std::list<BleSensorListener*> BleSensorListener::allSensors;

BTHomeEnvironmentSensor* BTHomeEnvironmentSensor::findBTHomeSensor(const uint8_t mac[6]){
BleSensorListener* BleSensorListener::findBleSensor(const uint8_t mac[6]){
for (auto ptr : allSensors) {
if(ptr->sameDevice(mac)){
return ptr;
}
}
return NULL;
}
BTHomeEnvironmentSensor* BTHomeEnvironmentSensor::getBTHomeSensor(const uint8_t mac[6]){
BTHomeEnvironmentSensor* bt=BTHomeEnvironmentSensor::findBTHomeSensor(mac);
BleSensorListener* BleSensorListener::getBleSensor(const uint8_t mac[6],uint8_t format){
BleSensorListener* bt=BleSensorListener::findBleSensor(mac);
if(bt != NULL){
bt->_count ++;
return bt;
}
// not found create a new one
bt=new BTHomeEnvironmentSensor(mac);
bt=new BleSensorListener(mac,format);
bt->_count =1;
bt->begin();
allSensors.push_front(bt);
return bt;
}

void BTHomeEnvironmentSensor::releaseSensor(BTHomeEnvironmentSensor* sensor){
void BleSensorListener::releaseSensor(BleSensorListener* sensor){
sensor->_count --;
if(sensor->_count ==0){
sensor->stop();
allSensors.remove(sensor);
delete sensor;
}
}
// BleHumiditySensor
BleHumiditySensor::BleHumiditySensor(const uint8_t mac[6],uint8_t format){
_listener = BleSensorListener::getBleSensor(mac,format);
}

BleHumiditySensor::~BleHumiditySensor(void){
BleSensorListener::releaseSensor(_listener);
}
unsigned char BleHumiditySensor::humidity(){
if(! _listener->isConnected()) return 0xFF;
return _listener->humidity() + _hCal;
}
// BleThermometer
BleThermometer::BleThermometer(const uint8_t mac[6],uint8_t format){
_listener = BleSensorListener::getBleSensor(mac,format);
}

bool BTHomeEnvironmentSensor::init(){
BleThermometer::~BleThermometer(void){
BleSensorListener::releaseSensor(_listener);
}
bool BleThermometer::init(){
return isConnected();
}

temperature BTHomeEnvironmentSensor::read(){
temperature BleThermometer::read(){
if (!isConnected()){
return TEMP_SENSOR_DISCONNECTED;
}
if(_temperature<-99.0) {
float t = _listener->temperature();
if(t <-99.0) {
return TEMP_SENSOR_DISCONNECTED;
}
temperature temp = _tempOffset + doubleToTemp((double)_temperature);
temperature temp = _tempOffset + doubleToTemp((double)t);
return temp;

}

void BTHomeEnvironmentSensor::setTemperatureCalibration(fixed4_4 cal){
void BleThermometer::setTemperatureCalibration(fixed4_4 cal){
const uint8_t shift = TEMP_FIXED_POINT_BITS-ONEWIRE_TEMP_SENSOR_PRECISION; // difference in precision between DS18B20 format and temperature adt
//temperature i fixed7_9, calibration fixed4_4
_tempOffset =constrainTemp16(temperature(cal)<<shift);
Expand Down
Loading

0 comments on commit 394abfd

Please sign in to comment.