#include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <ESP8266mDNS.h>
#ifndef STASSID #define STASSID "와이파이 이름" #define STAPSK "비밀번호" #endif
const char* ssid = STASSID; const char* password = STAPSK;
ESP8266WebServer server(80);
const int led = 2;
const char MAIN_PAGE[] PROGMEM = R"=====( <!doctype html> <html lang="ko"> <head> <link rel="icon" href="data:,"> <meta name="viewport"content="width=device-width,initial-scale=1,user-scalable=no"/> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>WiFi 컨트롤</title> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <style> button {border: 0; border-radius: 0.3rem; background: #858585; color: #faffff; line-height: 2.4rem; font-size: 1.2rem; -webkit-transition-duration: 0.4s; transition-duration: 0.4s; cursor: pointer;} button.off {background: #1fa3ec;} button.off:hover {background: #0e70a4;} button.on {background: gold;} button.on:hover {background:#ddba01;} CENTER > * {width: 250px;} </style> </head> <body> <CENTER> <h2>WiFi 컨트롤</h2> <p>스위치</p> __SWITCH__ </CENTER> </body> </html> )=====";
const char SWITCH_ON[] PROGMEM = R"=====(<button id = "switch" class="on" onClick="$.post(`/switch/${$(this).hasClass('off')?'on':'off'}`,(data)=>{data.status?$('#switch').addClass('on').removeClass('off').text('ON'):$('#switch').addClass('off').removeClass('on').text('OFF')})">ON</button>)====="; const char SWITCH_OFF[] PROGMEM = R"=====(<button id = "switch" class="off" onClick="$.post(`/switch/${$(this).hasClass('off')?'on':'off'}`,(data)=>{data.status?$('#switch').addClass('on').removeClass('off').text('ON'):$('#switch').addClass('off').removeClass('on').text('OFF')})">OFF</button>)=====";
void handleRoot() { String main_page = (const __FlashStringHelper *)MAIN_PAGE; if (digitalRead(led) == HIGH) { main_page.replace("__SWITCH__", (String)(const __FlashStringHelper *)SWITCH_ON); digitalWrite(led, HIGH); } else { main_page.replace("__SWITCH__", (String)(const __FlashStringHelper *)SWITCH_OFF); digitalWrite(led, LOW); } server.send(200, "text/html", main_page); }
void handleSwitchOn() { if (server.method() != HTTP_POST) { server.send(405, "text/plain", "Method Not Allowed"); } else { digitalWrite(led, HIGH); server.send(200, "application/json", "{\"status\":1}"); } }
void handleSwitchOff() { if (server.method() != HTTP_POST) { server.send(405, "text/plain", "Method Not Allowed"); } else { digitalWrite(led, LOW); server.send(200, "application/json", "{\"status\":0}"); } }
void handleNotFound() { String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET) ? "GET" : "POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i = 0; i < server.args(); i++) { message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } server.send(404, "text/plain", message); }
void setup(void) { pinMode(led, OUTPUT); digitalWrite(led, 0); Serial.begin(9600); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.println("");
// Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) { Serial.println("MDNS responder started"); }
server.on("/", handleRoot);
server.on("/inline", []() { server.send(200, "text/plain", "this works as well"); });
server.on("/gif", []() { static const uint8_t gif[] PROGMEM = { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x00, 0x02, 0x19, 0x8c, 0x8f, 0xa9, 0xcb, 0x9d, 0x00, 0x5f, 0x74, 0xb4, 0x56, 0xb0, 0xb0, 0xd2, 0xf2, 0x35, 0x1e, 0x4c, 0x0c, 0x24, 0x5a, 0xe6, 0x89, 0xa6, 0x4d, 0x01, 0x00, 0x3b }; char gif_colored[sizeof(gif)]; memcpy_P(gif_colored, gif, sizeof(gif)); // Set the background to a random set of colors gif_colored[16] = millis() % 256; gif_colored[17] = millis() % 256; gif_colored[18] = millis() % 256; server.send(200, "image/gif", gif_colored, sizeof(gif_colored)); });
server.onNotFound(handleNotFound);
if (MDNS.begin("esp8266")) { Serial.println("MDNS responder started"); }
server.on("/", handleRoot); server.on("/switch/on", handleSwitchOn); server.on("/switch/off", handleSwitchOff); server.begin(); Serial.println("HTTP server started"); }
void loop() { server.handleClient(); MDNS.update(); } |