From 00134aef4b6adc2867d0aac8d469b7bfea81863b Mon Sep 17 00:00:00 2001 From: DriftKingTW Date: Mon, 22 May 2023 00:05:33 +0800 Subject: [PATCH] feat: add Improv WiFi support --- platformio.ini | 1 + src/main.cpp | 34 +++++++++++++++++++++++++++++++++- src/main.hpp | 1 + 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index 94c2ccc..1c1cad2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -19,6 +19,7 @@ lib_deps = tinypico/TinyPICO Helper Library@^1.4.0 bblanchon/ArduinoJson@^6.19.4 xreef/PCF8574 library@^2.3.4 + jnthas/Improv WiFi Library@^0.0.1 build_flags = -D USE_NIMBLE monitor_speed = 115200 monitor_filters = esp32_exception_decoder diff --git a/src/main.cpp b/src/main.cpp index 91d2378..f1af8f0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -111,6 +111,28 @@ int contentIcon = 0; // Set web server port number to 80 WebServer server(80); +ImprovWiFi improvSerial(&Serial); + +void onImprovWiFiErrorCb(ImprovTypes::Error err) { + Serial.println("Error: " + String(err)); +} + +void onImprovWiFiConnectedCb(const char *ssid, const char *password) { + // Save ssid and password to config.json + DynamicJsonDocument doc(256); + + doc["ssid"] = ssid; + doc["password"] = password; + + File configFile = SPIFFS.open("/config.json", "w"); + if (!configFile) { + Serial.println("Failed to open config file for writing"); + } + if (serializeJson(doc, configFile) == 0) { + Serial.println("Failed to write to config file"); + } + configFile.close(); +} void setup() { Serial.begin(BAUD_RATE); @@ -135,6 +157,15 @@ void setup() { printSpacer(); + Serial.println("Starting improv serial work..."); + improvSerial.setDeviceInfo(ImprovTypes::ChipFamily::CF_ESP32, + "Schnell Firmware", "0.3.0 beta", + "Schnell Keyboard"); + improvSerial.onImprovError(onImprovWiFiErrorCb); + improvSerial.onImprovConnected(onImprovWiFiConnectedCb); + + printSpacer(); + pcf8574RotaryExtension.encoder(encoderPinA, encoderPinB); pcf8574RotaryExtension.pinMode(encoderSW, INPUT_PULLUP); pcf8574RotaryExtension.pinMode(extensionBtn1, INPUT_PULLUP); @@ -432,7 +463,8 @@ void ledTask(void *pvParameters) { void networkTask(void *pvParameters) { while (true) { server.handleClient(); - vTaskDelay(100 / portTICK_PERIOD_MS); + improvSerial.handleSerial(); + vTaskDelay(50 / portTICK_PERIOD_MS); } } diff --git a/src/main.hpp b/src/main.hpp index 3278484..ad0e1b1 100644 --- a/src/main.hpp +++ b/src/main.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include