main.ino
Arduino (C++) · 203 lines
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <Wire.h>
#include <Adafruit_SH110X.h>
// =======================
// SH1106 OLED CONFIGURATION
// =======================
/*
Connection to Wemos D1 mini / D1 R2:
VCC -> 3V3
GND -> G (GND)
SCK -> D1 (GPIO5, SCL)
SDA -> D2 (GPIO4, SDA)
On D1 R2 connect to the pins labeled SCL and SDA on the board.
Arduino IDE board: "LOLIN(WEMOS) D1 R2 & mini"
*/
#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
#define OLED_SDA D2 // GPIO4
#define OLED_SCL D1 // GPIO5
// OLED display object
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// =======================
// WIFI ACCESS POINT CONFIG
// =======================
const char* ap_ssid = "WEMOS-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;
ESP8266WebServer 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>Wemos D1 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>Wemos D1 + 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>" + String(ap_ssid) + "</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);
// I2C on D2 (SDA) and D1 (SCL)
Wire.begin(OLED_SDA, OLED_SCL);
// Display initialization
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.persistent(false); // Don't write WiFi config to flash
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();
}