-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesp8266LEDexpanded.ino
More file actions
122 lines (96 loc) · 3.59 KB
/
esp8266LEDexpanded.ino
File metadata and controls
122 lines (96 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
// ESP8266 NodeMCU v3 has the LED on Pin 2
#define LED_BUILTIN 2
// let's create a webserver and DNS server
ESP8266WebServer server(80);
DNSServer dnsServer;
const byte DNS_PORT = 53;
// our IP address. Since there is no connection to the rest of the world, we can choose any
IPAddress apIP(192, 168, 1, 1);
// state of the LED
int stateLED = LOW;
// generates the html based on value of variable stateLED
void response();
//for document root '/' calls response() to build the html
void handleRoot();
//for .../LEDOn -> turning LED on and calls response() to build the html
void handleLedOn();
//for .../LEDOff -> turning LED on and calls response() to build the html
void handleLedOff();
void setup() {
//start serial for some output
Serial.begin(115200);
Serial.println();
// setup the ESP8266 as access point with given IP and SSID
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP("lalilu");
Serial.print("visit: \n");
Serial.println(apIP);
// modify TTL associated with the domain name (in seconds)
// default is 60 seconds,
dnsServer.setTTL(300);
// set which return code will be used for all other domains (e.g. sending
// ServerFailure instead of NonExistentDomain will reduce number of queries
// sent by clients)
// default is DNSReplyCode::NonExistentDomain
dnsServer.setErrorReplyCode(DNSReplyCode::ServerFailure);
// start DNS server for a specific domain name
dnsServer.start(DNS_PORT, "/", apIP);
// define the functions for the different links
server.on("/", handleRoot);
server.on("/LEDOn", handleLedOn);
server.on("/LEDOff", handleLedOff);
// start the Webserver
server.begin();
Serial.println("HTTP server beginned");
// set LED Pin to output mode and set it to Low
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, stateLED);
}
// keep the webserver and DNS server listening. Everything else is made by the two servers
void loop() {
dnsServer.processNextRequest();
server.handleClient();
}
// if document root is requested...just generate response with current state
void handleRoot() {
response();
}
// if LED Off button is clicked, change LED state to low, turn the LED off and generate a html accordingly
void handleLedOn() {
stateLED = LOW;
digitalWrite(LED_BUILTIN, stateLED);
response();
}
// if LED On button is clicked, change LED state to high, turn the LED on and generate a html accordingly
void handleLedOff() {
stateLED = HIGH;
digitalWrite(LED_BUILTIN, stateLED);
response();
}
// defines the different parts of the html
const String HtmlHtml = "<html><head>"
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" /></head>";
const String HtmlHtmlClose = "</html>";
const String HtmlTitle = "<h1>Arduino-er: ESP8266 AP WebServer exercise</h1><br/>\n";
const String HtmlLedStateLow = "<big>LED is now <b>ON</b></big><br/>\n";
const String HtmlLedStateHigh = "<big>LED is now <b>OFF</b></big><br/>\n";
const String HtmlButtons =
"<a href=\"LEDOn\"><button style=\"display: block; width: 100%;\">ON</button></a><br/>"
"<a href=\"LEDOff\"><button style=\"display: block; width: 100%;\">OFF</button></a><br/>";
// build the html
void response(){
String htmlRes = HtmlHtml + HtmlTitle;
if(stateLED == LOW){
htmlRes += HtmlLedStateLow;
}else{
htmlRes += HtmlLedStateHigh;
}
htmlRes += HtmlButtons;
htmlRes += HtmlHtmlClose;
server.send(200, "text/html", htmlRes);
}