mirror of
https://github.com/Genaker/LoraSA.git
synced 2026-08-12 11:53:08 +02:00
mark: wip work to move stuff around
This commit is contained in:
+240
@@ -0,0 +1,240 @@
|
||||
#include "bt.h"
|
||||
|
||||
std::atomic<bool> bleDeviceConnected{false};
|
||||
|
||||
#ifdef BT_MOBILE
|
||||
#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) { bleDeviceConnected = true; };
|
||||
|
||||
void onDisconnect(BLEServer *pServer)
|
||||
{
|
||||
bleDeviceConnected = false;
|
||||
BLEDevice::startAdvertising();
|
||||
}
|
||||
};
|
||||
|
||||
#else
|
||||
#include <NimBLEDevice.h>
|
||||
|
||||
NimBLEServer *pServer = nullptr;
|
||||
NimBLECharacteristic *pCharacteristic = nullptr;
|
||||
NimBLEAdvertising *pAdvertising = nullptr;
|
||||
|
||||
class BTServerCallbacks : public NimBLEServerCallbacks
|
||||
{
|
||||
public:
|
||||
void onConnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo) override
|
||||
{
|
||||
bleDeviceConnected = true;
|
||||
Serial.printf("Device Connected | Free Heap: %d kByte\n",
|
||||
ESP.getFreeHeap() / 1000);
|
||||
Serial.printf("Client address: %s\n", connInfo.getAddress().toString().c_str());
|
||||
|
||||
pServer->updateConnParams(connInfo.getConnHandle(), 24, 48, 0, 180);
|
||||
}
|
||||
|
||||
void onDisconnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo,
|
||||
int reason) override
|
||||
{
|
||||
bleDeviceConnected = false;
|
||||
Serial.println("Device Disconnected");
|
||||
NimBLEDevice::startAdvertising();
|
||||
}
|
||||
|
||||
void onMTUChange(uint16_t MTU, NimBLEConnInfo &connInfo) override
|
||||
{
|
||||
Serial.printf("MTU updated: %u for connection ID: %u\n", MTU,
|
||||
connInfo.getConnHandle());
|
||||
}
|
||||
} BTServerCallbacks;
|
||||
|
||||
class CharacteristicCallbacks : public NimBLECharacteristicCallbacks
|
||||
{
|
||||
void onRead(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo) override
|
||||
{
|
||||
Serial.printf("%s : onRead(), value: %s\n",
|
||||
pCharacteristic->getUUID().toString().c_str(),
|
||||
pCharacteristic->getValue().c_str());
|
||||
}
|
||||
|
||||
void onWrite(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo) override
|
||||
{
|
||||
Serial.printf("%s : onWrite(), value: %s\n",
|
||||
pCharacteristic->getUUID().toString().c_str(),
|
||||
pCharacteristic->getValue().c_str());
|
||||
}
|
||||
|
||||
void onStatus(NimBLECharacteristic *pCharacteristic, int code) override
|
||||
{
|
||||
#ifdef COMPASS_DEBUG
|
||||
Serial.printf("Notification/Indication return code: %d, %s\n", code,
|
||||
NimBLEUtils::returnCodeToString(code));
|
||||
#endif
|
||||
}
|
||||
|
||||
void onSubscribe(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo,
|
||||
uint16_t subValue) override
|
||||
{
|
||||
std::string str = "Client ID: ";
|
||||
str += connInfo.getConnHandle();
|
||||
str += " Address: ";
|
||||
str += connInfo.getAddress().toString();
|
||||
if (subValue == 0)
|
||||
{
|
||||
str += " Unsubscribed to ";
|
||||
}
|
||||
else if (subValue == 1)
|
||||
{
|
||||
str += " Subscribed to notifications for ";
|
||||
}
|
||||
else if (subValue == 2)
|
||||
{
|
||||
str += " Subscribed to indications for ";
|
||||
}
|
||||
else if (subValue == 3)
|
||||
{
|
||||
str += " Subscribed to notifications and indications for ";
|
||||
}
|
||||
str += std::string(pCharacteristic->getUUID());
|
||||
|
||||
Serial.printf("%s\n", str.c_str());
|
||||
}
|
||||
} chrCallbacks;
|
||||
|
||||
class DescriptorCallbacks : public NimBLEDescriptorCallbacks
|
||||
{
|
||||
void onWrite(NimBLEDescriptor *pDescriptor, NimBLEConnInfo &connInfo) override
|
||||
{
|
||||
std::string dscVal = pDescriptor->getValue();
|
||||
Serial.printf("Descriptor written value: %s\n", dscVal.c_str());
|
||||
}
|
||||
|
||||
void onRead(NimBLEDescriptor *pDescriptor, NimBLEConnInfo &connInfo) override
|
||||
{
|
||||
Serial.printf("%s Descriptor read\n", pDescriptor->getUUID().toString().c_str());
|
||||
}
|
||||
} dscCallbacks;
|
||||
|
||||
#endif // BT_NM
|
||||
|
||||
void initBT()
|
||||
{
|
||||
#ifdef BT_NM
|
||||
NimBLEDevice::init("RSSI_Radar");
|
||||
|
||||
String macAddress = NimBLEDevice::getAddress().toString().c_str();
|
||||
Serial.println("Bluetooth MAC Address: " + macAddress);
|
||||
|
||||
pServer = NimBLEDevice::createServer();
|
||||
|
||||
if (!pServer)
|
||||
{
|
||||
Serial.println("Failed to create BLE Server");
|
||||
}
|
||||
|
||||
pServer->setCallbacks(&BTServerCallbacks);
|
||||
|
||||
NimBLEService *pService = pServer->createService(SERVICE_UUID);
|
||||
|
||||
pCharacteristic = pService->createCharacteristic(
|
||||
CHARACTERISTIC_UUID, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
|
||||
pCharacteristic->setCallbacks(&chrCallbacks);
|
||||
|
||||
pService->start();
|
||||
|
||||
pAdvertising = NimBLEDevice::getAdvertising();
|
||||
pAdvertising->addServiceUUID(SERVICE_UUID);
|
||||
pAdvertising->setName("ESP32_RSSI_Radar");
|
||||
pAdvertising->enableScanResponse(true);
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
#endif // BT_MOBILE
|
||||
|
||||
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
|
||||
#ifdef BT_MOBILE
|
||||
pCharacteristic->setValue(data.c_str());
|
||||
pCharacteristic->notify();
|
||||
#endif
|
||||
}
|
||||
|
||||
void sendBTData(Message &msg)
|
||||
{
|
||||
if (msg.type != SCAN_HEADING_MAX && msg.type != SCAN_MAX_RESULT &&
|
||||
msg.type != SCAN_RESULT)
|
||||
{
|
||||
Serial.println("Unsupported message type: " + String(msg.type));
|
||||
return;
|
||||
}
|
||||
|
||||
String data = "{\"SCAN_RESULT\":{\"Hmin\":" + String(msg.payload.dump.heading_min) +
|
||||
",\"Hmax\":" + String(msg.payload.dump.heading_max) + ",\"Spectrum\":[";
|
||||
|
||||
for (int i = 0; i < msg.payload.dump.sz; i++)
|
||||
{
|
||||
data += String(i == 0 ? "" : ",") +
|
||||
"{\"F\":" + String(msg.payload.dump.freqs_khz[i]) +
|
||||
",\"R\":" + String(msg.payload.dump.rssis[i]) +
|
||||
(msg.payload.dump.rssis2 == NULL
|
||||
? ""
|
||||
: ",\"R2\":" + String(msg.payload.dump.rssis2[i])) +
|
||||
"}";
|
||||
}
|
||||
|
||||
data += "]}}";
|
||||
#ifdef COMPASS_DEBUG
|
||||
Serial.println("Sending data: " + data);
|
||||
#endif
|
||||
#ifdef BT_MOBILE
|
||||
pCharacteristic->setValue(data.c_str());
|
||||
pCharacteristic->notify();
|
||||
#endif
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <atomic>
|
||||
#include <comms.h>
|
||||
|
||||
extern std::atomic<bool> bleDeviceConnected;
|
||||
|
||||
#ifdef BT_MOBILE
|
||||
void initBT();
|
||||
|
||||
#ifndef BT_NM
|
||||
#include <BLEDevice.h>
|
||||
#include <BLEServer.h>
|
||||
extern BLEServer *pServer;
|
||||
extern BLECharacteristic *pCharacteristic;
|
||||
extern BLEAdvertising *pAdvertising;
|
||||
#else
|
||||
#include <NimBLEDevice.h>
|
||||
extern NimBLEServer *pServer;
|
||||
extern NimBLECharacteristic *pCharacteristic;
|
||||
extern NimBLEAdvertising *pAdvertising;
|
||||
#endif
|
||||
|
||||
#endif // BT_MOBILE
|
||||
|
||||
void sendBTData(float heading, float rssi);
|
||||
void sendBTData(Message &msg);
|
||||
@@ -0,0 +1,108 @@
|
||||
#include "bt_wifi_scan.h"
|
||||
|
||||
#ifdef OSD_ENABLED
|
||||
|
||||
// These globals are defined in main.cpp
|
||||
extern long cycleCnt;
|
||||
extern bool present;
|
||||
extern bool scanFinished;
|
||||
|
||||
#ifdef WIFI_SCANNING_ENABLED
|
||||
#include "WiFi.h"
|
||||
|
||||
void scanWiFi(DFRobot_OSD &osd)
|
||||
{
|
||||
osd.clear();
|
||||
osd.displayString(14, 2, "Scanning WiFi..");
|
||||
int n = WiFi.scanNetworks();
|
||||
#ifdef PRINT_DEBUG
|
||||
Serial.println("scan done");
|
||||
if (n == 0)
|
||||
{
|
||||
Serial.println("no networks found");
|
||||
}
|
||||
#endif
|
||||
if (n > 0)
|
||||
{
|
||||
#ifdef PRINT_DEBUG
|
||||
Serial.print(n);
|
||||
Serial.println(" networks found");
|
||||
#endif
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
#ifdef PRINT_DEBUG
|
||||
Serial.print(i + 1);
|
||||
Serial.print(": ");
|
||||
Serial.print(WiFi.SSID(i));
|
||||
Serial.print(" (");
|
||||
Serial.print(WiFi.RSSI(i));
|
||||
Serial.print(")");
|
||||
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
|
||||
#endif
|
||||
osd.displayString(i + 1, 1,
|
||||
"WF:" + String((WiFi.SSID(i) + ":" + WiFi.RSSI(i))));
|
||||
}
|
||||
}
|
||||
osd.displayChar(14, 1, 0x10f);
|
||||
}
|
||||
#endif // WIFI_SCANNING_ENABLED
|
||||
|
||||
#ifdef BT_SCANNING_ENABLED
|
||||
#include <BLEAdvertisedDevice.h>
|
||||
#include <BLEDevice.h>
|
||||
#include <BLEScan.h>
|
||||
#include <BLEUtils.h>
|
||||
|
||||
void scanBT(DFRobot_OSD &osd)
|
||||
{
|
||||
osd.clear();
|
||||
osd.displayString(14, 2, "Scanning BT...");
|
||||
cycleCnt++;
|
||||
|
||||
BLEDevice::init("");
|
||||
BLEScan *pBLEScan = BLEDevice::getScan();
|
||||
pBLEScan->setActiveScan(true);
|
||||
pBLEScan->setInterval(0x50);
|
||||
pBLEScan->setWindow(0x30);
|
||||
|
||||
#ifdef SERIAL_PRINT
|
||||
Serial.printf("Start BLE scan for %d seconds...\n", BT_SCAN_TIME);
|
||||
#endif
|
||||
|
||||
BLEScanResults foundDevices = pBLEScan->start(BT_SCAN_TIME);
|
||||
int count = foundDevices.getCount();
|
||||
#ifdef PRINT_DEBUG
|
||||
Serial.printf("Found devices: %d \n", count);
|
||||
#endif
|
||||
present = false;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
BLEAdvertisedDevice device = foundDevices.getDevice(i);
|
||||
String currDevAddr = device.getAddress().toString().c_str();
|
||||
String deviceName;
|
||||
if (device.haveName())
|
||||
{
|
||||
deviceName = device.getName().c_str();
|
||||
}
|
||||
else
|
||||
{
|
||||
deviceName = currDevAddr;
|
||||
}
|
||||
|
||||
#ifdef PRINT_DEBUG
|
||||
Serial.printf("Found device #%s/%s with RSSI: %d \n", currDevAddr, deviceName,
|
||||
device.getRSSI());
|
||||
#endif
|
||||
osd.displayString(i + 1, 1,
|
||||
"BT:" + deviceName + ":" + String(device.getRSSI()) + " \n");
|
||||
}
|
||||
#ifdef PRINT_DEBUG
|
||||
Serial.println("Scan done!");
|
||||
Serial.printf("Cycle counter: %d, Free heap: %d \n", cycleCnt, ESP.getFreeHeap());
|
||||
#endif
|
||||
osd.displayChar(14, 1, 0x10f);
|
||||
scanFinished = true;
|
||||
}
|
||||
#endif // BT_SCANNING_ENABLED
|
||||
|
||||
#endif // OSD_ENABLED
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#ifdef OSD_ENABLED
|
||||
#include "DFRobot_OSD.h"
|
||||
|
||||
#ifdef WIFI_SCANNING_ENABLED
|
||||
void scanWiFi(DFRobot_OSD &osd);
|
||||
#endif
|
||||
|
||||
#ifdef BT_SCANNING_ENABLED
|
||||
void scanBT(DFRobot_OSD &osd);
|
||||
#endif
|
||||
|
||||
#endif // OSD_ENABLED
|
||||
@@ -10,7 +10,7 @@ size_t StackedChart::addChart(Chart *c)
|
||||
Chart **cc = new Chart *[charts_sz + 1];
|
||||
memcpy(cc, charts, charts_sz * sizeof(Chart *));
|
||||
cc[charts_sz] = c;
|
||||
free(charts);
|
||||
delete[] charts;
|
||||
|
||||
c->reset(pos_x + c->pos_x, pos_y + c->pos_y, trim_w(c->pos_x, c->width, width),
|
||||
c->height);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "comms.h"
|
||||
#include <atomic>
|
||||
|
||||
int packetRssi = 0;
|
||||
|
||||
@@ -414,8 +415,8 @@ Message *_deserialize_config_task(size_t len, size_t &p, uint8_t *packet)
|
||||
return message;
|
||||
}
|
||||
|
||||
volatile bool _received = false;
|
||||
void _rcv() { _received = true; }
|
||||
static std::atomic<bool> _received{false};
|
||||
void _rcv() { _received.store(true, std::memory_order_release); }
|
||||
|
||||
Message *RadioComms::receive(uint16_t timeout_ms)
|
||||
{
|
||||
@@ -426,7 +427,7 @@ Message *RadioComms::receive(uint16_t timeout_ms)
|
||||
#warning Radio Comms not fully supported for LR1121 or SX1276
|
||||
#else
|
||||
// because of this, receive is single-threaded, single-device
|
||||
_received = false;
|
||||
_received.store(false, std::memory_order_relaxed);
|
||||
radio.setDio1Action(_rcv);
|
||||
uint32_t timeout_ticks = (uint32_t)timeout_ms * (1000000 / 15625);
|
||||
|
||||
@@ -438,8 +439,7 @@ Message *RadioComms::receive(uint16_t timeout_ms)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// wait on a semaphore
|
||||
while (!_received)
|
||||
while (!_received.load(std::memory_order_acquire))
|
||||
{
|
||||
yield();
|
||||
}
|
||||
|
||||
@@ -136,7 +136,7 @@ bool Config::updateConfig(String key, String value)
|
||||
|
||||
if (key.equalsIgnoreCase("listen_on_usb"))
|
||||
{
|
||||
listen_on_serial0 = value;
|
||||
listen_on_usb = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -828,7 +828,7 @@ BusConfig BusConfig::configure(String cfg)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c.bus_type == SERIAL && k.equals("tx") ||
|
||||
if (c.bus_type == UART && k.equals("tx") ||
|
||||
c.bus_type == SPI && k.equals("mosi") ||
|
||||
c.bus_type == WIRE && k.equals("sda"))
|
||||
{
|
||||
@@ -836,7 +836,7 @@ BusConfig BusConfig::configure(String cfg)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c.bus_type == SERIAL && k.equals("rx") ||
|
||||
if (c.bus_type == UART && k.equals("rx") ||
|
||||
c.bus_type == SPI && k.equals("miso"))
|
||||
{
|
||||
c.rx = param.toInt();
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
#include "file_io.h"
|
||||
#include <LittleFS.h>
|
||||
|
||||
void initLittleFS()
|
||||
{
|
||||
if (!LittleFS.begin(true))
|
||||
{
|
||||
Serial.println("An error has occurred while mounting LittleFS");
|
||||
}
|
||||
Serial.println("LittleFS mounted successfully");
|
||||
}
|
||||
|
||||
String readFile(fs::FS &fs, const char *path)
|
||||
{
|
||||
Serial.printf("Reading file: %s\r\n", path);
|
||||
|
||||
File file = fs.open(path);
|
||||
if (!file || file.isDirectory())
|
||||
{
|
||||
Serial.println("- failed to open file for reading");
|
||||
return String("");
|
||||
}
|
||||
String content;
|
||||
Serial.println("- read from file:");
|
||||
while (file.available())
|
||||
{
|
||||
content += file.readStringUntil('\n') + "\n";
|
||||
}
|
||||
file.close();
|
||||
return content;
|
||||
}
|
||||
|
||||
void writeFile(fs::FS &fs, const char *path, const char *message)
|
||||
{
|
||||
Serial.printf("Writing file: %s\r\n", path);
|
||||
Serial.printf("Content: %s\r\n", message);
|
||||
|
||||
File file = fs.open(path, FILE_WRITE);
|
||||
if (!file)
|
||||
{
|
||||
Serial.println("- failed to open file for writing");
|
||||
return;
|
||||
}
|
||||
if (file.print(message))
|
||||
{
|
||||
Serial.println("- file written");
|
||||
delay(500);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("- write failed");
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "FS.h"
|
||||
|
||||
void initLittleFS();
|
||||
String readFile(fs::FS &fs, const char *path);
|
||||
void writeFile(fs::FS &fs, const char *path, const char *message);
|
||||
@@ -0,0 +1,47 @@
|
||||
#include "input.h"
|
||||
|
||||
int cursor_x_position = 0;
|
||||
bool joy_btn_clicked = false;
|
||||
|
||||
static int joyXMid = 0;
|
||||
|
||||
bool joy_btn_click()
|
||||
{
|
||||
joy_btn_clicked = true;
|
||||
return digitalRead(JOY_BTN_PIN) == HIGH ? false : true;
|
||||
}
|
||||
|
||||
void calibrate_joy()
|
||||
{
|
||||
int cal_X = 0;
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
cal_X += analogRead(JOY_X_PIN);
|
||||
}
|
||||
joyXMid = cal_X / 100;
|
||||
}
|
||||
|
||||
static const int MID = 100;
|
||||
|
||||
int get_joy_x(bool logical)
|
||||
{
|
||||
int joyX = analogRead(JOY_X_PIN);
|
||||
|
||||
if (logical)
|
||||
{
|
||||
if (joyX < joyXMid - MID)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (joyX > joyXMid + MID)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return joyX;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
// Joystick pin configuration
|
||||
constexpr int JOY_X_PIN = 19;
|
||||
constexpr int JOY_BTN_PIN = 46;
|
||||
|
||||
extern int cursor_x_position;
|
||||
extern bool joy_btn_clicked;
|
||||
|
||||
bool joy_btn_click();
|
||||
void calibrate_joy();
|
||||
int get_joy_x(bool logical = false);
|
||||
@@ -12,6 +12,17 @@ SX1262Module::SX1262Module(RadioModuleSPIConfig radio2) : RadioModule()
|
||||
Serial.printf("Initialized Radio2: %s\n", radio2.toStr().c_str());
|
||||
}
|
||||
|
||||
SX1262Module::~SX1262Module()
|
||||
{
|
||||
if (_radio != nullptr)
|
||||
{
|
||||
Module *mod = _radio->getMod();
|
||||
delete _radio;
|
||||
delete mod;
|
||||
_radio = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
int16_t SX1262Module::beginScan(float init_freq, float bw, uint8_t shaping)
|
||||
{
|
||||
int16_t status = _radio->beginFSK(init_freq);
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
struct RadioModule
|
||||
{
|
||||
RadioModule() {};
|
||||
virtual ~RadioModule() {};
|
||||
|
||||
virtual int16_t beginScan(float init_freq, float bw, uint8_t shaping) = 0;
|
||||
virtual int16_t setFrequency(float freq) = 0;
|
||||
@@ -19,6 +20,7 @@ struct SX1262Module : RadioModule
|
||||
SX1262 *_radio;
|
||||
|
||||
SX1262Module(RadioModuleSPIConfig cfg);
|
||||
~SX1262Module() override;
|
||||
|
||||
int16_t beginScan(float init_freq, float bw, uint8_t shaping) override;
|
||||
int16_t setFrequency(float freq) override;
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
#include "wifi_server.h"
|
||||
#include "file_io.h"
|
||||
#include <LittleFS.h>
|
||||
|
||||
// Parameter name constants
|
||||
const String SSID = "ssid";
|
||||
const String PASS = "pass";
|
||||
const String IP = "ip";
|
||||
const String GATEWAY = "gateway";
|
||||
const String FSTART = "fstart";
|
||||
const String FEND = "fend";
|
||||
|
||||
// WiFi config variables
|
||||
String ssid = "LoraSA", pass = "1234567890", ip = "192.168.1.100",
|
||||
gateway = "192.168.1.1", fstart = "", fend = "", smpls = "";
|
||||
|
||||
#ifdef WEB_SERVER
|
||||
#include <AsyncTCP.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
// Create AsyncWebServer object on port 80
|
||||
AsyncWebServer server(80);
|
||||
|
||||
IPAddress localIP;
|
||||
IPAddress localGateway;
|
||||
IPAddress subnet(255, 255, 0, 0);
|
||||
|
||||
unsigned long previousMillis = 0;
|
||||
const long interval = 10000;
|
||||
|
||||
bool initWiFi()
|
||||
{
|
||||
Serial.println("SSID:" + ssid);
|
||||
Serial.println("PSWD:" + pass);
|
||||
Serial.println("IP:" + ip);
|
||||
Serial.println("SUB:" + subnet);
|
||||
Serial.println("GATAWAY:" + gateway);
|
||||
if (ssid == "" || ip == "")
|
||||
{
|
||||
Serial.println("Undefined SSID or IP address.");
|
||||
return false;
|
||||
}
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
localIP.fromString(ip.c_str());
|
||||
localGateway.fromString(gateway.c_str());
|
||||
|
||||
if (!WiFi.config(localIP, localGateway, subnet))
|
||||
{
|
||||
Serial.println("STA Failed to configure");
|
||||
return false;
|
||||
}
|
||||
WiFi.begin(ssid.c_str(), pass.c_str());
|
||||
Serial.println("Connecting to WiFi...");
|
||||
|
||||
unsigned long currentMillis = millis();
|
||||
previousMillis = currentMillis;
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
currentMillis = millis();
|
||||
if (currentMillis - previousMillis >= interval)
|
||||
{
|
||||
Serial.println("Failed to connect.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Serial.println(WiFi.localIP());
|
||||
return true;
|
||||
}
|
||||
|
||||
static void serverServer()
|
||||
{
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{ request->send(LittleFS, "/index.html", "text/html"); });
|
||||
|
||||
server.serveStatic("/", LittleFS, "/");
|
||||
|
||||
server.on("/", HTTP_POST,
|
||||
[](AsyncWebServerRequest *request)
|
||||
{
|
||||
int params = request->params();
|
||||
for (int i = 0; i < params; i++)
|
||||
{
|
||||
Serial.println("Parameter " + String(i) + ": " +
|
||||
request->getParam(i)->value());
|
||||
}
|
||||
Serial.println(request->params());
|
||||
|
||||
String p;
|
||||
if (request->hasParam(IP, true))
|
||||
{
|
||||
p = request->getParam(IP, true)->value();
|
||||
writeParameterToParameterFile(IP, p);
|
||||
}
|
||||
|
||||
if (request->hasParam(SSID, true))
|
||||
{
|
||||
p = request->getParam(SSID, true)->value();
|
||||
writeParameterToParameterFile(SSID, p);
|
||||
}
|
||||
|
||||
if (request->hasParam(PASS, true))
|
||||
{
|
||||
p = request->getParam(PASS, true)->value();
|
||||
writeParameterToParameterFile(PASS, p);
|
||||
}
|
||||
|
||||
if (request->hasParam(GATEWAY, true))
|
||||
{
|
||||
p = request->getParam(GATEWAY, true)->value();
|
||||
writeParameterToParameterFile(GATEWAY, p);
|
||||
}
|
||||
|
||||
if (request->hasParam(FSTART, true))
|
||||
{
|
||||
p = request->getParam(FSTART, true)->value();
|
||||
writeParameterToParameterFile(FSTART, p);
|
||||
}
|
||||
|
||||
if (request->hasParam(FEND, true))
|
||||
{
|
||||
p = request->getParam(FEND, true)->value();
|
||||
writeParameterToParameterFile(FEND, p);
|
||||
}
|
||||
|
||||
if (request->hasParam("samples", true))
|
||||
{
|
||||
p = request->getParam("samples", true)->value();
|
||||
writeParameterToParameterFile("samples", p);
|
||||
}
|
||||
|
||||
request->send(200, "text/plain",
|
||||
"Done. ESP will restart, connect to your router and "
|
||||
"go to IP address: " +
|
||||
ip);
|
||||
delay(3000);
|
||||
ESP.restart();
|
||||
});
|
||||
|
||||
server.begin();
|
||||
}
|
||||
|
||||
void serverStart()
|
||||
{
|
||||
if (initWiFi())
|
||||
{
|
||||
Serial.println("Setting Secure WIFI (Access Point)");
|
||||
serverServer();
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Setting AP (Access Point)");
|
||||
WiFi.softAP("LoraSA", NULL);
|
||||
|
||||
IPAddress IP = WiFi.softAPIP();
|
||||
Serial.print("AP IP address: ");
|
||||
Serial.println(IP);
|
||||
|
||||
serverServer();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void writeParameterToFile(String value, String file)
|
||||
{
|
||||
writeFile(LittleFS, file.c_str(), value.c_str());
|
||||
}
|
||||
|
||||
void writeParameterToParameterFile(String param, String value)
|
||||
{
|
||||
String file = String("/" + param + ".txt");
|
||||
writeParameterToFile(value, file.c_str());
|
||||
}
|
||||
|
||||
String readParameterFromParameterFile(String param)
|
||||
{
|
||||
String file = String("/" + param + ".txt");
|
||||
return readFile(LittleFS, file.c_str());
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
// Parameter name constants
|
||||
extern const String SSID;
|
||||
extern const String PASS;
|
||||
extern const String IP;
|
||||
extern const String GATEWAY;
|
||||
extern const String FSTART;
|
||||
extern const String FEND;
|
||||
|
||||
// WiFi config variables
|
||||
extern String ssid, pass, ip, gateway, fstart, fend, smpls;
|
||||
|
||||
void writeParameterToFile(String value, String file);
|
||||
void writeParameterToParameterFile(String param, String value);
|
||||
String readParameterFromParameterFile(String param);
|
||||
|
||||
#ifdef WEB_SERVER
|
||||
void serverStart();
|
||||
#endif
|
||||
Reference in New Issue
Block a user