-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathesp8266ArduinoToggle.ino
71 lines (55 loc) · 1.27 KB
/
esp8266ArduinoToggle.ino
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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const char* ssid = "thorhammer";
const char* password = "thorthor";
ESP8266WebServer server(80);
const int led = 4;
int state = 0;
void handle_root() {
digitalWrite(led, state);
String s = "<html>";
if (state == 1) {
s += "OUTPUT IS OFF<br>";
}
if (state == 0) {
s += "OUTPUT IS ON<br>";
}
s += "<button style='font-size: 2em;' onclick='javascript:location.reload();'>Toggle</button>";
s += "</html>";
server.send(200, "text/html", s);
Serial.println("Toggled state!");
delay(100);
if (state == 1) {
Serial.println("LED is now off!");
state = 0;
return;
}
if (state == 0) {
Serial.println("LED is now on!");
state = 1;
return;
}
}
void setup(void)
{
Serial.begin(9600);
pinMode(led, OUTPUT);
digitalWrite(led, 0);
// Connect to WiFi network
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
Serial.println("");
Serial.println("");
Serial.print("Access Point SSID: ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.softAPIP());
server.on("/", handle_root);
server.begin();
Serial.println("HTTP server started");
}
void loop(void)
{
server.handleClient();
}