mirror of
https://github.com/Genaker/LoraSA.git
synced 2026-03-28 17:42:59 +01:00
UI Spectrum + BT and Compass Fixes
This commit is contained in:
170
src/main.cpp
170
src/main.cpp
@@ -63,19 +63,38 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef BT_MOBILE
|
||||
#include "esp_task_wdt.h"
|
||||
#include <NimBLEDevice.h>
|
||||
|
||||
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
|
||||
#include "esp_log.h"
|
||||
bool deviceConnected = false;
|
||||
#define SERVICE_UUID "00001234-0000-1000-8000-00805f9b34fb"
|
||||
#define CHARACTERISTIC_UUID "00001234-0000-1000-8000-00805f9b34ac"
|
||||
|
||||
#ifndef BT_NM
|
||||
#include <BLE2902.h>
|
||||
#include <BLEDevice.h>
|
||||
#include <BLEServer.h>
|
||||
#include <BLEUtils.h>
|
||||
|
||||
BLEServer *pServer = NULL;
|
||||
BLECharacteristic *pCharacteristic = NULL;
|
||||
BLEAdvertising *pAdvertising = NULL;
|
||||
|
||||
class MyServerCallbacks : public BLEServerCallbacks
|
||||
{
|
||||
void onConnect(BLEServer *pServer) { deviceConnected = true; };
|
||||
|
||||
void onDisconnect(BLEServer *pServer)
|
||||
{
|
||||
deviceConnected = false;
|
||||
BLEDevice::startAdvertising(); // Restart advertising after disconnect
|
||||
}
|
||||
};
|
||||
|
||||
#else
|
||||
#include <NimBLEDevice.h>
|
||||
|
||||
NimBLEServer *pServer = nullptr;
|
||||
NimBLECharacteristic *pCharacteristic = nullptr;
|
||||
NimBLEAdvertising *pAdvertising = nullptr;
|
||||
bool deviceConnected = false;
|
||||
|
||||
#define SERVICE_UUID "00001234-0000-1000-8000-00805f9b34fb"
|
||||
#define CHARACTERISTIC_UUID "00001234-0000-1000-8000-00805f9b34ac"
|
||||
|
||||
class BTServerCallbacks : public NimBLEServerCallbacks
|
||||
{
|
||||
@@ -86,13 +105,7 @@ class BTServerCallbacks : public NimBLEServerCallbacks
|
||||
Serial.printf("Device Connected | Free Heap: %d kByte\n",
|
||||
ESP.getFreeHeap() / 1000);
|
||||
Serial.printf("Client address: %s\n", connInfo.getAddress().toString().c_str());
|
||||
/**
|
||||
* We can use the connection handle here to ask for different connection
|
||||
* parameters. Args: connection handle, min connection interval, max connection
|
||||
* interval latency, supervision timeout. Units; Min/Max Intervals: 1.25
|
||||
* millisecond increments. Latency: number of intervals allowed to skip. Timeout:
|
||||
* 10 millisecond increments.
|
||||
*/
|
||||
|
||||
pServer->updateConnParams(connInfo.getConnHandle(), 24, 48, 0, 180);
|
||||
}
|
||||
|
||||
@@ -111,7 +124,6 @@ class BTServerCallbacks : public NimBLEServerCallbacks
|
||||
}
|
||||
} BTServerCallbacks;
|
||||
|
||||
/** Handler class for characteristic actions */
|
||||
class CharacteristicCallbacks : public NimBLECharacteristicCallbacks
|
||||
{
|
||||
void onRead(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo) override
|
||||
@@ -128,16 +140,14 @@ class CharacteristicCallbacks : public NimBLECharacteristicCallbacks
|
||||
pCharacteristic->getValue().c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
* The value returned in code is the NimBLE host return code.
|
||||
*/
|
||||
void onStatus(NimBLECharacteristic *pCharacteristic, int code) override
|
||||
{
|
||||
#ifdef COMPASS_DEBUG
|
||||
Serial.printf("Notification/Indication return code: %d, %s\n", code,
|
||||
NimBLEUtils::returnCodeToString(code));
|
||||
#endif
|
||||
}
|
||||
|
||||
/** Peer subscribed to notifications/indications */
|
||||
void onSubscribe(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo,
|
||||
uint16_t subValue) override
|
||||
{
|
||||
@@ -167,7 +177,6 @@ class CharacteristicCallbacks : public NimBLECharacteristicCallbacks
|
||||
}
|
||||
} chrCallbacks;
|
||||
|
||||
/** Handler class for descriptor actions */
|
||||
class DescriptorCallbacks : public NimBLEDescriptorCallbacks
|
||||
{
|
||||
void onWrite(NimBLEDescriptor *pDescriptor, NimBLEConnInfo &connInfo) override
|
||||
@@ -182,11 +191,11 @@ class DescriptorCallbacks : public NimBLEDescriptorCallbacks
|
||||
}
|
||||
} dscCallbacks;
|
||||
|
||||
#endif
|
||||
|
||||
void initBT()
|
||||
{
|
||||
esp_log_level_set("*", ESP_LOG_VERBOSE);
|
||||
ESP_LOGI("DEBUG", "Starting ESP32 Debugging...");
|
||||
|
||||
#ifdef BT_NM
|
||||
// Initialize BLE device
|
||||
NimBLEDevice::init("RSSI_Radar");
|
||||
|
||||
@@ -230,6 +239,30 @@ void initBT()
|
||||
pAdvertising->start();
|
||||
|
||||
Serial.println("BLE server started.");
|
||||
#else
|
||||
BLEDevice::init("ESP32_RADAR");
|
||||
pServer = BLEDevice::createServer();
|
||||
pServer->setCallbacks(new MyServerCallbacks());
|
||||
|
||||
BLEService *pService = pServer->createService(SERVICE_UUID);
|
||||
|
||||
pCharacteristic = pService->createCharacteristic(
|
||||
CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ |
|
||||
BLECharacteristic::PROPERTY_WRITE |
|
||||
BLECharacteristic::PROPERTY_NOTIFY);
|
||||
|
||||
pCharacteristic->setValue("Hello from ESP32");
|
||||
pService->start();
|
||||
|
||||
pAdvertising = BLEDevice::getAdvertising();
|
||||
pAdvertising->addServiceUUID(SERVICE_UUID);
|
||||
pAdvertising->setScanResponse(true);
|
||||
pAdvertising->setMinInterval(300);
|
||||
pAdvertising->setMaxInterval(350);
|
||||
BLEDevice::startAdvertising();
|
||||
|
||||
Serial.println("BLE server is ready!");
|
||||
#endif
|
||||
}
|
||||
|
||||
// Function to send RSSI and Heading Data
|
||||
@@ -237,7 +270,10 @@ void sendBTData(float heading, float rssi)
|
||||
{
|
||||
String data =
|
||||
"RSSI_HEADING: '{H:" + String(heading) + ",RSSI:-" + String(rssi) + "}'";
|
||||
|
||||
#ifdef COMPASS_DEBUG
|
||||
Serial.println("Sending data: " + data);
|
||||
#endif
|
||||
pCharacteristic->setValue(data.c_str()); // Set BLE characteristic value
|
||||
pCharacteristic->notify(); // Notify connected client
|
||||
}
|
||||
@@ -381,7 +417,7 @@ void displaySensorDetails(void)
|
||||
Serial.println(" uT");
|
||||
Serial.println("------------------------------------");
|
||||
Serial.println("");
|
||||
delay(500);
|
||||
heltec_delay(500);
|
||||
}
|
||||
|
||||
// Variables for dynamic calibration
|
||||
@@ -925,7 +961,7 @@ int16_t initForScan(float freq)
|
||||
|
||||
// LR1121 TCXO Voltage 2.85~3.15V
|
||||
radio.setTCXO(3.0);
|
||||
delay(1000);
|
||||
heltec_delay(1000);
|
||||
#else
|
||||
state = radio.beginFSK(freq);
|
||||
#endif
|
||||
@@ -947,7 +983,7 @@ A:
|
||||
Serial.print(F("Failed to start receive mode, error code: "));
|
||||
display.drawString(0, 64 - 10, "E:startReceive");
|
||||
display.display();
|
||||
delay(2000);
|
||||
heltec_delay(2000);
|
||||
Serial.println(state);
|
||||
gotoAcounter++;
|
||||
if (gotoAcounter < 5)
|
||||
@@ -1071,6 +1107,7 @@ void init_radio()
|
||||
|
||||
if (config.radio2.enabled && config.radio2.module.equalsIgnoreCase("SX1262"))
|
||||
{
|
||||
|
||||
radio2 = new SX1262Module(config.radio2);
|
||||
state = radio2->beginScan(CONF_FREQ_BEGIN, BANDWIDTH, RADIOLIB_SHAPING_NONE);
|
||||
if (state == RADIOLIB_ERR_NONE)
|
||||
@@ -1286,7 +1323,6 @@ void draw360Scale(int start = 0, int end = 360, int width = 128, int height = 64
|
||||
for (int x = 0; x <= width; x += stepPixel)
|
||||
{
|
||||
// int x = map(i, start, end, 0, scaleLength);
|
||||
Serial.println("Tick: " + String(x));
|
||||
if (x == 128)
|
||||
{
|
||||
x = x - 1;
|
||||
@@ -2353,11 +2389,12 @@ float getCompassHeading()
|
||||
{
|
||||
calStart = millis();
|
||||
}
|
||||
#ifdef COMPASS_ENABLED
|
||||
|
||||
/* Get a new sensor event */
|
||||
sensors_event_t event2;
|
||||
mag.getEvent(&event2);
|
||||
sensors_event_t event3;
|
||||
mag.getEvent(&event3);
|
||||
|
||||
#ifdef COMPASS_DEBUG
|
||||
/* Display the results (magnetic vector values are in micro-Tesla (uT)) */
|
||||
@@ -2389,9 +2426,9 @@ float getCompassHeading()
|
||||
// Dynamicly Calibrated out
|
||||
|
||||
// Read raw magnetometer data
|
||||
float x = event2.magnetic.x;
|
||||
float y = event2.magnetic.y;
|
||||
float z = event2.magnetic.z;
|
||||
float x = (event2.magnetic.x + event3.magnetic.x) / 2;
|
||||
float y = (event2.magnetic.y + event3.magnetic.y) / 2;
|
||||
float z = (event2.magnetic.z + event3.magnetic.z) / 2;
|
||||
|
||||
// Doing calibration first 1 minute
|
||||
if (millis() - calStart < 60000)
|
||||
@@ -2446,7 +2483,6 @@ float getCompassHeading()
|
||||
// Convert radians to degrees for readability.
|
||||
float headingDegrees = heading * 180 / M_PI;
|
||||
return headingDegrees;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -2483,6 +2519,51 @@ void loop(void)
|
||||
doScan();
|
||||
reportScan();
|
||||
}
|
||||
|
||||
#ifdef BT_MOBILE
|
||||
#ifdef BT_RSSI
|
||||
while (true)
|
||||
{
|
||||
float startFreq = FREQ_BEGIN - 0.5; // Start 2 MHz left
|
||||
float endFreq = FREQ_END + 0.5; // End 2 MHz right
|
||||
float step = 0.5; // Step size in MHz
|
||||
float rssi = -122;
|
||||
float rssiMax = -999;
|
||||
|
||||
for (float freq = startFreq; freq <= endFreq; freq += step)
|
||||
{
|
||||
setFrequency(freq);
|
||||
// Serial.println("COMPASS FREQ SET: " + String(freq));
|
||||
|
||||
// heltec_delay(5);
|
||||
#ifdef USING_LR1121
|
||||
radio.getRssiInst(&rssi);
|
||||
#else
|
||||
rssi = getRssi(false);
|
||||
#endif
|
||||
if (rssi > rssiMax)
|
||||
{
|
||||
rssiMax = rssi;
|
||||
}
|
||||
Serial.println("RSSI: " + String(freq) + ":" + String(rssiMax));
|
||||
// String p = _scan_result_str(m.payload.dump, 10);
|
||||
|
||||
if (pServer && pServer->getConnectedCount())
|
||||
{
|
||||
String str = "RSSI:TEST";
|
||||
if (pCharacteristic)
|
||||
{
|
||||
pCharacteristic->setValue(
|
||||
str.c_str()); // Set BLE characteristic value
|
||||
pCharacteristic->notify(); // Notify connected client
|
||||
}
|
||||
delay(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef COMPASS_ENABLED
|
||||
#if defined(COMPASS_FREQ)
|
||||
// delay(1000);
|
||||
@@ -2516,15 +2597,15 @@ void loop(void)
|
||||
draw360Scale(0, 360, 128, 64);
|
||||
}
|
||||
|
||||
float rssi = -122;
|
||||
float rssiMax = -999;
|
||||
if (headingDegrees >= 0 && headingDegrees <= 360)
|
||||
{
|
||||
float startFreq = COMPASS_FREQ - 1.0; // Start 2 MHz left
|
||||
float endFreq = COMPASS_FREQ + 1.0; // End 2 MHz right
|
||||
float startFreq = COMPASS_FREQ - 0.5; // Start 2 MHz left
|
||||
float endFreq = COMPASS_FREQ + 0.5; // End 2 MHz right
|
||||
float step = 0.5; // Step size in MHz
|
||||
#ifdef COMPASS_RSSI
|
||||
draw360Scale(0, 360, 128, 64);
|
||||
float rssi = -122;
|
||||
float rssiMax = -999;
|
||||
for (int i = 0; i < SAMPLES_RSSI; i++)
|
||||
{
|
||||
for (float freq = startFreq; freq <= endFreq; freq += step)
|
||||
@@ -2540,7 +2621,7 @@ void loop(void)
|
||||
#endif
|
||||
float headingDegreesAfter = getCompassHeading();
|
||||
float compassDiff = abs(headingDegreesAfter - headingDegrees);
|
||||
if (compassDiff >= 2)
|
||||
if (compassDiff >= 3)
|
||||
{
|
||||
goto compass;
|
||||
}
|
||||
@@ -2963,6 +3044,7 @@ void doScan()
|
||||
max_x_rssi[display_x] = max_rssi;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // SCAN_METHOD == METHOD_RSSI
|
||||
|
||||
// if this code is not executed LORA radio doesn't work
|
||||
@@ -3316,17 +3398,7 @@ void loraSendMessage(Message &msg)
|
||||
|
||||
void reportScan()
|
||||
{
|
||||
#ifdef BT_MOBILE
|
||||
#ifndef COMPASS_ENABLED
|
||||
// String p = _scan_result_str(m.payload.dump, 10);
|
||||
if (pServer->getConnectedCount())
|
||||
{
|
||||
pCharacteristic->setValue("Test"); // Set BLE characteristic value
|
||||
pCharacteristic->notify(); // Notify connected client
|
||||
}
|
||||
delay(1000);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if (!config.lora_enabled)
|
||||
return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user