main.ino
Arduino (C++) · 216 lines
#include <Arduino.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// ===================== OLED (SSD1306 I2C) =====================
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_ADDR 0x3C
// Your board description says I2C pins are:
// SCL = GPIO12 (D5), SDA = GPIO14 (D6)
static const uint8_t I2C_SCL = 12;
static const uint8_t I2C_SDA = 14;
// SSD1306 instance (no reset pin used on most modules -> -1)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// ===================== LED (fix wrong product pin info) =====================
// Use LED_BUILTIN so we don't rely on possibly incorrect descriptions.
// On ESP8266 the built-in LED is typically GPIO2 and is ACTIVE-LOW.
static const uint8_t LED_PIN = LED_BUILTIN;
static const bool LED_ACTIVE_LOW = true;
// ===================== Captive Portal =====================
static const char* AP_SSID = "mikrobot-portal";
static const char* AP_PASS = ""; // leave empty for open AP (recommended for captive portal)
const byte DNS_PORT = 53;
DNSServer dnsServer;
ESP8266WebServer webServer(80);
// Text shown on OLED
String currentText = "mikrobot.pl";
// Simple HTML (kept short)
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><body style='font-family:Arial;max-width:520px;margin:24px auto;'>";
h += "<h2>mikrobot OLED</h2>";
h += "<p>Current text:</p><pre style='padding:12px;background:#f3f3f3;border-radius:8px;'>";
h += currentText;
h += "</pre>";
h += "<form action='/set' method='POST'>";
h += "<label>New text (max ~32 chars recommended):</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);
// Two-line friendly rendering:
// If text is long, we split it roughly in half.
String t = text;
t.trim();
if (t.length() == 0) t = " ";
display.setTextSize(1);
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;
display.setCursor(max(0, 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);
}
// Captive portal helpers: redirect unknown paths to "/"
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();
// Redirect back to root
webServer.sendHeader("Location", "/", true);
webServer.send(302, "text/plain", "Redirecting...");
}
// Common captive portal detection endpoints (helps phones open the portal)
void handleGenerate204() { webServer.sendHeader("Location", "http://192.168.4.1/", true); webServer.send(302, "text/plain", ""); }
void handleHotspotDetect() { webServer.sendHeader("Location", "http://192.168.4.1/", true); webServer.send(302, "text/plain", ""); }
void handleFWLink() { webServer.sendHeader("Location", "http://192.168.4.1/", true); webServer.send(302, "text/plain", ""); }
void handleNotFound() {
// Always redirect to portal root
webServer.sendHeader("Location", "http://192.168.4.1/", true);
webServer.send(302, "text/plain", "");
}
void setupCaptivePortal() {
WiFi.mode(WIFI_AP);
if (strlen(AP_PASS) == 0) {
WiFi.softAP(AP_SSID);
} else {
WiFi.softAP(AP_SSID, AP_PASS);
}
// DNS: redirect all domains to AP IP (captive portal)
dnsServer.start(DNS_PORT, "*", WiFi.softAPIP());
// Web routes
webServer.on("/", HTTP_GET, handleRoot);
webServer.on("/set", HTTP_POST, handleSet);
// Captive portal detection URLs
webServer.on("/generate_204", HTTP_GET, handleGenerate204);
webServer.on("/hotspot-detect.html", HTTP_GET, handleHotspotDetect);
webServer.on("/fwlink", HTTP_GET, handleFWLink);
webServer.onNotFound(handleNotFound);
webServer.begin();
}
void setupOled() {
Wire.begin(I2C_SDA, I2C_SCL);
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
// If OLED init fails, blink LED forever
pinMode(LED_PIN, OUTPUT);
while (true) {
setLed(true); delay(200);
setLed(false); delay(200);
}
}
// Rotate display by 180 degrees (fix upside-down mounting)
display.setRotation(2); // 0=normal, 2=180 degrees
display.clearDisplay();
display.setTextWrap(false);
oledShowCentered(currentText);
}
void setup() {
pinMode(LED_PIN, OUTPUT);
setLed(false);
setupOled();
setupCaptivePortal();
}
void loop() {
dnsServer.processNextRequest();
webServer.handleClient();
// Optional: slow heartbeat blink every ~2s to show it's alive
static unsigned long lastBeat = 0;
if (millis() - lastBeat > 2000) {
lastBeat = millis();
blinkLed(20);
}
}