This commit is contained in:
richonguzman 2023-08-05 10:20:55 -04:00
parent dfdcee932a
commit 3eef6ebfea
4 changed files with 125 additions and 1 deletions

View File

@ -71,7 +71,8 @@
"standingUpdateTime": 15,
"sendAltitude": true,
"sendBatteryInfo": false,
"bluetooth": true
"bluetooth": true,
"disableGPS": false
},
"ptt_trigger": {
"active": false,

View File

@ -6,6 +6,7 @@
#include <WiFi.h>
#include <LoRa.h>
#include <vector>
#include "bluetooth_utils.h"
#include "configuration.h"
#include "station_utils.h"
#include "button_utils.h"
@ -83,6 +84,7 @@ void setup() {
WiFi.mode(WIFI_OFF);
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "WiFi controller stopped");
BLUETOOTH_Utils::setup();
userButton.attachClick(BUTTON_Utils::singlePress);
userButton.attachLongPressStart(BUTTON_Utils::longPress);

105
src/bluetooth_utils.cpp Normal file
View File

@ -0,0 +1,105 @@
#include <esp_bt.h>
#include "bluetooth_utils.h"
#include "logger.h"
#include "display.h"
#include "lora_utils.h"
#include "configuration.h"
#include "TinyGPSPlus.h"
extern Configuration Config;
extern BluetoothSerial SerialBT;
extern logging::Logger logger;
extern TinyGPSPlus gps;
namespace BLUETOOTH_Utils {
String serialReceived;
bool shouldSendToLoRa = false;
void setup() {
if (!Config.bluetooth) {
btStop();
esp_bt_controller_disable();
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "BT controller disabled");
return;
}
serialReceived.reserve(255);
SerialBT.register_callback(BLUETOOTH_Utils::bluetoothCallback);
SerialBT.onData(BLUETOOTH_Utils::getData); // callback instead of while to avoid RX buffer limit when NMEA data received
uint8_t dmac[6];
esp_efuse_mac_get_default(dmac);
char ourId[5];
snprintf(ourId, sizeof(ourId), "%02x%02x", dmac[4], dmac[5]);
if (!SerialBT.begin(String("LoRa Tracker " + String(ourId)))) {
logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "Bluetooth", "Starting Bluetooth failed!");
show_display("ERROR", "Starting Bluetooth failed!");
while(true) {
delay(1000);
}
}
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Bluetooth", "Bluetooth init done!");
}
void bluetoothCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
if (event == ESP_SPP_SRV_OPEN_EVT) {
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Bluetooth", "Bluetooth client connected !");
} else if (event == ESP_SPP_CLOSE_EVT) {
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Bluetooth", "Bluetooth client disconnected !");
}
}
void getData(const uint8_t *buffer, size_t size) {
if (size == 0) {
return;
}
shouldSendToLoRa = false;
serialReceived.clear();
bool isNmea = buffer[0] == '$';
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "bluetooth", "Received buffer size %d. Nmea=%d", size, isNmea);
for (int i = 0; i < size; i++) {
char c = (char) buffer[i];
if (isNmea) {
gps.encode(c);
} else {
serialReceived += c;
}
}
shouldSendToLoRa = !serialReceived.isEmpty(); // because we can't send data here
if (shouldSendToLoRa) {
logger.log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, "bluetooth",
"Data received should be transmitted to RF => %s", serialReceived.c_str());
}
}
void sendToLoRa() {
if (!shouldSendToLoRa) {
return;
}
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "BT TX", "%s", serialReceived.c_str());
show_display("BT Tx >>", "", serialReceived, 1000);
LoRa_Utils::sendNewPacket(serialReceived);
shouldSendToLoRa = false;
}
void sendPacket(const String& packet) {
if (Config.bluetooth && !packet.isEmpty()) {
SerialBT.println(packet);
}
}
}

16
src/bluetooth_utils.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef BLUETOOTH_UTILS_H
#define BLUETOOTH_UTILS_H
#include <BluetoothSerial.h>
namespace BLUETOOTH_Utils {
void setup();
void bluetoothCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param);
void getData(const uint8_t *buffer, size_t size);
void sendToLoRa();
void sendPacket(const String& packet);
}
#endif