-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
118 lines (107 loc) · 2.98 KB
/
main.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
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
include "TrafficLight.h"
include <ESP8266WiFi.h>
include <ESP8266WebServer.h>
char ssid[] = "<your-wifi-ssid>";
char pass[] = "<your-wifi-password>";
// create a server with port 80 (default web port)
ESP8266WebServer server(80);
// this will hold the current traffic light max
int ct = 0;
// set a maximum of 3 because of memory & pin limitations
TrafficLight *trafficLights[3];
void connectWIFI() {
// for decoration purposes
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// try to connect to the wifi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
// retry attempt every 500ms
// we won't start our code if wifi hasn't connected
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected!");
// shows the IP address that our device uses
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
// returnInvalid will return a response with 400 Invalid Request
void returnInvalid() {
server.send(400, "text/plain", "Invalid Request");
}
// returnOK will return a response with 200 OK
void returnOK() {
server.send(200, "text/plain", "OK");
}
// handleTraffic will handle requests to change a specific traffic light object's state
void handleTraffic() {
if(!server.hasArg("state") || !server.hasArg("id")) {
returnInvalid();
}
int id = server.arg("id").toInt();
if(trafficLights[id]==NULL) {
returnInvalid();
}
switch(server.arg("state").toInt()) {
case RED:
trafficLights[id]->Stop();
break;
case GREEN:
trafficLights[id]->Go();
break;
case YELLOW:
trafficLights[id]->Careful();
break;
default:
returnInvalid();
}
returnOK();
}
// handleRegister handles requests to register a new traffic light object provided the LED pins
void handleRegister() {
if(!server.hasArg("redPin") || !server.hasArg("yellowPin") || !server.hasArg("greenPin")) {
returnInvalid();
}
if(trafficLights[ct]!=NULL) {
free(trafficLights[ct]);
}
TrafficLight* tl = new TrafficLight(server.arg("redPin").toInt(), server.arg("yellowPin").toInt(), server.arg("greenPin").toInt());
if(server.hasArg("id")){
int id = server.arg("id").toInt();
if(id<3 && id>=0) {
ct = id;
} else {
returnInvalid();
}
}
trafficLights[ct] = tl;
server.send(200, "text/plain", String(ct));
ct = (ct + 1)%3;
}
// startServer will register the handlers and start the server
void startServer() {
server.on("/traffic", handleTraffic);
server.on("/register", handleRegister);
server.begin(); //Start the server
Serial.println("Server listening");
}
// we will call this function first
void setup() {
Serial.begin(115200);
delay(10);
// this section is optional, but I find it being more consistent
WiFi.mode(WIFI_OFF);
delay(1000);
WiFi.mode(WIFI_STA);
while(!Serial);
connectWIFI();
startServer();
}
void loop() {
//Handling of incoming client requests
server.handleClient();
}