-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from WLANThermo-nano/Meater2_and_TempSpike
Meater2 and temp spike
- Loading branch information
Showing
6 changed files
with
426 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/*************************************************** | ||
Copyright (C) 2024 Steffen Ochs | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
HISTORY: Please refer Github History | ||
****************************************************/ | ||
|
||
#include "BleSensorGrp.h" | ||
#include "BleTemperatureMeater2.h" | ||
#include <ArduinoLog.h> | ||
|
||
#define MEATER2_NUM_OF_TEMERATURES 6u | ||
|
||
BleTemperatureMeater2::BleTemperatureMeater2(ble_gap_addr_t *peerAddress) : BleSensorBase(peerAddress, MEATER2_NUM_OF_TEMERATURES, false) | ||
{ | ||
bleServ = new BLEClientService(BLEUuid(SERV_UUID_MEATER2)); | ||
bleChar = new BLEClientCharacteristic(BLEUuid(CHAR_UUID_MEATER2)); | ||
bleChar->setNotifyCallback(BleSensorGrp::notifyCb); | ||
|
||
bleServ->begin(); | ||
bleChar->begin(); | ||
|
||
Bluefruit.Central.connect(peerAddress); | ||
} | ||
|
||
void BleTemperatureMeater2::connect(uint16_t bleConnHdl) | ||
{ | ||
char buffer[30] = {0}; | ||
|
||
this->bleConnHdl = bleConnHdl; | ||
|
||
BLEConnection *bleConnection = Bluefruit.Connection(bleConnHdl); | ||
|
||
if (bleConnection != NULL) | ||
{ | ||
if (bleConnection->getPeerName(buffer, sizeof(buffer))) | ||
{ | ||
name = buffer; | ||
} | ||
} | ||
|
||
// disconnect if disabled | ||
if (false == enabled) | ||
{ | ||
Bluefruit.disconnect(bleConnHdl); | ||
return; | ||
} | ||
|
||
Log.notice("Discovering Meater2 service ... "); | ||
|
||
// Check for service | ||
if (!bleServ->discover(bleConnHdl)) | ||
{ | ||
Log.notice("failed!" CR); | ||
Bluefruit.disconnect(bleConnHdl); | ||
return; | ||
} | ||
|
||
Log.notice("success" CR); | ||
|
||
// Check characteristic | ||
Log.notice("Discovering Meater2 characteristic ... "); | ||
if (!bleChar->discover()) | ||
{ | ||
Log.notice("failed!" CR); | ||
Bluefruit.disconnect(bleConnHdl); | ||
return; | ||
} | ||
|
||
Log.notice("success" CR); | ||
|
||
// Enable notification | ||
Log.notice("Enabling Meater2 notification ... "); | ||
if (!bleChar->enableNotify()) | ||
{ | ||
Log.notice("failed!" CR); | ||
Bluefruit.disconnect(bleConnHdl); | ||
return; | ||
} | ||
|
||
Log.notice("success" CR); | ||
|
||
this->connected = true; | ||
} | ||
|
||
float BleTemperatureMeater2::readAmbientTemperature(uint8_t *data) | ||
{ | ||
uint16_t t = 0; | ||
float temp = 0; | ||
t = data[11] << 8; | ||
t += data[10]; | ||
temp = t / 32.0; | ||
if (temp > 2000) | ||
{ | ||
temp = temp - 2048; | ||
} | ||
return temp; | ||
} | ||
|
||
void BleTemperatureMeater2::notify(BLEClientCharacteristic *chr, uint8_t *data, uint16_t len) | ||
{ | ||
this->lastSeen = 0u; | ||
Log.notice("----------- Meater2 data -----------" CR); | ||
|
||
uint8_t probeId = 0; | ||
for (uint8_t i = 0u; i <= 8; i = i + 2) | ||
{ | ||
uint16_t t = 0; | ||
t = data[i + 1] << 8; | ||
t += data[i]; | ||
currentValue[probeId] = t / 32.0; | ||
if (currentValue[probeId] > 2000) | ||
{ | ||
currentValue[probeId] = currentValue[probeId] - 2048; | ||
} | ||
|
||
Log.notice("Probe %d has value %F" CR, probeId, currentValue[probeId]); | ||
probeId++; | ||
} | ||
|
||
currentValue[5] = readAmbientTemperature(data); | ||
Log.notice("Ambient has value %F" CR, currentValue[5]); | ||
|
||
Log.verbose("Raw data: "); | ||
|
||
for (uint8_t i = 0u; i < len; i++) | ||
Log.verbose("%x ", data[i]); | ||
|
||
Log.verbose(CR); | ||
} | ||
|
||
void BleTemperatureMeater2::disconnect(uint16_t conn_handle, uint8_t reason) | ||
{ | ||
this->bleConnHdl = INVALID_BLE_CONN_HANDLE; | ||
this->connected = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/*************************************************** | ||
Copyright (C) 2024 Steffen Ochs | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
HISTORY: Please refer Github History | ||
****************************************************/ | ||
#pragma once | ||
|
||
#include "BleSensorBase.h" | ||
|
||
const uint8_t SERV_UUID_MEATER2[16] = {0x8b, 0xcf, 0x55, 0x45, 0xe5, 0xe1, 0xDD, 0xa0, 0x54, 0x4e, 0xf1, 0x59, 0x6c, 0x74, 0xe2, 0xc9}; | ||
const uint8_t CHAR_UUID_MEATER2[16] = {0x76, 0x28, 0x1a, 0x99, 0xd1, 0x45, 0x9b, 0x90, 0xbf, 0x4b, 0x5e, 0x04, 0x74, 0xa7, 0xdd, 0x7e}; | ||
|
||
class BleTemperatureMeater2 : public BleSensorBase | ||
{ | ||
public: | ||
BleTemperatureMeater2(ble_gap_addr_t *peerAddress); | ||
void virtual connect(uint16_t bleConnHdl); | ||
void notify(BLEClientCharacteristic *chr, uint8_t *data, uint16_t len); | ||
void disconnect(uint16_t conn_handle, uint8_t reason); | ||
|
||
private: | ||
float readAmbientTemperature(uint8_t *data); | ||
BLEClientService *bleServ; | ||
BLEClientCharacteristic *bleChar; | ||
}; |
Oops, something went wrong.