Skip to content

Instantly share code, notes, and snippets.

@bmidgley
Last active January 15, 2024 19:17
Show Gist options
  • Save bmidgley/daf193284f9d6e4ebe31f0375f59ef58 to your computer and use it in GitHub Desktop.
Save bmidgley/daf193284f9d6e4ebe31f0375f59ef58 to your computer and use it in GitHub Desktop.
Scan and parse remoteid bt5 broadcasts
/*
Print and log csv-style lines with altitude in meters
id,lat,long,altitude,isotime
0DFFFAABB,41.118233,-112.045356,1632.5,2024-01-13T06:25:21Z
*/
#ifndef SOC_BLE_50_SUPPORTED
#warning "This SoC does not support BLE5. Try using ESP32-C3, or ESP32-S3"
#else
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <esp_wifi.h>
#include <esp_nan.h>
#include <time.h>
uint32_t scanTime = 100; //In 10ms (1000ms)
BLEScan* pBLEScan;
esp_bd_addr_t last_addr;
char iso[30];
int cs = 21;
int max_log = 256;
int getId(uint8_t *packet, int offset) {
return packet[offset] | packet[offset+1] << 8 | packet[offset+2] << 16 | packet[offset+3] << 24;
}
float getCoord(uint8_t *packet, int offset) {
int v = getId(packet, offset);
return v / 10000000.0;
}
float getAlt(uint8_t *packet, int offset) {
int v = packet[offset] | packet[offset+1] << 8;
return (v / 2.0) - 1000.0;
}
int getTime(uint8_t *packet, int offset) {
int v = getId(packet, offset);
if(v == 0) return 0;
return v + 1546300800;
}
void appendFile(fs::FS &fs, const char * path, const char * message){
File file = fs.open(path, FILE_APPEND);
if(!file){
Serial.println("Failed to open file for appending");
return;
}
if(!file.print(message)){
Serial.println("Append failed");
}
file.close();
}
class MyBLEExtAdvertisingCallbacks: public BLEExtAdvertisingCallbacks {
void onResult(esp_ble_gap_ext_adv_reprot_t report) {
char log_string[max_log];
if (report.event_type & ESP_BLE_GAP_SET_EXT_ADV_PROP_LEGACY) return;
digitalWrite(LED_BUILTIN, LOW);
delay(25);
digitalWrite(LED_BUILTIN, HIGH);
int id = getId(report.adv_data, 1);
if (id == 0) return;
time_t raw_time = getTime(report.adv_data, 88);
if (raw_time == 0) return;
struct tm ts = *localtime(&raw_time);
strftime(iso, sizeof(iso), "%Y-%m-%dT%H:%M:%SZ", &ts);
snprintf(log_string, sizeof(log_string), "%.8X,%f,%f,%.1f,%s\n", id, getCoord(report.adv_data, 64), getCoord(report.adv_data, 68), getAlt(report.adv_data, 74), iso);
appendFile(SD, "/drone.txt", log_string);
printf(log_string);
}
};
void bt_setup() {
Serial.println("Scanning bt...");
BLEDevice::init("");
pBLEScan = BLEDevice::getScan();
pBLEScan->setExtendedScanCallback(new MyBLEExtAdvertisingCallbacks());
pBLEScan->setExtScanParams();
delay(1000);
pBLEScan->startExtScan(scanTime, 3);
}
void wifi_beacon_setup() {
}
void wifi_nan_setup() {
esp_err_t status;
uint8_t handle;
esp_netif_init();
wifi_nan_config_t nan_cfg;
wifi_nan_subscribe_cfg_t subscribe_cfg;
Serial.println("Scanning nan...");
status = esp_wifi_nan_start(&nan_cfg);
handle = esp_wifi_nan_subscribe_service(&subscribe_cfg);
}
boolean sd_setup() {
if(!SD.begin(cs)){
Serial.println("Card Mount Failed");
return false;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD card attached");
return false;
}
return true;
}
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
bt_setup();
sd_setup();
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}
#endif // SOC_BLE_50_SUPPORTED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment