-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino.ino
83 lines (70 loc) · 1.78 KB
/
arduino.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
72
73
74
75
76
77
78
79
80
81
82
83
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#define redLight D1
#define greenLight D2
const char* ssid = "xxx";
const char* password = "xxx";
ESP8266WebServer server(80);
void root() {
server.send(200, "text/plain", "I am smart traffic light");
}
void success() {
server.send(200, "text/plain", "Ligar");
digitalWrite(redLight, LOW);
digitalWrite(greenLight, HIGH);
}
void failure() {
server.send(200, "text/plain", "Desligar");
digitalWrite(redLight, HIGH);
digitalWrite(greenLight, LOW);
}
void localIp() {
server.send(200, "text/plain", WiFi.localIP());
}
void notFound(){
String msg = "File not found\n\n";
msg += "URI: ";
msg += server.uri();
msg += "\nMethod: ";
msg += (server.method() == HTTP_GET)?"GET":"POST";
msg += "\nArguments: ";
msg += server.args();
msg += "\n";
for (uint8_t i=0; i<server.args(); i++){
msg += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", msg);
}
void setup(){
pinMode(redLight, OUTPUT);
pinMode(greenLight, OUTPUT);
digitalWrite(redLight, LOW);
digitalWrite(greenLight, LOW);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println(".");
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("/", root);
server.on("/failure", failure);
server.on("/success", success);
server.on("/localip", localIp);
server.onNotFound(notFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(){
server.handleClient();
}