main.ino
Arduino (C++) · 225 lines
#include <Arduino.h>
#include <Wire.h>
#include <WiFi.h>
#include <WebServer.h>
#include <DNSServer.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// ===================== OLED SSD1306 I2C =====================
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_ADDR 0x3C
// Recommended default ESP32 I2C pins
static const uint8_t I2C_SDA = 21;
static const uint8_t I2C_SCL = 22;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// ===================== LED =====================
// Most ESP32 dev boards have onboard LED on GPIO2.
// If your board has no LED, this will not hurt.
static const uint8_t LED_PIN = 2;
static const bool LED_ACTIVE_LOW = false;
// ===================== Captive Portal =====================
static const char* AP_SSID = "mikrobot-portal";
static const char* AP_PASS = ""; // empty = open Wi-Fi network
const byte DNS_PORT = 53;
DNSServer dnsServer;
WebServer webServer(80);
String currentText = "mikrobot.pl";
String htmlPage() {
String h;
h += "<!doctype html><html><head><meta charset='utf-8'>";
h += "<meta name='viewport' content='width=device-width,initial-scale=1'>";
h += "<title>mikrobot OLED</title></head>";
h += "<body style='font-family:Arial;max-width:520px;margin:24px auto;'>";
h += "<h2>mikrobot OLED</h2>";
h += "<p>Current text:</p>";
h += "<pre style='padding:12px;background:#f3f3f3;border-radius:8px;'>";
h += currentText;
h += "</pre>";
h += "<form action='/set' method='POST'>";
h += "<label>New text:</label><br>";
h += "<input name='t' style='width:100%;padding:10px;font-size:16px' maxlength='80' autofocus>";
h += "<button style='margin-top:12px;padding:10px 14px;font-size:16px'>Update OLED</button>";
h += "</form>";
h += "<p style='margin-top:18px;color:#666'>Connect to Wi-Fi: <b>";
h += AP_SSID;
h += "</b></p>";
h += "</body></html>";
return h;
}
void oledShowCentered(const String& text) {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
String t = text;
t.trim();
if (t.length() == 0) t = " ";
String line1 = t;
String line2 = "";
if (t.length() > 18) {
int split = t.lastIndexOf(' ', t.length() / 2);
if (split < 0) split = t.length() / 2;
line1 = t.substring(0, split);
line2 = t.substring(split);
line2.trim();
}
auto drawCentered = [&](int y, const String& s) {
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(s, 0, y, &x1, &y1, &w, &h);
int x = (SCREEN_WIDTH - (int)w) / 2;
if (x < 0) x = 0;
display.setCursor(x, y);
display.print(s);
};
if (line2.length() > 0) {
drawCentered(22, line1);
drawCentered(38, line2);
} else {
drawCentered(32, line1);
}
display.display();
}
void setLed(bool on) {
if (LED_ACTIVE_LOW) {
digitalWrite(LED_PIN, on ? LOW : HIGH);
} else {
digitalWrite(LED_PIN, on ? HIGH : LOW);
}
}
void blinkLed(uint16_t msOn = 80) {
setLed(true);
delay(msOn);
setLed(false);
}
void handleRoot() {
webServer.send(200, "text/html", htmlPage());
}
void handleSet() {
if (!webServer.hasArg("t")) {
webServer.send(400, "text/plain", "Missing parameter: t");
return;
}
String t = webServer.arg("t");
t.trim();
if (t.length() == 0) {
t = " ";
}
currentText = t;
oledShowCentered(currentText);
blinkLed();
webServer.sendHeader("Location", "/", true);
webServer.send(302, "text/plain", "Redirecting...");
}
// Captive portal redirects
void redirectToPortal() {
webServer.sendHeader("Location", "http://192.168.4.1/", true);
webServer.send(302, "text/plain", "");
}
void handleNotFound() {
redirectToPortal();
}
void setupCaptivePortal() {
WiFi.mode(WIFI_AP);
IPAddress apIP(192, 168, 4, 1);
IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
WiFi.softAPConfig(apIP, gateway, subnet);
if (strlen(AP_PASS) == 0) {
WiFi.softAP(AP_SSID);
} else {
WiFi.softAP(AP_SSID, AP_PASS);
}
dnsServer.start(DNS_PORT, "*", apIP);
webServer.on("/", HTTP_GET, handleRoot);
webServer.on("/set", HTTP_POST, handleSet);
// Android / iOS / Windows captive portal checks
webServer.on("/generate_204", HTTP_GET, redirectToPortal);
webServer.on("/hotspot-detect.html", HTTP_GET, redirectToPortal);
webServer.on("/fwlink", HTTP_GET, redirectToPortal);
webServer.on("/connecttest.txt", HTTP_GET, redirectToPortal);
webServer.on("/ncsi.txt", HTTP_GET, redirectToPortal);
webServer.onNotFound(handleNotFound);
webServer.begin();
}
void setupOled() {
Wire.begin(I2C_SDA, I2C_SCL);
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
pinMode(LED_PIN, OUTPUT);
while (true) {
setLed(true);
delay(200);
setLed(false);
delay(200);
}
}
// Use 0 normally. Change to 2 if your display is upside down.
display.setRotation(0);
display.clearDisplay();
display.setTextWrap(false);
oledShowCentered(currentText);
}
void setup() {
pinMode(LED_PIN, OUTPUT);
setLed(false);
setupOled();
setupCaptivePortal();
}
void loop() {
dnsServer.processNextRequest();
webServer.handleClient();
static unsigned long lastBeat = 0;
if (millis() - lastBeat > 2000) {
lastBeat = millis();
blinkLed(20);
}
}