main.ino

Arduino (C++) · 193 lines

#include <WiFi.h>
#include <WebServer.h>
#include <DNSServer.h>

#include <Wire.h>
#include <Adafruit_SH110X.h>  // same include as in your working example

// =======================
// SH1106 OLED CONFIGURATION
// =======================

/*
Connection to ESP32:
VCC -> 3.3V
GND -> GND
SCK -> GPIO22 (SCL / clock)
SDA -> GPIO21 (SDA / data)
*/

#define i2c_Address 0x3C        // I2C address of the display
#define SCREEN_WIDTH 128        // OLED width in pixels
#define SCREEN_HEIGHT 64        // OLED height in pixels
#define OLED_RESET -1           // No reset pin

// OLED display object (same style as your working code)
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// =======================
// WIFI ACCESS POINT CONFIG
// =======================
const char* ap_ssid = "ESP32-OLED";   // Open AP (no password)
IPAddress apIP(192, 168, 4, 1);
IPAddress netMsk(255, 255, 255, 0);

// =======================
// DNS / CAPTIVE PORTAL
// =======================
const byte DNS_PORT = 53;
DNSServer dnsServer;
WebServer server(80);

// Text displayed on the OLED (default)
String displayText = "mikrobot.pl";

// =======================
// OLED RENDER FUNCTION
// =======================
void showOnDisplay(const String& text) {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(0, 0);
  display.setTextWrap(true);
  display.println(text);
  display.display();
}

// =======================
// HTML PAGE
// =======================
String htmlPage(const String& currentText) {
  String html =
    "<!DOCTYPE html><html><head>"
    "<meta charset='UTF-8'>"
    "<meta name='viewport' content='width=device-width, initial-scale=1.0'>"
    "<title>ESP32 OLED Text Portal</title>"
    "<style>"
    "body{font-family:sans-serif;margin:20px;}"
    "input[type=text]{width:100%;padding:8px;margin:8px 0;box-sizing:border-box;}"
    "input[type=submit]{padding:8px 16px;}"
    "pre{background:#eee;padding:8px;white-space:pre-wrap;word-wrap:break-word;}"
    "</style>"
    "</head><body>";

  html += "<h2>ESP32 + SH1106 OLED</h2>";
  html += "<p>Current text on display:</p>";
  html += "<pre><b>" + currentText + "</b></pre>";

  html += "<form action='/set' method='GET'>";
  html += "<label>New text:</label><br>";
  html += "<input type='text' name='msg' maxlength='128' "
          "placeholder='Enter text to show on the OLED'><br>";
  html += "<input type='submit' value='Send to OLED'>";
  html += "</form>";

  html += "<p>Default text after reboot: <b>mikrobot.pl</b></p>";
  html += "<p>Connect to WiFi network <b>ESP32-OLED</b> (open, no password).</p>";
  html += "<p>This page should automatically open as a captive portal.</p>";

  html += "</body></html>";
  return html;
}

// =======================
// CAPTIVE PORTAL REDIRECT
// =======================
void redirectToRoot() {
  String url = "http://" + apIP.toString() + "/";
  server.sendHeader("Location", url, true);
  server.send(302, "text/plain", "");
}

// =======================
// HTTP HANDLERS
// =======================
void handleRoot() {
  server.send(200, "text/html", htmlPage(displayText));
}

void handleSetText() {
  if (server.hasArg("msg")) {
    String msg = server.arg("msg");
    if (msg.length() == 0) msg = " ";   // Avoid an empty string
    displayText = msg;
    showOnDisplay(displayText);
  }
  server.send(200, "text/html", htmlPage(displayText));
}

void handleNotFound() {
  // Redirect unknown paths to main page – captive portal behavior
  redirectToRoot();
}

// Captive portal triggers used by Android, iOS, Windows
void handleGenerate204() { handleRoot(); }          // /generate_204 (Android)
void handleHotspotDetect() { handleRoot(); }        // /hotspot-detect.html (iOS/macOS)
void handleNCsi() { handleRoot(); }                 // /ncsi.txt (Windows)

// =======================
// SETUP
// =======================
void setup() {
  Serial.begin(9600);
  delay(250);

  // Display initialization (same style as your working example)
  display.begin(i2c_Address, true); // init display
  display.display();                // show buffer (required on startup)
  delay(500);
  display.clearDisplay();

  // Show default text
  showOnDisplay(displayText);

  // Start WiFi in AP mode (open network)
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP, apIP, netMsk);
  WiFi.softAP(ap_ssid);

  Serial.println();
  Serial.print("AP started. SSID: ");
  Serial.println(ap_ssid);
  Serial.print("AP IP address: ");
  Serial.println(apIP);

  // Show AP info on OLED (bottom area)
  display.setTextSize(1);
  display.setTextColor(SH110X_WHITE);
  display.setTextWrap(false);
  display.setCursor(0, 40);
  display.println();
  display.print("AP: ");
  display.println(ap_ssid);
  display.print("IP:");
  display.println(apIP);
  display.display();

  // Start DNS server – redirect all domains to our AP IP
  dnsServer.start(DNS_PORT, "*", apIP);

  // Setup web server routes
  server.on("/", handleRoot);
  server.on("/set", handleSetText);

  // Captive portal detection URLs
  server.on("/generate_204", handleGenerate204);
  server.on("/hotspot-detect.html", handleHotspotDetect);
  server.on("/ncsi.txt", handleNCsi);

  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("Web server + DNS captive portal started.");
}

// =======================
// LOOP
// =======================
void loop() {
  dnsServer.processNextRequest();
  server.handleClient();
}