Files
klubhaus-doorbell/sketches/wifi-diag/wifi-diag.ino
2026-02-16 19:05:13 -08:00

165 lines
5.1 KiB
C++

/*
* WiFi Diagnostic — NO display, NO touch, just network tests
* Waveshare ESP32-S3-Touch-LCD-4.3
*/
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#define WIFI_SSID "iot-2GHz"
#define WIFI_PASS "lesson-greater"
void setup() {
Serial.begin(115200);
unsigned long t = millis();
while (!Serial && millis() - t < 5000) delay(10);
delay(1000);
Serial.println("\n========================================");
Serial.println(" WiFi Diagnostic — NO display");
Serial.printf(" Heap: %d KB PSRAM: %d KB\n",
ESP.getFreeHeap() / 1024, ESP.getFreePsram() / 1024);
Serial.println("========================================\n");
// Connect WiFi
Serial.printf("Connecting to %s...\n", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
int tries = 0;
while (WiFi.status() != WL_CONNECTED && tries++ < 40) {
delay(500);
Serial.print(".");
}
Serial.println();
if (!WiFi.isConnected()) {
Serial.println("*** WiFi FAILED ***");
while (true) delay(1000);
}
Serial.printf("Connected!\n");
Serial.printf(" SSID: %s\n", WiFi.SSID().c_str());
Serial.printf(" IP: %s\n", WiFi.localIP().toString().c_str());
Serial.printf(" GW: %s\n", WiFi.gatewayIP().toString().c_str());
Serial.printf(" DNS: %s\n", WiFi.dnsIP().toString().c_str());
Serial.printf(" RSSI: %d dBm\n", WiFi.RSSI());
Serial.printf(" Heap: %d KB\n\n", ESP.getFreeHeap() / 1024);
// Test 1: DNS
Serial.println("--- TEST 1: DNS ---");
IPAddress ip;
if (WiFi.hostByName("ntfy.sh", ip))
Serial.printf(" ntfy.sh -> %s OK\n", ip.toString().c_str());
else
Serial.println(" ntfy.sh DNS FAILED");
if (WiFi.hostByName("google.com", ip))
Serial.printf(" google.com -> %s OK\n", ip.toString().c_str());
else
Serial.println(" google.com DNS FAILED");
// Test 2: Raw TCP port 80
Serial.println("\n--- TEST 2: TCP port 80 ---");
WiFiClient tcp80;
Serial.println(" Connecting google.com:80...");
if (tcp80.connect("google.com", 80, 10000)) {
Serial.println(" google.com:80 OK!");
tcp80.stop();
} else {
Serial.println(" google.com:80 FAILED");
}
Serial.println(" Connecting ntfy.sh:80...");
WiFiClient tcp80b;
if (tcp80b.connect("ntfy.sh", 80, 10000)) {
Serial.println(" ntfy.sh:80 OK!");
tcp80b.stop();
} else {
Serial.println(" ntfy.sh:80 FAILED");
}
// Test 3: Raw TCP port 443
Serial.println("\n--- TEST 3: TCP port 443 ---");
WiFiClient tcp443;
Serial.println(" Connecting ntfy.sh:443...");
if (tcp443.connect("ntfy.sh", 443, 10000)) {
Serial.println(" ntfy.sh:443 OK!");
tcp443.stop();
} else {
Serial.println(" ntfy.sh:443 FAILED");
}
// Test 4: TLS handshake
Serial.println("\n--- TEST 4: TLS handshake (setInsecure) ---");
Serial.printf(" Heap before TLS: %d KB\n", ESP.getFreeHeap() / 1024);
WiFiClientSecure tls;
tls.setInsecure();
Serial.println(" Connecting ntfy.sh:443 with TLS...");
if (tls.connect("ntfy.sh", 443, 15000)) {
Serial.println(" TLS handshake OK!");
tls.stop();
} else {
Serial.println(" TLS handshake FAILED");
}
Serial.printf(" Heap after TLS: %d KB\n", ESP.getFreeHeap() / 1024);
// Test 5: Full HTTP GET (plain HTTP)
Serial.println("\n--- TEST 5: HTTP GET (plain) ---");
HTTPClient http;
http.setTimeout(10000);
http.begin("http://httpbin.org/get");
int code = http.GET();
Serial.printf(" httpbin.org HTTP %d\n", code);
if (code > 0) Serial.printf(" Body: %.100s...\n", http.getString().c_str());
else Serial.printf(" Error: %s\n", http.errorToString(code).c_str());
http.end();
// Test 6: Full HTTPS GET
Serial.println("\n--- TEST 6: HTTPS GET (ntfy.sh) ---");
Serial.printf(" Heap before: %d KB\n", ESP.getFreeHeap() / 1024);
WiFiClientSecure sc;
sc.setInsecure();
HTTPClient https;
https.setTimeout(10000);
https.begin(sc, "https://ntfy.sh/test_diag_12345/json?poll=1&since=10s");
code = https.GET();
Serial.printf(" ntfy.sh HTTPS %d\n", code);
if (code > 0) Serial.printf(" Body: %.200s...\n", https.getString().c_str());
else Serial.printf(" Error: %s\n", https.errorToString(code).c_str());
https.end();
Serial.printf(" Heap after: %d KB\n", ESP.getFreeHeap() / 1024);
// Test 7: Manual HTTP request over raw TCP
Serial.println("\n--- TEST 7: Manual HTTP over raw TCP ---");
WiFiClient raw;
if (raw.connect("ntfy.sh", 80, 10000)) {
raw.println("GET /test_diag_12345/json?poll=1&since=10s HTTP/1.0");
raw.println("Host: ntfy.sh");
raw.println("Connection: close");
raw.println();
unsigned long start = millis();
while (!raw.available() && millis() - start < 10000) delay(10);
String resp = "";
while (raw.available()) resp += (char)raw.read();
Serial.printf(" Got %d bytes\n", resp.length());
Serial.printf(" Response: %.200s\n", resp.c_str());
raw.stop();
} else {
Serial.println(" Raw TCP to ntfy.sh:80 FAILED");
}
Serial.println("\n========================================");
Serial.println(" ALL TESTS COMPLETE");
Serial.printf(" Final heap: %d KB\n", ESP.getFreeHeap() / 1024);
Serial.println("========================================");
}
void loop() {
delay(10000);
}