main.ino
Arduino (C++) · 180 lines
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
// =======================
// OLED CONFIGURATION
// =======================
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// =======================
// WIFI ACCESS POINT CONFIG
// =======================
const char* ap_ssid = "ESP-OLED"; // open AP (no password)
IPAddress apIP(192, 168, 4, 1);
// =======================
// DNS / CAPTIVE PORTAL
// =======================
const byte DNS_PORT = 53;
DNSServer dnsServer;
ESP8266WebServer server(80);
// Text displayed on OLED (default)
String displayText = "mikrobot.pl";
// =======================
// OLED RENDER FUNCTION
// =======================
void showOnDisplay(const String& text) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(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>ESP OLED</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>ESP8266 + SSD1306 OLED</h2>";
html += "<p>Current text displayed:</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 display'><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>ESP-OLED</b> (open network)</p>";
html += "<p>This page should open automatically (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 = " ";
displayText = msg;
showOnDisplay(displayText);
}
server.send(200, "text/html", htmlPage(displayText));
}
void handleNotFound() {
redirectToRoot();
}
// Captive portal triggers used by Android, iOS, Windows
void handleGenerate204() { handleRoot(); }
void handleHotspotDetect() { handleRoot(); }
void handleNCsi() { handleRoot(); }
// =======================
// SETUP
// =======================
void setup() {
Wire.begin(14, 12); // SDA = 14, SCL = 12 (specific to your ESP-12F board)
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 initialization failed"));
while (true);
}
display.clearDisplay();
display.display();
showOnDisplay(displayText);
// WiFi Access Point (open)
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255,255,255,0));
WiFi.softAP(ap_ssid);
Serial.print("AP started. SSID: ");
Serial.println(ap_ssid);
Serial.print("IP: ");
Serial.println(apIP);
// Show AP info on OLED
display.setCursor(0, 40);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setTextWrap(false);
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
dnsServer.start(DNS_PORT, "*", apIP);
// Start web server
server.on("/", handleRoot);
server.on("/set", handleSetText);
// Captive portal routes
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();
}