From 817c144bc206b06899ebe005b0118fb680d2d9dd Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 10 Aug 2021 16:05:39 +0200 Subject: [PATCH 01/14] Added WiFi configuration via BLE --- main/ble.h | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/main/ble.h b/main/ble.h index 40a6080..f2df43d 100644 --- a/main/ble.h +++ b/main/ble.h @@ -1,8 +1,10 @@ #pragma once +#define FEATURE_BLE +#define FEATURE_WIRELESS_CONFIG + // esp-idf includes #include - // 3rdparty lib includes #include #ifdef FEATURE_BLE @@ -20,6 +22,9 @@ BLEServer *pServer{}; BLEService *pService{}; BLECharacteristic *livestatsCharacteristic{}; BLECharacteristic *remotecontrolCharacteristic{}; +#ifdef FEATURE_WIRELESS_CONFIG +BLECharacteristic *wirelessConfig{}; +#endif void createBle(); void destroyBle(); @@ -30,8 +35,20 @@ public: void onWrite(NimBLECharacteristic* pCharacteristic) override; }; +#ifdef FEATURE_WIRELESS_CONFIG +class WirelessSettingsCallbacks : public NimBLECharacteristicCallbacks +{ +public: + void onWrite(NimBLECharacteristic* pCharacteristic) override; +}; +#endif + RemoteControlCallbacks bleRemoteCallbacks; +#ifdef FEATURE_WIRELESS_CONFIG +WirelessSettingsCallbacks bleWirelessSettingsCallbacks; +#endif + void initBle() { if (settings.bleSettings.bleEnabled) @@ -150,6 +167,22 @@ void handleBle() livestatsCharacteristic->setValue(json); livestatsCharacteristic->notify(); } + +#ifdef FEATURE_WIRELESS_CONFIG + + if (wirelessConfig->getSubscribedCount()) + { + StaticJsonDocument<1024> doc; + { + } + + std::string json; + serializeJson(doc, json); + + livestatsCharacteristic->setValue(json); + livestatsCharacteristic->notify(); + } +#endif } else if (pServer) { @@ -172,6 +205,10 @@ void createBle() livestatsCharacteristic = pService->createCharacteristic("a48321ea-329f-4eab-a401-30e247211524", NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY); remotecontrolCharacteristic = pService->createCharacteristic("4201def0-a264-43e6-946b-6b2d9612dfed", NIMBLE_PROPERTY::WRITE); remotecontrolCharacteristic->setCallbacks(&bleRemoteCallbacks); +#ifdef FEATURE_WIRELESS_CONFIG + wirelessConfig = pService->createCharacteristic("4201def1-a264-43e6-946b-6b2d9612dfed", NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE); + wirelessConfig->setCallbacks(&bleWirelessSettingsCallbacks); +#endif pService->start(); @@ -191,6 +228,7 @@ void destroyBle() pService = {}; livestatsCharacteristic = {}; remotecontrolCharacteristic = {}; + wirelessConfig = {}; } void RemoteControlCallbacks::onWrite(NimBLECharacteristic* pCharacteristic) @@ -211,5 +249,31 @@ void RemoteControlCallbacks::onWrite(NimBLECharacteristic* pCharacteristic) .backRight = doc["br"].as() }); } + +#ifdef FEATURE_WIRELESS_CONFIG +void WirelessSettingsCallbacks::onWrite(NimBLECharacteristic* pCharacteristic) +{ + const auto &val = pCharacteristic->getValue(); + + StaticJsonDocument<256> doc; + if (const auto error = deserializeJson(doc, val)) + { + ESP_LOGW(TAG, "ignoring cmd with invalid json: %.*s %s", val.size(), val.data(), error.c_str()); + return; + } + + const char* write_type = doc["type"].as(); + + if (strcmp(write_type, "wifi") == 0) { + const int index = doc["wifi_index"].as(); + ESP_LOGI(TAG, "Set wifi%i: WiFi-SSID: %s, WiFi-Password: %s", doc["wifi_index"].as(), doc["wifi_ssid"].as(), doc["wifi_pass"].as()); + stringSettings.wifis[index].ssid = doc["wifi_ssid"].as(); + stringSettings.wifis[index].key = doc["wifi_pass"].as(); + } else { + const auto deserialized = deserializeJson(doc, val); + ESP_LOGW(TAG, "Unkown type %s -> json: %.*s %s", doc["type"].as(), val.size(), val.data(), deserialized.c_str()); + } +} +#endif #endif } From db3be639df2521421f59b21bfca2acf878ae6037 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 10 Aug 2021 16:28:36 +0200 Subject: [PATCH 02/14] Removed password from debug log --- main/ble.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/main/ble.h b/main/ble.h index f2df43d..36335d2 100644 --- a/main/ble.h +++ b/main/ble.h @@ -174,13 +174,14 @@ void handleBle() { StaticJsonDocument<1024> doc; { + doc.add(42); } std::string json; serializeJson(doc, json); - livestatsCharacteristic->setValue(json); - livestatsCharacteristic->notify(); + wirelessConfig->setValue(json); + wirelessConfig->notify(); } #endif } @@ -266,7 +267,7 @@ void WirelessSettingsCallbacks::onWrite(NimBLECharacteristic* pCharacteristic) if (strcmp(write_type, "wifi") == 0) { const int index = doc["wifi_index"].as(); - ESP_LOGI(TAG, "Set wifi%i: WiFi-SSID: %s, WiFi-Password: %s", doc["wifi_index"].as(), doc["wifi_ssid"].as(), doc["wifi_pass"].as()); + ESP_LOGI(TAG, "[ble_config]: Set wifi%i: WiFi-SSID: %s, WiFi-Password: ***", doc["wifi_index"].as(), doc["wifi_ssid"].as()); stringSettings.wifis[index].ssid = doc["wifi_ssid"].as(); stringSettings.wifis[index].key = doc["wifi_pass"].as(); } else { From 290531562db1428cee9d2facbfabe984afead18a Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 10 Aug 2021 16:44:25 +0200 Subject: [PATCH 03/14] Added saveSettings() Forgot to push settings from RAM to NVS --- main/ble.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main/ble.h b/main/ble.h index 36335d2..b592085 100644 --- a/main/ble.h +++ b/main/ble.h @@ -15,6 +15,7 @@ #include "globals.h" #include "futurecpp.h" #include "modes/remotecontrolmode.h" +#include "utils.h" namespace { #ifdef FEATURE_BLE @@ -270,6 +271,7 @@ void WirelessSettingsCallbacks::onWrite(NimBLECharacteristic* pCharacteristic) ESP_LOGI(TAG, "[ble_config]: Set wifi%i: WiFi-SSID: %s, WiFi-Password: ***", doc["wifi_index"].as(), doc["wifi_ssid"].as()); stringSettings.wifis[index].ssid = doc["wifi_ssid"].as(); stringSettings.wifis[index].key = doc["wifi_pass"].as(); + saveSettings(); } else { const auto deserialized = deserializeJson(doc, val); ESP_LOGW(TAG, "Unkown type %s -> json: %.*s %s", doc["type"].as(), val.size(), val.data(), deserialized.c_str()); From a1a99bd4cfa40bbf9bc8c533cbd8c1d6cb680363 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 10 Aug 2021 17:01:07 +0200 Subject: [PATCH 04/14] Cleanup --- main/ble.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/main/ble.h b/main/ble.h index b592085..9c3c2a3 100644 --- a/main/ble.h +++ b/main/ble.h @@ -1,8 +1,5 @@ #pragma once -#define FEATURE_BLE -#define FEATURE_WIRELESS_CONFIG - // esp-idf includes #include // 3rdparty lib includes From e46e2b7e01878e569e3457d7e674381cf8e8d0ad Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 10 Aug 2021 18:53:49 +0200 Subject: [PATCH 05/14] Added newline for @0xFEEDC0DE64 --- main/ble.h | 1 + 1 file changed, 1 insertion(+) diff --git a/main/ble.h b/main/ble.h index 9c3c2a3..58d0fe6 100644 --- a/main/ble.h +++ b/main/ble.h @@ -2,6 +2,7 @@ // esp-idf includes #include + // 3rdparty lib includes #include #ifdef FEATURE_BLE From 63ef389aaa7e1e71e461b8a5166d765cbd7d4b5a Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 10 Aug 2021 23:26:19 +0200 Subject: [PATCH 06/14] Updated to meet requirements --- main/ble.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/main/ble.h b/main/ble.h index 58d0fe6..8d0eaf3 100644 --- a/main/ble.h +++ b/main/ble.h @@ -9,6 +9,9 @@ #include #endif +#define FEATURE_BLE +#define FEATURE_WIRELESS_CONFIG + // local includes #include "globals.h" #include "futurecpp.h" @@ -262,9 +265,9 @@ void WirelessSettingsCallbacks::onWrite(NimBLECharacteristic* pCharacteristic) return; } - const char* write_type = doc["type"].as(); + auto write_type = doc["type"].as(); - if (strcmp(write_type, "wifi") == 0) { + if (write_type == "wifi") { const int index = doc["wifi_index"].as(); ESP_LOGI(TAG, "[ble_config]: Set wifi%i: WiFi-SSID: %s, WiFi-Password: ***", doc["wifi_index"].as(), doc["wifi_ssid"].as()); stringSettings.wifis[index].ssid = doc["wifi_ssid"].as(); From e68d8389a2354c5d7a2b935406cb71e1ed0b2e9e Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 10 Aug 2021 23:28:22 +0200 Subject: [PATCH 07/14] Updated to meet requirements --- main/ble.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/ble.h b/main/ble.h index 8d0eaf3..991ba9d 100644 --- a/main/ble.h +++ b/main/ble.h @@ -270,8 +270,8 @@ void WirelessSettingsCallbacks::onWrite(NimBLECharacteristic* pCharacteristic) if (write_type == "wifi") { const int index = doc["wifi_index"].as(); ESP_LOGI(TAG, "[ble_config]: Set wifi%i: WiFi-SSID: %s, WiFi-Password: ***", doc["wifi_index"].as(), doc["wifi_ssid"].as()); - stringSettings.wifis[index].ssid = doc["wifi_ssid"].as(); - stringSettings.wifis[index].key = doc["wifi_pass"].as(); + stringSettings.wifis[index].ssid = doc["wifi_ssid"].as(); + stringSettings.wifis[index].key = doc["wifi_pass"].as(); saveSettings(); } else { const auto deserialized = deserializeJson(doc, val); From ddee5741295357db1736f90b01441c92c11d3fb9 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Fri, 13 Aug 2021 15:32:42 +0200 Subject: [PATCH 08/14] Added files to ignore list --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index bdae45c..a6d619b 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ desktop.ini /sdkconfig.old* /.idea /.ccache +flash.sh +app-flash.sh From 43c32d56d5b6e6bf0d1626a9c4d59afe5c985d72 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Fri, 13 Aug 2021 15:33:55 +0200 Subject: [PATCH 09/14] Removed defines, removed code from handler --- main/ble.h | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/main/ble.h b/main/ble.h index 991ba9d..724e047 100644 --- a/main/ble.h +++ b/main/ble.h @@ -9,9 +9,6 @@ #include #endif -#define FEATURE_BLE -#define FEATURE_WIRELESS_CONFIG - // local includes #include "globals.h" #include "futurecpp.h" @@ -169,23 +166,6 @@ void handleBle() livestatsCharacteristic->setValue(json); livestatsCharacteristic->notify(); } - -#ifdef FEATURE_WIRELESS_CONFIG - - if (wirelessConfig->getSubscribedCount()) - { - StaticJsonDocument<1024> doc; - { - doc.add(42); - } - - std::string json; - serializeJson(doc, json); - - wirelessConfig->setValue(json); - wirelessConfig->notify(); - } -#endif } else if (pServer) { @@ -209,7 +189,7 @@ void createBle() remotecontrolCharacteristic = pService->createCharacteristic("4201def0-a264-43e6-946b-6b2d9612dfed", NIMBLE_PROPERTY::WRITE); remotecontrolCharacteristic->setCallbacks(&bleRemoteCallbacks); #ifdef FEATURE_WIRELESS_CONFIG - wirelessConfig = pService->createCharacteristic("4201def1-a264-43e6-946b-6b2d9612dfed", NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE); + wirelessConfig = pService->createCharacteristic("4201def1-a264-43e6-946b-6b2d9612dfed", NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::NOTIFY); wirelessConfig->setCallbacks(&bleWirelessSettingsCallbacks); #endif From 25b9716e1ff60f32552777c7d20c7289921fec7f Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Sun, 15 Aug 2021 19:55:57 +0200 Subject: [PATCH 10/14] Added wifi list to ble capabilities --- main/ble.h | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/main/ble.h b/main/ble.h index 724e047..5f6b316 100644 --- a/main/ble.h +++ b/main/ble.h @@ -15,6 +15,9 @@ #include "modes/remotecontrolmode.h" #include "utils.h" +//wifistack +#include "wifi_bobbycar.h" + namespace { #ifdef FEATURE_BLE BLEServer *pServer{}; @@ -23,6 +26,7 @@ BLECharacteristic *livestatsCharacteristic{}; BLECharacteristic *remotecontrolCharacteristic{}; #ifdef FEATURE_WIRELESS_CONFIG BLECharacteristic *wirelessConfig{}; +BLECharacteristic *getwifilist{}; #endif void createBle(); @@ -40,12 +44,19 @@ class WirelessSettingsCallbacks : public NimBLECharacteristicCallbacks public: void onWrite(NimBLECharacteristic* pCharacteristic) override; }; + +class WiFiListCallbacks : public NimBLECharacteristicCallbacks +{ +public: + void onRead(NimBLECharacteristic* pCharacteristic) override; +}; #endif RemoteControlCallbacks bleRemoteCallbacks; #ifdef FEATURE_WIRELESS_CONFIG WirelessSettingsCallbacks bleWirelessSettingsCallbacks; +WiFiListCallbacks bleWiFiListCallbacks; #endif void initBle() @@ -189,8 +200,10 @@ void createBle() remotecontrolCharacteristic = pService->createCharacteristic("4201def0-a264-43e6-946b-6b2d9612dfed", NIMBLE_PROPERTY::WRITE); remotecontrolCharacteristic->setCallbacks(&bleRemoteCallbacks); #ifdef FEATURE_WIRELESS_CONFIG - wirelessConfig = pService->createCharacteristic("4201def1-a264-43e6-946b-6b2d9612dfed", NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::NOTIFY); + wirelessConfig = pService->createCharacteristic("4201def1-a264-43e6-946b-6b2d9612dfed", NIMBLE_PROPERTY::WRITE); wirelessConfig->setCallbacks(&bleWirelessSettingsCallbacks); + getwifilist = pService->createCharacteristic("4201def2-a264-43e6-946b-6b2d9612dfed", NIMBLE_PROPERTY::READ); + getwifilist->setCallbacks(&bleWiFiListCallbacks); #endif pService->start(); @@ -212,6 +225,7 @@ void destroyBle() livestatsCharacteristic = {}; remotecontrolCharacteristic = {}; wirelessConfig = {}; + getwifilist = {}; } void RemoteControlCallbacks::onWrite(NimBLECharacteristic* pCharacteristic) @@ -258,6 +272,28 @@ void WirelessSettingsCallbacks::onWrite(NimBLECharacteristic* pCharacteristic) ESP_LOGW(TAG, "Unkown type %s -> json: %.*s %s", doc["type"].as(), val.size(), val.data(), deserialized.c_str()); } } + +void WiFiListCallbacks::onRead(NimBLECharacteristic *pCharacteristic) { + StaticJsonDocument<1024> responseDoc; + auto wifis = stringSettings.wifis; + auto wifiArray = responseDoc.createNestedArray("wifis"); + ESP_LOGI(TAG, "[ble_wifilist] Got request for listing wifi ssids."); + for (unsigned int index = 0; index < wifis.size(); index++) { + wifiArray.add(wifis[index].ssid); + } +/* + if (const auto result = wifi_stack::get_ip_info(TCPIP_ADAPTER_IF_STA); result) + responseDoc["ip"] = wifi_stack::toString(result->ip); + else + { + ESP_LOGW(TAG, "[ble_wifilist] get_ip_info() failed with %.*s", result.error().size(), result.error().data()); + } +*/ + + std::string json; + serializeJson(responseDoc, json); + pCharacteristic->setValue(json); +} #endif #endif } From 77b6f2f013d1408f9f9beeaebf1abd970920a897 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Wed, 25 Aug 2021 05:36:13 +0200 Subject: [PATCH 11/14] Removed IP, adjusted size of jsondocument --- main/ble.h | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/main/ble.h b/main/ble.h index 5f6b316..cb9abe8 100644 --- a/main/ble.h +++ b/main/ble.h @@ -274,22 +274,14 @@ void WirelessSettingsCallbacks::onWrite(NimBLECharacteristic* pCharacteristic) } void WiFiListCallbacks::onRead(NimBLECharacteristic *pCharacteristic) { - StaticJsonDocument<1024> responseDoc; + StaticJsonDocument<768> responseDoc; auto wifis = stringSettings.wifis; auto wifiArray = responseDoc.createNestedArray("wifis"); ESP_LOGI(TAG, "[ble_wifilist] Got request for listing wifi ssids."); for (unsigned int index = 0; index < wifis.size(); index++) { wifiArray.add(wifis[index].ssid); } -/* - if (const auto result = wifi_stack::get_ip_info(TCPIP_ADAPTER_IF_STA); result) - responseDoc["ip"] = wifi_stack::toString(result->ip); - else - { - ESP_LOGW(TAG, "[ble_wifilist] get_ip_info() failed with %.*s", result.error().size(), result.error().data()); - } -*/ - + responseDoc["wifi_count"] = wifis.size(); std::string json; serializeJson(responseDoc, json); pCharacteristic->setValue(json); From fe81facbdb1ab21fb9ff0aa2fb96ab96852042a9 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Thu, 26 Aug 2021 22:18:33 +0200 Subject: [PATCH 12/14] Added script for opening serial monitor --- monitor.sh | 2 ++ 1 file changed, 2 insertions(+) create mode 100755 monitor.sh diff --git a/monitor.sh b/monitor.sh new file mode 100755 index 0000000..ea93661 --- /dev/null +++ b/monitor.sh @@ -0,0 +1,2 @@ +#!/bin/bash +idf.py -p /dev/ttyUSB0 -b 921600 monitor From b6f3c4c069ae9cc6bd6c9163b9dc7e967f49e166 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Thu, 26 Aug 2021 22:22:46 +0200 Subject: [PATCH 13/14] Added helper scripts --- .gitignore | 2 -- app-flash.sh | 2 ++ flash.sh | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) create mode 100755 app-flash.sh create mode 100755 flash.sh diff --git a/.gitignore b/.gitignore index a6d619b..bdae45c 100644 --- a/.gitignore +++ b/.gitignore @@ -9,5 +9,3 @@ desktop.ini /sdkconfig.old* /.idea /.ccache -flash.sh -app-flash.sh diff --git a/app-flash.sh b/app-flash.sh new file mode 100755 index 0000000..50e0112 --- /dev/null +++ b/app-flash.sh @@ -0,0 +1,2 @@ +#!/bin/bash +idf.py -p /dev/ttyUSB0 -b 921600 app-flash monitor diff --git a/flash.sh b/flash.sh new file mode 100755 index 0000000..b7be12b --- /dev/null +++ b/flash.sh @@ -0,0 +1,2 @@ +#!/bin/bash +idf.py -p /dev/ttyUSB0 -b 921600 flash monitor From f0192c27e891849e358db730b610314fbe4e2405 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Thu, 26 Aug 2021 22:26:43 +0200 Subject: [PATCH 14/14] Forgot ifdef --- main/ble.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main/ble.h b/main/ble.h index cb9abe8..6c78838 100644 --- a/main/ble.h +++ b/main/ble.h @@ -224,8 +224,10 @@ void destroyBle() pService = {}; livestatsCharacteristic = {}; remotecontrolCharacteristic = {}; +#ifdef FEATURE_WIRELESS_CONFIG wirelessConfig = {}; getwifilist = {}; +#endif } void RemoteControlCallbacks::onWrite(NimBLECharacteristic* pCharacteristic)