From 9dabf14aa3bce6535f7df76cb5d279a8accf5f2d Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sun, 28 Aug 2022 22:54:26 +0200 Subject: [PATCH] ESP32: Experimental BT support, enable with #define SERIAL_BT_COMMANDS --- GITHUB_SHA.h | 2 +- SerialManager.cpp | 23 +++++++++++++++++++++++ WifiESP32.cpp | 33 +++++++++++++++++++++++++-------- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h index c5dc455..777795e 100644 --- a/GITHUB_SHA.h +++ b/GITHUB_SHA.h @@ -1 +1 @@ -#define GITHUB_SHA "PORTX-HAL-20220823" +#define GITHUB_SHA "PORTX-HAL-20220828" diff --git a/SerialManager.cpp b/SerialManager.cpp index 4b6bcf5..9e14ea3 100644 --- a/SerialManager.cpp +++ b/SerialManager.cpp @@ -24,6 +24,18 @@ #include "DCCEXParser.h" #include "StringFormatter.h" +#ifdef ARDUINO_ARCH_ESP32 +#ifdef SERIAL_BT_COMMANDS +#include +//#include +#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) +#error No Bluetooth library available +#endif //ENABLED +BluetoothSerial SerialBT; +//BleSerial SerialBT; +#endif //COMMANDS +#endif //ESP32 + SerialManager * SerialManager::first=NULL; SerialManager::SerialManager(Stream * myserial) { @@ -51,6 +63,17 @@ void SerialManager::init() { Serial1.begin(115200); new SerialManager(&Serial1); #endif +#ifdef SERIAL_BT_COMMANDS + { + uint64_t chipid = ESP.getEfuseMac(); + char idstr[16] = {0}; + snprintf(idstr, 15, "DCCEX-%08X", + __builtin_bswap32((uint32_t)(chipid>>16))); + SerialBT.begin(idstr); + new SerialManager(&SerialBT); + delay(1000); + } +#endif } void SerialManager::broadcast(char * stringBuffer) { diff --git a/WifiESP32.cpp b/WifiESP32.cpp index 98b42ed..7754e11 100644 --- a/WifiESP32.cpp +++ b/WifiESP32.cpp @@ -115,6 +115,10 @@ bool WifiESP::setup(const char *SSid, bool wifiUp = false; uint8_t tries = 40; + //#ifdef SERIAL_BT_COMMANDS + //return false; + //#endif + // tests // enableCoreWDT(1); // disableCoreWDT(0); @@ -135,7 +139,11 @@ bool WifiESP::setup(const char *SSid, if (haveSSID && havePassword) { WiFi.mode(WIFI_STA); +#ifdef SERIAL_BT_COMMANDS + WiFi.setSleep(true); +#else WiFi.setSleep(false); +#endif WiFi.setAutoReconnect(true); WiFi.begin(SSid, password); while (WiFi.status() != WL_CONNECTED && tries) { @@ -178,7 +186,11 @@ bool WifiESP::setup(const char *SSid, strPass.concat(strMac); WiFi.mode(WIFI_AP); +#ifdef SERIAL_BT_COMMANDS + WiFi.setSleep(true); +#else WiFi.setSleep(false); +#endif if (WiFi.softAP(strSSID.c_str(), havePassword ? password : strPass.c_str(), channel, false, 8)) { @@ -319,14 +331,19 @@ void WifiESP::loop() { } } else if (!APmode) { // in STA mode but not connected any more // kick it again - DIAG(F("Wifi aborted with error %s. Kicking Wifi!"), wlStatus <= 6 ? wlerror[wlStatus] : "UNKNOWN"); - esp_wifi_start(); - esp_wifi_connect(); - uint8_t tries=40; - while (WiFi.status() != WL_CONNECTED && tries) { - Serial.print('.'); - tries--; - delay(500); + if (wlStatus <= 6) { + DIAG(F("Wifi aborted with error %s. Kicking Wifi!"), wlerror[wlStatus]); + esp_wifi_start(); + esp_wifi_connect(); + uint8_t tries=40; + while (WiFi.status() != WL_CONNECTED && tries) { + Serial.print('.'); + tries--; + delay(500); + } + } else { + // all well, probably + //DIAG(F("Running BT")); } }