Skip to content

A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol

add wifi scan and setup on screen for tembed

Geo54504d78ed62
diff --git a/src/Config/WifiTypeConfigurator.cpp b/src/Config/WifiTypeConfigurator.cpp
index 499260af..47d57a7f 100644
--- a/src/Config/WifiTypeConfigurator.cpp
+++ b/src/Config/WifiTypeConfigurator.cpp
@@ -11,11 +11,11 @@ std::string WifiTypeConfigurator::configure(TerminalTypeEnum& terminalType) {
#elif defined(DEVICE_M5STICK)
// Use this standalone setup for now
setupStickWifi(); // check stored creds
- #elif defined(DEVICE_M5STAMPS3)
- // Use this standalone setup for now
#elif defined(DEVICE_TEMBEDS3) || defined(DEVICE_TEMBEDS3CC1101)
- setupTembedWifi(); // check stored creds
+ // Use this standalone setup for now
+ setupTembedWifi(); // endless loop until a valid WiFi is selected and connected
#else
+ // Use this standalone setup for now
setupS3Wifi(); // check stored creds
#endif
diff --git a/src/Config/WifiTypeConfigurator.h b/src/Config/WifiTypeConfigurator.h
index 5690175a..3c5633a2 100644
--- a/src/Config/WifiTypeConfigurator.h
+++ b/src/Config/WifiTypeConfigurator.h
@@ -5,11 +5,19 @@
#include <Vendors/StickWifiSetup.h>
#include <Vendors/S3WifiSetup.h>
#include <Vendors/TembedWifiSetup.h>
+#include <Interfaces/IDeviceView.h>
+#include <Interfaces/IInput.h>
#include <string>
#include <WiFi.h>
class WifiTypeConfigurator {
public:
- WifiTypeConfigurator() = default;
+ WifiTypeConfigurator(IDeviceView& view, IInput& input)
+ : view(view), input(input) {}
+
std::string configure(TerminalTypeEnum& terminalType);
-};
+
+private:
+ IDeviceView& view;
+ IInput& input;
+};
\ No newline at end of file
diff --git a/src/Vendors/TembedWifiSetup.cpp b/src/Vendors/TembedWifiSetup.cpp
index 54914ef5..00d3d425 100644
--- a/src/Vendors/TembedWifiSetup.cpp
+++ b/src/Vendors/TembedWifiSetup.cpp
@@ -1,11 +1,89 @@
#if defined(DEVICE_TEMBEDS3) || defined(DEVICE_TEMBEDS3CC1101)
#include "TembedWifiSetup.h"
+
#include <WiFi.h>
#include <Preferences.h>
#include <TFT_eSPI.h>
+#include <RotaryEncoder.h>
+#include <esp_sleep.h>
+#include "Inputs/InputKeys.h"
+
+#ifdef DEVICE_TEMBEDS3CC1101
+ #define TEMBED_PIN_ENCODE_A 4
+ #define TEMBED_PIN_ENCODE_B 5
+ #define TEMBED_PIN_SIDE_BTN 6
+#else
+ #define TEMBED_PIN_ENCODE_A 2
+ #define TEMBED_PIN_ENCODE_B 1
+ #define TEMBED_PIN_SIDE_BTN 0 // side button is reset
+#endif
+
+#define TEMBED_PIN_ENCODE_BTN 0
TFT_eSPI tft;
+RotaryEncoder encoder(TEMBED_PIN_ENCODE_A, TEMBED_PIN_ENCODE_B, RotaryEncoder::LatchMode::TWO03);
+
+char lastInput;
+int lastPos;
+bool lastButton;
+
+void checkShutdownRequest() {
+ if (!digitalRead(TEMBED_PIN_ENCODE_BTN) || !digitalRead(TEMBED_PIN_SIDE_BTN)) {
+ unsigned long start = millis();
+ for (int i = 0; i < 30; ++i) {
+ if (digitalRead(TEMBED_PIN_ENCODE_BTN) && digitalRead(TEMBED_PIN_SIDE_BTN)) return;
+ delay(100);
+ }
+
+ tft.fillScreen(TFT_BLACK);
+ tft.setCursor(40, 60);
+ tft.setTextColor(TFT_WHITE);
+ tft.setTextFont(2);
+ int16_t x1, y1;
+ uint16_t w, h;
+ std::string text = "Shutting down...";
+ tft.setTextDatum(MC_DATUM);
+ tft.drawString(text.c_str(), tft.width() / 2, 80);
+ // Shutdown
+ delay(3000);
+ esp_sleep_enable_ext0_wakeup((gpio_num_t)TEMBED_PIN_SIDE_BTN, 0);
+ esp_deep_sleep_start();
+ }
+}
+
+void tick() {
+ encoder.tick();
+ int pos = encoder.getPosition();
+ if (pos < lastPos) {
+ lastInput = KEY_ARROW_LEFT;
+ lastPos = pos;
+ } else if (pos > lastPos) {
+ lastInput = KEY_ARROW_RIGHT;
+ lastPos = pos;
+ } else if (!digitalRead(TEMBED_PIN_ENCODE_BTN) && !lastButton) {
+ lastInput = KEY_OK;
+ lastButton = true;
+ } else if (digitalRead(TEMBED_PIN_ENCODE_BTN)) {
+ lastButton = false;
+ }
+ checkShutdownRequest();
+}
+
+char readChar() {
+ tick();
+ char c = lastInput;
+ lastInput = KEY_NONE;
+ return c;
+}
+
+char handler() {
+ while (true) {
+ char c = readChar();
+ if (c != KEY_NONE) return c;
+ delay(5);
+ }
+}
Preferences& getPreferences() {
static Preferences preferences;
@@ -23,6 +101,7 @@ bool loadWifiCredentials(String& ssid, String& password) {
void drawWifiBox(uint16_t borderColor, const String& msg1, const String& msg2, uint8_t textSize = 2) {
tft.fillScreen(TFT_BLACK);
+ tft.setTextFont(1);
tft.fillRoundRect(20, 20, tft.width() - 40, tft.height() - 40, 5, DARK_GREY);
tft.drawRoundRect(20, 20, tft.width() - 40, tft.height() - 40, 5, borderColor);
@@ -31,40 +110,152 @@ void drawWifiBox(uint16_t borderColor, const String& msg1, const String& msg2, u
tft.setTextSize(textSize);
tft.drawString(msg1, 37, 45);
- tft.setTextFont(1);
tft.setTextSize(1);
tft.setTextColor(TFT_LIGHTGREY);
- tft.drawString(msg2, 36, 70);
+ tft.drawString(msg2, 37, 70);
}
-bool setupTembedWifi() {
- tft.setRotation(3);
- String ssid, password;
- drawWifiBox(TFT_GREEN, "Loading WiFi...", "");
+String selectWifiNetwork(TFT_eSPI& tft) {
+ int networksCount = 0;
- if (!loadWifiCredentials(ssid, password)) {
- drawWifiBox(TFT_GREEN, "No saved WiFi", "Setup your Wi-Fi with USB Serial");
- delay(5000);
- return false;
+ while (networksCount == 0) {
+ drawWifiBox(TFT_GREEN, "Scanning WiFi...", "");
+ networksCount = WiFi.scanNetworks();
+ if (networksCount == 0) {
+ drawWifiBox(TFT_RED, "No networks found", "Retrying...");
+ delay(2000);
+ }
}
- drawWifiBox(TFT_GREEN, "Connecting", ssid);
+ int selected = 0;
+ tft.fillScreen(TFT_BLACK);
+ tft.setTextColor(TFT_GREEN);
+ tft.setTextSize(2);
+ tft.drawString("Select Wi-Fi:", 10, 10);
+ while (true) {
+ tft.fillRect(0, 40, tft.width(), tft.height() - 40, TFT_BLACK);
+ tft.setTextSize(1);
+ for (int i = 0; i < min(5, networksCount); ++i) {
+ String ssid = WiFi.SSID(i);
+ if (i == selected) {
+ tft.setTextColor(TFT_GREEN);
+ tft.drawString("> " + ssid, 20, 40 + i * 15);
+ } else {
+ tft.setTextColor(TFT_LIGHTGREY);
+ tft.drawString(" " + ssid, 20, 40 + i * 15);
+ }
+ }
- WiFi.begin(ssid.c_str(), password.c_str());
+ char key = handler();
+ if (key == KEY_ARROW_RIGHT) {
+ selected = (selected - 1 + networksCount) % networksCount;
+ } else if (key == KEY_ARROW_LEFT) {
+ selected = (selected + 1) % networksCount;
+ } else if (key == KEY_OK) {
+ return WiFi.SSID(selected);
+ }
+ }
+}
- tft.setTextColor(TFT_LIGHTGREY);
- tft.setCursor(55, 100);
- for (int i = 0; i < 30; ++i) {
- if (WiFi.status() == WL_CONNECTED) {
- return true;
+String enterText(const String& label, TFT_eSPI& tft) {
+ const String charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_ .@#<";
+ int index = 0;
+ String text = "";
+
+ tft.fillScreen(TFT_BLACK);
+ tft.setTextColor(TFT_GREEN);
+ tft.setTextSize(2);
+ tft.drawString(label, 10, 10);
+ tft.setTextSize(1);
+ tft.drawString("Wheel to select - [OK] to Confirm - [<-] to Erase", 10, tft.height() - 20);
+
+ while (true) {
+ char currentChar = charset[index];
+
+ String alias;
+ if (currentChar == '<') {
+ alias = "<-";
+ } else if (currentChar == '#') {
+ alias = "OK";
+ } else {
+ alias = String(currentChar);
+ }
+
+ String charDisplay = (currentChar == '#' || currentChar == '<')
+ ? "[" + alias + "]"
+ : alias;
+ String display = text + charDisplay;
+
+ tft.fillRect(0, 50, tft.width(), tft.height() - 100, TFT_BLACK);
+ tft.setTextSize(1);
+ tft.setTextFont(2);
+ tft.setTextColor(TFT_LIGHTGREY);
+ tft.drawString(display, 10, 70);
+ tft.setTextColor(TFT_WHITE);
+ char key = handler();
+ if (key == KEY_ARROW_RIGHT) {
+ index = (index - 1 + charset.length()) % charset.length();
+ } else if (key == KEY_ARROW_LEFT) {
+ index = (index + 1) % charset.length();
+ } else if (key == KEY_OK) {
+ if (currentChar == '<') {
+ if (!text.isEmpty()) text.remove(text.length() - 1);
+ } else if (currentChar == '#') {
+ return text;
+ } else {
+ text += currentChar;
+ }
+ }
+ }
+}
+
+void setWifiCredentials(String ssid, String password) {
+ Preferences& preferences = getPreferences();
+ preferences.begin("wifi_settings", false);
+ preferences.putString(NVS_SSID_KEY, ssid);
+ preferences.putString(NVS_PASS_KEY, password);
+ preferences.end();
+}
+
+bool setupTembedWifi() {
+ tft.setRotation(3);
+ String ssid, password;
+
+ while (true) {
+
+
+ if (!loadWifiCredentials(ssid, password)) {
+ ssid = selectWifiNetwork(tft);
+ password = enterText("Enter password", tft);
+ setWifiCredentials(ssid, password);
+ }
+
+ drawWifiBox(TFT_GREEN, "Connecting", ssid);
+ WiFi.begin(ssid.c_str(), password.c_str());
+
+ tft.setTextColor(TFT_LIGHTGREY);
+ tft.setCursor(55, 100);
+ for (int i = 0; i < 30; ++i) {
+ if (WiFi.status() == WL_CONNECTED) {
+ drawWifiBox(TFT_GREEN, "Connected", "");
+ delay(2000);
+ return true;
+ }
+ delay(600);
+ tft.print(".");
+ auto key = readChar();
+ if (key == KEY_ARROW_LEFT) break;
}
- delay(500);
- tft.print(".");
+
+ drawWifiBox(TFT_WHITE, "Failed to connect", "Retry or setup Wi-Fi with Serial");
+ setWifiCredentials("", ""); // reset
+ WiFi.disconnect(true);
+ WiFi.mode(WIFI_STA);
+ delay(3000);
}
- drawWifiBox(TFT_GREEN, "Failed to connect", "Setup your Wi-Fi with USB Serial");
- delay(4000);
return false;
}
+
#endif
diff --git a/src/Vendors/TembedWifiSetup.h b/src/Vendors/TembedWifiSetup.h
index 16cf2855..df22a3af 100644
--- a/src/Vendors/TembedWifiSetup.h
+++ b/src/Vendors/TembedWifiSetup.h
@@ -1,6 +1,7 @@
#pragma once
#include <Arduino.h>
+#include "Inputs/InputKeys.h"
#define NVS_SSID_KEY "ssid"
#define NVS_PASS_KEY "pass"
diff --git a/src/Views/TembedDeviceView.cpp b/src/Views/TembedDeviceView.cpp
index d57b388e..964409d2 100644
--- a/src/Views/TembedDeviceView.cpp
+++ b/src/Views/TembedDeviceView.cpp
@@ -61,12 +61,15 @@ void TembedDeviceView::welcome(TerminalTypeEnum& terminalType, std::string& term
void TembedDeviceView::loading() {
tft.fillScreen(TFT_BLACK);
- tft.setTextSize(1);
tft.setTextColor(TFT_WHITE);
+ tft.setTextFont(1);
tft.fillRoundRect(20, 20, tft.width() - 40, tft.height() - 40, 5, DARK_GREY_RECT);
tft.drawRoundRect(20, 20, tft.width() - 40, tft.height() - 40, 5, TFT_GREEN);
- drawCenterText("Loading...", 80, 2);
+
+ tft.setTextColor(TFT_WHITE);
+ tft.setTextSize(2);
+ tft.drawString("Loading...", 102, 52);
}
void TembedDeviceView::clear() {
diff --git a/src/main.cpp b/src/main.cpp
index 39806555..14693b2c 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -71,7 +71,7 @@ void setup() {
// Configure Wi-Fi if needed
if (terminalType == TerminalTypeEnum::WiFiClient) {
- WifiTypeConfigurator wifiTypeConfigurator;
+ WifiTypeConfigurator wifiTypeConfigurator(deviceView, deviceInput);
webIp = wifiTypeConfigurator.configure(terminalType);
if (webIp == "0.0.0.0") {