From 3eef6ebfea6a20a7ac1daf5f5547404bd8042b37 Mon Sep 17 00:00:00 2001 From: richonguzman Date: Sat, 5 Aug 2023 10:20:55 -0400 Subject: [PATCH] 1.1 --- data/tracker_config.json | 3 +- src/LoRa_APRS_Tracker.cpp | 2 + src/bluetooth_utils.cpp | 105 ++++++++++++++++++++++++++++++++++++++ src/bluetooth_utils.h | 16 ++++++ 4 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 src/bluetooth_utils.cpp create mode 100644 src/bluetooth_utils.h diff --git a/data/tracker_config.json b/data/tracker_config.json index 53437f8..5ec5f37 100644 --- a/data/tracker_config.json +++ b/data/tracker_config.json @@ -71,7 +71,8 @@ "standingUpdateTime": 15, "sendAltitude": true, "sendBatteryInfo": false, - "bluetooth": true + "bluetooth": true, + "disableGPS": false }, "ptt_trigger": { "active": false, diff --git a/src/LoRa_APRS_Tracker.cpp b/src/LoRa_APRS_Tracker.cpp index 2969d46..48dc615 100644 --- a/src/LoRa_APRS_Tracker.cpp +++ b/src/LoRa_APRS_Tracker.cpp @@ -6,6 +6,7 @@ #include #include #include +#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); diff --git a/src/bluetooth_utils.cpp b/src/bluetooth_utils.cpp new file mode 100644 index 0000000..e286f95 --- /dev/null +++ b/src/bluetooth_utils.cpp @@ -0,0 +1,105 @@ +#include +#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); + } + } + +} \ No newline at end of file diff --git a/src/bluetooth_utils.h b/src/bluetooth_utils.h new file mode 100644 index 0000000..eb6e80d --- /dev/null +++ b/src/bluetooth_utils.h @@ -0,0 +1,16 @@ +#ifndef BLUETOOTH_UTILS_H +#define BLUETOOTH_UTILS_H + +#include + +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 \ No newline at end of file