-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraffic_server.ino
177 lines (153 loc) · 4.22 KB
/
traffic_server.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Connects to the home WiFi network
* Asks some network parameters
* Starts WiFi server with fix IP and listens
* Receives and sends messages to the client
* Communicates: traffic_client.ino
*/
#include <SPI.h>
#include <ESP8266WiFi.h>
bool debugMode = false; // Debug will let you put in light values in the Serial Monitor and see the result in the output
int GREEN_RELAY = D2;
int AMBER_RELAY = D3;
int RED_RELAY = D4;
int AMBER_DWELL_TIME = 2900;
int POO_TIME_SECONDS = 60 * 4;
int AIR_OUT_AMBER_SECONDS = 60;
int currentLight = -1;
bool currentlyOccupied = false;
unsigned long startTime = 0;
char ssid[] = "Hackspace_2_4"; // SSID of your home WiFi
char pass[] = "xxxx_x_xxx"; // password of your home WiFi
WiFiServer server(80);
IPAddress ip(192, 168, 0, 80); // IP address of the server
IPAddress gateway(192,168,0,1); // gateway of your network
IPAddress subnet(255,255,255,0); // subnet mask of your network
void setup()
{
Serial.begin(115200); // only for debug
// Sets pins to output
pinMode(GREEN_RELAY, OUTPUT);
pinMode(AMBER_RELAY, OUTPUT);
pinMode(RED_RELAY, OUTPUT);
showLights(true, true, true);
if (!debugMode)
{
WiFi.disconnect(); // Because the wemos logs AP information to memory and that removes the last AP information.
WiFi.config(ip, gateway, subnet); // forces to use the fix IP
WiFi.begin(ssid, pass); // connects to the WiFi router
Serial.println("Connecting to wifi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("Connected");
server.begin(); // starts the server
}
showLights(false, false, true);
}
void printLights(bool red, bool amber, bool green)
{
Serial.println("---");
if (red)
Serial.println("(x)");
else
Serial.println("( )");
if (amber)
Serial.println("(x)");
else
Serial.println("( )");
if (green)
Serial.println("(x)");
else
Serial.println("( )");
Serial.println("---");
}
void showLights(bool red, bool amber, bool green)
{
digitalWrite(RED_RELAY, red ? HIGH : LOW);
digitalWrite(AMBER_RELAY, amber ? HIGH : LOW);
digitalWrite(GREEN_RELAY, green ? HIGH : LOW);
if (debugMode)
printLights(red, amber, green);
}
void setOccupied(bool occupied)
{
if (currentlyOccupied == occupied)
return;
if (occupied)
{
startTime = millis();
showLights(false, true, false); // Amber
delay(AMBER_DWELL_TIME);
showLights(true, false, false); // Red
}
else
{
Serial.print("You spent ");
unsigned long totalTimeSeconds = (millis() - startTime) / 1000;
Serial.print(totalTimeSeconds);
Serial.println(" seconds in the toilet");
if (totalTimeSeconds > POO_TIME_SECONDS)
{
Serial.print("Long time taken, amber alert");
for (int i = 0; i < AIR_OUT_AMBER_SECONDS; i++)
{
showLights(false, true, false);
delay(500);
showLights(false, false, false);
delay(500);
}
}
else
{
showLights(true, true, false); // Red/Amber
delay(AMBER_DWELL_TIME);
}
showLights(false, false, true); // Green
}
currentlyOccupied = occupied;
}
void interpretLightValue(float lightValue)
{
if(lightValue > 15 )
{
setOccupied(true);
Serial.println("Toilet occupied");
}
else if(lightValue > 1)
{
setOccupied(false);
Serial.println("Toilet unoccupied");
}
else
{
Serial.println("Erroneous reading, ignore!");
}
}
void loop()
{
if (debugMode)
debugLoop();
else
wifiLoop();
}
void debugLoop()
{
String request = Serial.readStringUntil('\r');
float value = request.toFloat();
interpretLightValue(value);
}
void wifiLoop() {
WiFiClient client = server.available();
if (client) {
if (client.connected())
{
Serial.println(".");
String request = client.readStringUntil('\r'); // receives the message from the client
float lightValue = request.toFloat();
client.flush();
interpretLightValue(lightValue);
}
client.stop(); // tarminates the connection with the client
}
}