Skip to content
C

wifi setup support

YU CHIH CHANG705e0e6905d0
diff --git a/platformio.ini b/platformio.ini
index 6355b2d..104fd87 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -18,7 +18,7 @@ lib_deps =
noah1510/LedController @ ^1.7.0
locoduino/RingBuffer@^1.0.4
https://github.com/maxint-rd/TM16xx#95a1bdd959bf6fce75d0059f129386155f49f984
- https://github.com/khoih-prog/ESPAsync_WiFiManager
+ tzapu/WiFiManager@^2.0.17
makuna/NeoPixelBus
#####
@@ -85,7 +85,7 @@ lib_deps =
https://github.com/MacManley/gt7-udp.git
locoduino/RingBuffer@^1.0.4
https://github.com/paulo-raca/ArduinoBufferedStreams.git#5e3a1a3d140955384a07878c64808e77fa2a7521
- https://github.com/khoih-prog/ESPAsync_WiFiManager
+ tzapu/WiFiManager@^2.0.17
#lemmingdev/ESP32-BLE-Gamepad@^0.5.2 #liberia per bluettoth
#h2zero/NimBLE-Arduino@^1.4.0 #liberia nible usata per bluettoth
diff --git a/src/SHCustomProtocol.h b/src/SHCustomProtocol.h
index e3ee0a2..158c76f 100644
--- a/src/SHCustomProtocol.h
+++ b/src/SHCustomProtocol.h
@@ -15,6 +15,7 @@
static LGFX tft;
+static const int RESET_WAITING_TIME = 3;
static const int SCREEN_WIDTH = 320;
static const int SCREEN_HEIGHT = 240;
static const int X_CENTER = SCREEN_WIDTH / 2;
@@ -36,6 +37,49 @@ std::map<String, int32_t> prevColor;
BleGamepad bleGamepad("ESP32 Touch Gamepad", "YourCompany", 100);
BleGamepadConfiguration bleGamepadConfig;
+static void showWifiConnectionFailedScreen()
+{
+ tft.fillScreen(TFT_BLACK);
+ tft.setTextDatum(MC_DATUM);
+
+ tft.setTextColor(TFT_RED);
+ tft.drawString("WI-FI FAILED", 160, 75);
+
+ tft.setTextColor(TFT_WHITE);
+ tft.drawString("Check your password", 160, 125);
+ tft.drawString("and try again", 160, 155);
+
+ tft.setTextColor(TFT_LIGHTGREY);
+ tft.drawString("Setup portal will restart", 160, 205);
+}
+
+static void showWifiSetupScreen()
+{
+ tft.fillScreen(TFT_BLACK);
+
+ tft.setTextDatum(MC_DATUM);
+
+ tft.setTextColor(TFT_CYAN, TFT_BLACK);
+ tft.setTextSize(2);
+ tft.drawString("WI-FI SETUP", 160, 35);
+
+ tft.setTextColor(TFT_WHITE, TFT_BLACK);
+ tft.setTextSize(1.5);
+ tft.drawString("Connect phone to:", 160, 85);
+
+ tft.setTextColor(TFT_YELLOW, TFT_BLACK);
+ tft.setTextSize(2);
+ tft.drawString("GT7-DASH-SETUP", 160, 120);
+
+ tft.setTextColor(TFT_WHITE, TFT_BLACK);
+ tft.setTextSize(1.5);
+ tft.drawString("Open:", 160, 160);
+
+ tft.setTextColor(TFT_GREEN, TFT_BLACK);
+ tft.setTextSize(2);
+ tft.drawString("192.168.4.1", 160, 195);
+}
+
int currentPage = 1; // Variabile per tenere traccia della pagina corrente
bool forceUpdate = false; // Variabile per forzare l'aggiornamento delle celle
@@ -89,9 +133,14 @@ private:
bool gameStoppedTimerStarted = false;
bool screenSleeping = false;
- // 是否由使用者長按手動關閉
+ // 是否由使用者手動關閉
bool screenOffByUser = false;
+ // 全螢幕長按 5 秒後顯示 Wi-Fi 重設確認畫面
+ static constexpr unsigned long WIFI_RESET_HOLD_MS = 5000UL;
+ bool wifiResetConfirmOpen = false;
+ bool wifiResetRequested = false;
+
// 記錄上一筆 GT7 執行狀態,用來偵測 False -> True
bool previousGameRunning = false;
@@ -226,32 +275,30 @@ public:
// Called when starting the arduino (setup method in main sketch)
void setup()
{
- // Serial.begin(115200); //x debug
- // Serial.println("Test seriale avviato!"); //x debug
-
tft.init();
-
tft.setRotation(3);
tft.setBrightness(SCREEN_NORMAL_BRIGHTNESS);
currentBrightness = SCREEN_NORMAL_BRIGHTNESS;
tft.fillScreen(TFT_BLACK);
screenSleeping = false;
+ // Serial.begin(115200); //x debug
+ // Serial.println("Test seriale avviato!"); //x debug
gameStoppedTimerStarted = false;
#if INCLUDE_GT7_WIFI
derivedMetrics.reset();
#endif
- bleGamepadConfig.setAutoReport(true); // in false non invia i comandi a windows
- bleGamepadConfig.setAxesMax(32760);
- bleGamepadConfig.setIncludeSlider1(false);
- bleGamepadConfig.setIncludeXAxis(false);
- bleGamepadConfig.setIncludeYAxis(false);
- bleGamepadConfig.setIncludeZAxis(false);
- bleGamepadConfig.setIncludeRxAxis(false);
- bleGamepadConfig.setIncludeRyAxis(false);
- bleGamepadConfig.setIncludeRzAxis(false);
- bleGamepadConfig.setButtonCount(numButtons); // Variabile per il numero di pulsanti
- bleGamepad.begin(&bleGamepadConfig);
+ // bleGamepadConfig.setAutoReport(true); // in false non invia i comandi a windows
+ // bleGamepadConfig.setAxesMax(32760);
+ // bleGamepadConfig.setIncludeSlider1(false);
+ // bleGamepadConfig.setIncludeXAxis(false);
+ // bleGamepadConfig.setIncludeYAxis(false);
+ // bleGamepadConfig.setIncludeZAxis(false);
+ // bleGamepadConfig.setIncludeRxAxis(false);
+ // bleGamepadConfig.setIncludeRyAxis(false);
+ // bleGamepadConfig.setIncludeRzAxis(false);
+ // bleGamepadConfig.setButtonCount(numButtons); // Variabile per il numero di pulsanti
+ // bleGamepad.begin(&bleGamepadConfig);
// Serial.println("Configurazione BleGamepad completata."); //x debug
}
@@ -292,8 +339,8 @@ public:
gt7Telem.getCurrentGearFromByte();
gear = currentGear == 0
- ? "N"
- : String(currentGear);
+ ? "N"
+ : String(currentGear);
// RPM Bar
const float rpm = data.EngineRPM;
@@ -336,21 +383,21 @@ public:
if (totalLapCount > 0)
{
- tyrePressureRearLeft = String(currentLap) + "/" + String(totalLapCount);
+ tyrePressureRearLeft = String(currentLap) + "/" + String(totalLapCount);
}
else
{
- tyrePressureRearLeft = String(currentLap);
+ tyrePressureRearLeft = String(currentLap);
}
// 目前封包只有賽前起跑位置,沒有比賽中的即時排名
if (data.RaceStartPosition > 0)
{
- tyrePressureFrontRight = String(data.RaceStartPosition);
+ tyrePressureFrontRight = String(data.RaceStartPosition);
}
else
{
- tyrePressureFrontRight = "--";
+ tyrePressureFrontRight = "--";
}
// 油量百分比
@@ -413,8 +460,8 @@ public:
millis());
absActive = derivedMetrics.abs.isActive()
- ? "True"
- : "False";
+ ? "True"
+ : "False";
isTCCutNull = "True";
tcTcCut = "0";
@@ -581,7 +628,7 @@ public:
{
const bool telemetryIsAlive =
lastGT7PacketTime != 0 &&
- millis() - lastGT7PacketTime < 3000;
+ millis() - lastGT7PacketTime < RESET_WAITING_TIME * 1000;
// 不只要求 UDP 還活著,也要求車輛真的在賽道上。
// 因此離開賽道但 GT7 仍持續送封包時,也會切回等待畫面。
@@ -659,9 +706,15 @@ public:
fadeScreenOff();
}
- // 暗屏時仍需讀取觸控,才能點擊喚醒。
+ // 暗屏時仍需讀取觸控,才能點擊喚醒或長按進入 Wi-Fi 重設。
readTouch();
+ // 確認畫面開啟時,不讓 Dashboard 或 Connecting 畫面蓋回來。
+ if (wifiResetConfirmOpen)
+ {
+ return;
+ }
+
if (screenSleeping)
{
return;
@@ -749,7 +802,24 @@ public:
barHeight,
tft.color565(28, 28, 28));
- tft.setTextDatum(TL_DATUM);
+ char text[40];
+ snprintf(
+ text,
+ sizeof(text),
+ "Touch & hold %ds to change WiFi",
+ RESET_WAITING_TIME);
+
+ tft.drawCentreString(
+ text,
+ SCREEN_WIDTH / 2,
+ SCREEN_HEIGHT - 26,
+ 1);
+
+ tft.drawCentreString(
+ "v1.2.0 | by caa1211",
+ SCREEN_WIDTH / 2,
+ SCREEN_HEIGHT - 12,
+ 1);
}
void drawConnectingBar()
@@ -913,9 +983,9 @@ public:
if (sessionBestLiveDeltaSeconds != "--")
{
lastBestColor = sessionBestLiveDeltaSeconds.startsWith("+") &&
- sessionBestLiveDeltaSeconds != "+0.000"
- ? TFT_RED
- : TFT_GREEN;
+ sessionBestLiveDeltaSeconds != "+0.000"
+ ? TFT_RED
+ : TFT_GREEN;
}
drawCell(
@@ -1484,15 +1554,15 @@ public:
const int32_t brakeY = y + cellHeight + gap;
auto updateOneCell = [&](
- int32_t cellY,
- int percent,
- const char *id,
- uint16_t color)
+ int32_t cellY,
+ int percent,
+ const char *id,
+ uint16_t color)
{
const bool firstDraw = prevData.count(id) == 0;
const int oldPercent = firstDraw
- ? 0
- : constrain(prevData[id].toInt(), 0, 100);
+ ? 0
+ : constrain(prevData[id].toInt(), 0, 100);
if (!forceUpdate && !firstDraw && oldPercent == percent)
{
@@ -1744,37 +1814,174 @@ public:
fadeToBrightness(0);
}
+ void redrawAfterWifiResetDialog()
+ {
+ tft.fillScreen(TFT_BLACK);
+ prevData.clear();
+ prevColor.clear();
+ prev_gear = "";
+ prev_rpmPercent = 50;
+ connectingScreenActive = false;
+ connectingAnimationStep = 0;
+ forceUpdate = true;
+ }
+
+ void showWifiResetConfirm()
+ {
+ // 即使原本處於暗屏,也要先亮起確認畫面。
+ screenSleeping = false;
+ screenOffByUser = false;
+ tft.setBrightness(SCREEN_NORMAL_BRIGHTNESS);
+ currentBrightness = SCREEN_NORMAL_BRIGHTNESS;
+
+ tft.fillScreen(TFT_BLACK);
+ tft.setTextPadding(0);
+ tft.setTextDatum(MC_DATUM);
+
+ tft.setTextColor(TFT_WHITE, TFT_BLACK);
+ tft.drawString("Reset saved Wi-Fi?", SCREEN_WIDTH / 2, 60, 4);
+
+ tft.setTextColor(TFT_DARKGREY, TFT_BLACK);
+ tft.drawString("Device will restart", SCREEN_WIDTH / 2, 95, 2);
+
+ const int buttonY = 135;
+ const int buttonHeight = 62;
+ const int buttonWidth = 120;
+ const int noX = 25;
+ const int yesX = SCREEN_WIDTH - 25 - buttonWidth;
+
+ tft.fillRoundRect(noX, buttonY, buttonWidth, buttonHeight, 8, TFT_DARKGREY);
+ tft.drawRoundRect(noX, buttonY, buttonWidth, buttonHeight, 8, TFT_WHITE);
+ tft.setTextColor(TFT_WHITE, TFT_DARKGREY);
+ tft.drawString("NO", noX + buttonWidth / 2, buttonY + buttonHeight / 2, 4);
+
+ tft.fillRoundRect(yesX, buttonY, buttonWidth, buttonHeight, 8, TFT_RED);
+ tft.drawRoundRect(yesX, buttonY, buttonWidth, buttonHeight, 8, TFT_WHITE);
+ tft.setTextColor(TFT_WHITE, TFT_RED);
+ tft.drawString("YES", yesX + buttonWidth / 2, buttonY + buttonHeight / 2, 4);
+
+ tft.setTextDatum(TL_DATUM);
+ }
+
+ void showWifiResettingScreen()
+ {
+ tft.fillScreen(TFT_BLACK);
+ tft.setTextPadding(0);
+ tft.setTextDatum(MC_DATUM);
+
+ tft.setTextColor(TFT_CYAN, TFT_BLACK);
+ tft.drawString("WI-FI RESET", SCREEN_WIDTH / 2, 90, 4);
+
+ tft.setTextColor(TFT_WHITE, TFT_BLACK);
+ tft.drawString("Restarting...", SCREEN_WIDTH / 2, 145, 2);
+
+ tft.setTextDatum(TL_DATUM);
+ }
+
+ bool takeWifiResetRequest()
+ {
+ if (!wifiResetRequested)
+ {
+ return false;
+ }
+
+ wifiResetRequested = false;
+ return true;
+ }
+
void readTouch()
{
- /*
- * 整體關閉觸控開關屏功能時,
- * 不讀取觸控,也不做任何亮屏或關屏動作。
- */
if (!TOUCH_SCREEN_CONTROL_ENABLED)
{
return;
}
static bool wasTouched = false;
+ static bool longPressTriggered = false;
+ static bool waitForReleaseAfterDialog = false;
+ static unsigned long touchStartTime = 0;
- const bool isTouched =
- tft.getTouch(&touchX, &touchY);
+ const bool isTouched = tft.getTouch(&touchX, &touchY);
- /*
- * 只處理手指剛碰到螢幕的瞬間。
- */
+ // 長按開啟確認畫面後,必須先放開,避免同一次觸控誤按 YES/NO。
+ if (waitForReleaseAfterDialog)
+ {
+ if (!isTouched)
+ {
+ waitForReleaseAfterDialog = false;
+ wasTouched = false;
+ }
+ return;
+ }
+
+ // 確認畫面中的按鈕處理。
+ if (wifiResetConfirmOpen)
+ {
+ if (isTouched && !wasTouched)
+ {
+ touchStartTime = millis();
+ }
+
+ if (!isTouched && wasTouched)
+ {
+ const int buttonY = 135;
+ const int buttonHeight = 62;
+ const int buttonWidth = 120;
+ const int noX = 25;
+ const int yesX = SCREEN_WIDTH - 25 - buttonWidth;
+
+ const bool noPressed =
+ touchX >= noX && touchX < noX + buttonWidth &&
+ touchY >= buttonY && touchY < buttonY + buttonHeight;
+
+ const bool yesPressed =
+ touchX >= yesX && touchX < yesX + buttonWidth &&
+ touchY >= buttonY && touchY < buttonY + buttonHeight;
+
+ if (yesPressed)
+ {
+ showWifiResettingScreen();
+ wifiResetRequested = true;
+ }
+ else if (noPressed)
+ {
+ wifiResetConfirmOpen = false;
+ redrawAfterWifiResetDialog();
+ }
+ }
+
+ wasTouched = isTouched;
+ return;
+ }
+
+ // 手指剛碰到螢幕:開始計算長按時間。
if (isTouched && !wasTouched)
+ {
+ touchStartTime = millis();
+ longPressTriggered = false;
+ }
+
+ // 全螢幕持續按住 6 秒:顯示 Wi-Fi 重設確認畫面。
+ if (isTouched &&
+ !longPressTriggered &&
+ millis() - touchStartTime >= WIFI_RESET_HOLD_MS)
+ {
+ longPressTriggered = true;
+ wifiResetConfirmOpen = true;
+ showWifiResetConfirm();
+ waitForReleaseAfterDialog = true;
+ wasTouched = isTouched;
+ return;
+ }
+
+ // 未達 6 秒便放開:維持原本的短按亮屏/熄屏功能。
+ if (!isTouched && wasTouched && !longPressTriggered)
{
if (screenSleeping)
{
- /*
- * 暗屏時點一下:淡入亮屏
- */
screenSleeping = false;
screenOffByUser = false;
-
fadeScreenOn();
-
forceUpdate = true;
if (!previousGameRunning)
@@ -1789,14 +1996,9 @@ public:
}
else
{
- /*
- * 亮屏時點一下:淡出關屏
- */
screenOffByUser = true;
screenSleeping = true;
-
fadeScreenOff();
-
gameStoppedTimerStarted = false;
}
}
diff --git a/src/main.cpp b/src/main.cpp
index dca2ab2..2aa9ca4 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -35,8 +35,10 @@ FullLoopbackStream incomingStream;
#define INCLUDE_GT7_WIFI true
#if INCLUDE_GT7_WIFI
#include <WiFi.h>
- #define GT7_WIFI_SSID "caahome_3fb 2.4G"
- #define GT7_WIFI_PASSWORD "caacaacaac"
+ #include <WiFiManager.h>
+
+ // WiFiManager captive portal shown only when no saved Wi-Fi can be used.
+ static constexpr const char *GT7_SETUP_AP_NAME = "GT7-DASH-SETUP";
#endif
#define DEVICE_NAME "HelloWorldEsp" //{"Group":"General","Name":"DEVICE_NAME","Title":"Device name,\r\n make sure to use a unique name when using multiple arduinos","DefaultValue":"SimHub Dash","Type":"string","Template":"#define DEVICE_NAME \"{0}\""}
@@ -1120,6 +1122,8 @@ void buttonMatrixStatusChanged(int buttonId, byte Status) {
}
#endif
+
+
void setup()
{
#if INCLUDE_WIFI
@@ -1145,25 +1149,7 @@ void setup()
#else
FlowSerialBegin(19200);
#endif
- delay(1000);
-#if INCLUDE_GT7_WIFI
- delay(1000);
- WiFi.mode(WIFI_STA);
- WiFi.begin(GT7_WIFI_SSID, GT7_WIFI_PASSWORD);
- Serial.print("Connecting to WiFi");
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- Serial.print("ESP32 IP: ");
- Serial.println(WiFi.localIP());
-
- IPAddress ps5IP(192, 168, 1, 145);
- gt7Telem.begin(ps5IP, 'C');
- Serial.print("PS5 IP: ");
- Serial.println(ps5IP);
-#endif
+
#ifdef INCLUDE_GAMEPAD
Joystick.begin(false);
@@ -1328,6 +1314,92 @@ void setup()
shCustomProtocol.setup();
arqserial.setIdleFunction(idle);
+#if INCLUDE_GT7_WIFI
+ delay(1000);
+
+ WiFi.mode(WIFI_STA);
+
+ WiFiManager wifiManager;
+ // for test reset
+ // wifiManager.resetSettings();
+
+ wifiManager.setConnectTimeout(6);
+ wifiManager.setConfigPortalBlocking(true);
+ wifiManager.setClass("invert");
+ wifiManager.setTitle("GT7 DASH SETUP");
+ std::vector<const char *> menu = {
+ "wifi",
+ "restart",
+ "info"
+ };
+ wifiManager.setMenu(menu);
+ Serial.println();
+ Serial.println("Connecting to saved Wi-Fi...");
+ Serial.print("If connection fails, connect your phone to: ");
+ Serial.println(GT7_SETUP_AP_NAME);
+ Serial.println("Then open http://192.168.4.1 if the setup page does not appear automatically.");
+
+ wifiManager.setAPCallback([](WiFiManager *manager)
+ {
+ Serial.println("=== WiFi setup portal started ===");
+ showWifiSetupScreen(); });
+
+ wifiManager.setBreakAfterConfig(true);
+
+ while (WiFi.status() != WL_CONNECTED)
+ {
+ Serial.println("Starting Wi-Fi setup portal...");
+
+ const bool portalResult =
+ wifiManager.autoConnect(GT7_SETUP_AP_NAME);
+
+ Serial.print("autoConnect result: ");
+ Serial.println(portalResult ? "true" : "false");
+
+ Serial.print("WiFi status: ");
+ Serial.println(WiFi.status());
+
+ // 不看 autoConnect() 回傳值,只確認 ESP32 是否真的連上
+ if (WiFi.status() == WL_CONNECTED)
+ {
+ break;
+ }
+
+ Serial.println("Wi-Fi connection failed.");
+ showWifiConnectionFailedScreen();
+
+ delay(100);
+ }
+
+ Serial.println("Wi-Fi connected.");
+ Serial.print("SSID: ");
+ Serial.println(WiFi.SSID());
+ Serial.print("ESP32 IP: ");
+ Serial.println(WiFi.localIP());
+
+ // Phase 1 keeps the existing fixed PS5 IP. Auto discovery comes next.
+ // IPAddress ps5IP(192, 168, 1, 145);
+ // gt7Telem.begin(ps5IP, 'C');
+ // Serial.print("PS5 IP: ");
+ // Serial.println(ps5IP);
+
+ IPAddress gt7BroadcastIP(255, 255, 255, 255);
+
+ gt7Telem.begin(gt7BroadcastIP, 'C');
+
+ Serial.println("GT7 auto discovery enabled");
+ Serial.print("Broadcast address: ");
+ Serial.println(gt7BroadcastIP);
+
+ // 明確停止 WiFiManager portal
+ wifiManager.stopConfigPortal();
+ // 關閉設定用的 SoftAP,只保留 STA
+ WiFi.softAPdisconnect(true);
+ WiFi.mode(WIFI_STA);
+#endif
+
+delay(1000);
+
#if(GAMEPAD_AXIS_01_ENABLED == 1)
SHGAMEPADAXIS01.SetJoystick(&Joystick);
#endif
@@ -1435,7 +1507,18 @@ void loop() {
UpdateGamepadState();
#endif
shCustomProtocol.loop();
+#if INCLUDE_GT7_WIFI
+ if (shCustomProtocol.takeWifiResetRequest())
+ {
+ Serial.println("Clearing saved Wi-Fi settings...");
+ WiFiManager wifiManager;
+ wifiManager.resetSettings();
+
+ delay(1000);
+ ESP.restart();
+ }
+#endif
// Wait for data
if (FlowSerialAvailable() > 0) {
if (FlowSerialTimedRead() == MESSAGE_HEADER)