Skip to content

Instantly share code, notes, and snippets.

@dhavlik
Last active May 21, 2019 15:09
Show Gist options
  • Save dhavlik/fffec5e7ffb2a6bf1259ed14b4cef0d0 to your computer and use it in GitHub Desktop.
Save dhavlik/fffec5e7ffb2a6bf1259ed14b4cef0d0 to your computer and use it in GitHub Desktop.
short example for esp8266 - run in ap mode and reply with a static page to every request with a simple dns trick - this leads to a captive portal being displayed on most clients directly after connecting to the ap.
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
const byte DNS_PORT = 53;
IPAddress apIP(192, 168, 1, 1);
DNSServer dnsServer;
ESP8266WebServer webServer(80);
String responseHTML = "<!DOCTYPE html><html><head><meta charset='utf-8'><title>_internet_of_weihnachten</title><style type='text/css'>body {background-color: #000;color: #00ff00;font-size: 2rem;font-family: Helvetica, sans-serif;padding:0;margin:0;padding-left:15px;padding-right: 15px;}a, a:visited, a:active {text-decoration: none;display: block;font-size: 3.5rem;font-family: Helvetica, sans-serif;font-weight: 200;text-align: center;background-color: #000;color: #00ff00;border: 1px solid #00ff00;padding-top:15px;padding-bottom:15px;margin: 15px;}a:active {background-color: #00ff00;color: #000;}</style></head><body><br/>_internet_of_weihnachten v.1.16<br/><a href='?1=letitsnowden'>let it snowden</a><a href='?2=stillenacht'>stille nacht heimlich überwacht</a><a href='?3=byteentsprungen'>es ist ein byte entsprungen</a><a href='?4=klingwhatsapp'>kling, whatsapp, klingelingeling</a></body></html>";
void bling(int times, int p, int out1, int out2, int out3) {
digitalWrite(12, out1);
digitalWrite(13, out2);
digitalWrite(14, out3);
//for (int x=0; x<times; x++) {
// digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
// delay(30); // Wait for a second
// digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
// delay(p);
//}
}
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(14, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP("_i.o.weihnachten");
// if DNSServer is started with "*" for domain name, it will reply with
// provided IP to all DNS request
dnsServer.start(DNS_PORT, "*", apIP);
// replay to all requests with same HTML
webServer.onNotFound([]() {
if (webServer.args() > 0) {
String param = webServer.arg(0);
Serial.println("received arg");
Serial.println(param);
if (param == "letitsnowden") {
bling(1,340, HIGH, LOW, LOW);
} else if (param == "stillenacht") {
bling(2,340, LOW, HIGH, LOW);
} else if (param == "byteentsprungen") {
bling(3,340, LOW, LOW, HIGH);
} else if (param == "klingwhatsapp") {
bling(4,340, LOW, LOW, LOW);
} else {
bling(5,340, LOW, HIGH, HIGH);
}
}
webServer.send(200, "text/html", responseHTML);
});
webServer.begin();
}
void loop() {
dnsServer.processNextRequest();
webServer.handleClient();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment