Working across ESP32 S3, ESP32 C3 and nRF52
This commit is contained in:
@@ -160,7 +160,7 @@ void setup() {
|
|||||||
while (1) delay(1000);
|
while (1) delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
victron.setDebug(false);
|
victron.setDebug(true);
|
||||||
victron.setCallback(onVictronData);
|
victron.setCallback(onVictronData);
|
||||||
|
|
||||||
// Replace with your own devices (MAC + 32-char hex key from VictronConnect)
|
// Replace with your own devices (MAC + 32-char hex key from VictronConnect)
|
||||||
|
|||||||
@@ -53,6 +53,15 @@ void VictronBLEAdvertisedDeviceCallbacks::onResult(BLEAdvertisedDevice advertise
|
|||||||
}
|
}
|
||||||
|
|
||||||
void VictronBLE::processDevice(BLEAdvertisedDevice& advertisedDevice) {
|
void VictronBLE::processDevice(BLEAdvertisedDevice& advertisedDevice) {
|
||||||
|
// Debug: print every BLE device seen (before any filtering)
|
||||||
|
if (debugEnabled) {
|
||||||
|
Serial.printf("[VictronBLE] MAC=%-17s RSSI=%-4d Name=%-20s ManData=%s\n",
|
||||||
|
advertisedDevice.getAddress().toString().c_str(),
|
||||||
|
advertisedDevice.getRSSI(),
|
||||||
|
advertisedDevice.haveName() ? advertisedDevice.getName().c_str() : "(none)",
|
||||||
|
advertisedDevice.haveManufacturerData() ? "yes" : "no");
|
||||||
|
}
|
||||||
|
|
||||||
if (!advertisedDevice.haveManufacturerData()) return;
|
if (!advertisedDevice.haveManufacturerData()) return;
|
||||||
|
|
||||||
// getManufacturerData() returns std::string on older ESP32 BLE libraries and
|
// getManufacturerData() returns std::string on older ESP32 BLE libraries and
|
||||||
|
|||||||
@@ -43,18 +43,39 @@ void VictronBLE::loop() {
|
|||||||
|
|
||||||
void VictronBLE::scanCallback(ble_gap_evt_adv_report_t* report) {
|
void VictronBLE::scanCallback(ble_gap_evt_adv_report_t* report) {
|
||||||
if (s_instance) {
|
if (s_instance) {
|
||||||
|
// Format MAC (little-endian to big-endian hex)
|
||||||
|
const uint8_t* a = report->peer_addr.addr;
|
||||||
|
char mac[18];
|
||||||
|
snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||||
|
a[5], a[4], a[3], a[2], a[1], a[0]);
|
||||||
|
|
||||||
|
// Debug: print every BLE device seen (before any filtering)
|
||||||
|
if (s_instance->debugEnabled) {
|
||||||
|
// Manufacturer specific data (AD type 0xFF) — includes the 0x02E1 vendor ID
|
||||||
|
uint8_t mfgBuf[31];
|
||||||
|
uint8_t mfgLen = Bluefruit.Scanner.parseReportByType(
|
||||||
|
report, BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA, mfgBuf, sizeof(mfgBuf));
|
||||||
|
|
||||||
|
// Try to get device name
|
||||||
|
char nameBuf[32] = "(none)";
|
||||||
|
uint8_t nameLen = Bluefruit.Scanner.parseReportByType(
|
||||||
|
report, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, (uint8_t*)nameBuf, sizeof(nameBuf) - 1);
|
||||||
|
if (nameLen == 0) {
|
||||||
|
nameLen = Bluefruit.Scanner.parseReportByType(
|
||||||
|
report, BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME, (uint8_t*)nameBuf, sizeof(nameBuf) - 1);
|
||||||
|
}
|
||||||
|
if (nameLen > 0) nameBuf[nameLen] = '\0';
|
||||||
|
|
||||||
|
Serial.printf("[VictronBLE] MAC=%-17s RSSI=%-4d Name=%-20s ManData=%s\n",
|
||||||
|
mac, report->rssi, nameBuf, mfgLen >= 2 ? "yes" : "no");
|
||||||
|
}
|
||||||
|
|
||||||
// Manufacturer specific data (AD type 0xFF) — includes the 0x02E1 vendor ID
|
// Manufacturer specific data (AD type 0xFF) — includes the 0x02E1 vendor ID
|
||||||
uint8_t buf[31];
|
uint8_t buf[31];
|
||||||
uint8_t len = Bluefruit.Scanner.parseReportByType(
|
uint8_t len = Bluefruit.Scanner.parseReportByType(
|
||||||
report, BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA, buf, sizeof(buf));
|
report, BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA, buf, sizeof(buf));
|
||||||
|
|
||||||
if (len >= 2) {
|
if (len >= 2) {
|
||||||
// peer_addr.addr is little-endian (LSB first); format big-endian to
|
|
||||||
// match the AA:BB:CC:DD:EE:FF MAC users copy from VictronConnect.
|
|
||||||
const uint8_t* a = report->peer_addr.addr;
|
|
||||||
char mac[18];
|
|
||||||
snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x",
|
|
||||||
a[5], a[4], a[3], a[2], a[1], a[0]);
|
|
||||||
s_instance->onAdvertisement(buf, len, mac, report->rssi);
|
s_instance->onAdvertisement(buf, len, mac, report->rssi);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user