-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.ino
212 lines (188 loc) · 4.75 KB
/
status.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <WiFiNINA.h>
#include <WS2812FX.h>
#include "utility/wifi_drv.h"
#define LED_COUNT 16
#define LED_PIN 6
#define STATUS_AVAILABLE 0
#define STATUS_BUSY 1
#define STATUS_INTERUPTIBLE 2
#define STATUS_OFF 3
#define STATUS_UNKNOWN 4
#define POLL_MILLIS 15000
WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
int cycle = 0;
char hostName[] = "10.0.0.134"; // pi or server address
int status = WL_IDLE_STATUS;
String inString = "";
int light_status = -1;
unsigned long poll_timer = 0;
WiFiClient client;
void setup() {
Serial.println("Wifi connection");
ws2812fx.init();
set_connecting();
ws2812fx.start();
ws2812fx.service();
resetConnection();
}
void resetConnection() {
while ( status != WL_CONNECTED && status != WL_CONNECT_FAILED && status != WL_NO_SSID_AVAIL) {
Serial.print("Attempting to connect to Wifi");
// Connect to WPA/WPA2 network:
status = WiFi.begin("", "");
delay(5000);
}
Serial.println("connected!");
printStatus();
}
void printStatus() {
Serial.print("Wifi status: ");
switch(status){
case WL_IDLE_STATUS:
Serial.println("Idle"); break;
case WL_NO_SSID_AVAIL:
Serial.println("SSID not found"); break;
case WL_SCAN_COMPLETED:
Serial.println("Scan complete"); break;
case WL_CONNECTED:
Serial.println("Connected"); break;
case WL_CONNECT_FAILED:
Serial.println("Conneciton Failed"); break;
case WL_CONNECTION_LOST:
Serial.println("Connection Lost"); break;
case WL_DISCONNECTED:
Serial.println("Disconnected"); break;
default:
Serial.print("Unknown - ");
Serial.println(status);
}
}
void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the Nina module
client.stop();
inString = "";
if (status != WL_CONNECTED) {
resetConnection();
}
// if there's a successful connection:
if (client.connect(hostName, 80)) {
Serial.println("connecting...");
// send the HTTP GET request:
client.println("GET /status HTTP/1.1");
client.println("Host: 10.0.0.134");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
while (!client.available()) {
Serial.print(".");
delay(10);
}
char prevchar1 = 0;
char prevchar2 = 0;
char prevchar3 = 0;
bool inbody = false;
while (client.available()) {
char c = client.read();
if (c == '\n' && prevchar1 == '\r' && prevchar2 == '\n' && prevchar3 == '\r'){
inbody = true;
}
prevchar3 = prevchar2;
prevchar2 = prevchar1;
prevchar1 = c;
if(inbody){
if (isDigit(c)) {
inString += c;
}
}
}
set_status(inString.toInt());
Serial.print("Value: ");
Serial.println(light_status);
} else {
// if you couldn't make a connection:
Serial.println("connection failed");
status = WiFi.status();
if(status == WL_CONNECTION_LOST) {
WiFi.disconnect();
}
set_status(STATUS_UNKNOWN);
printStatus();
}
}
void loop() {
unsigned long now = millis();
if(now < poll_timer) {
// overflow, just reset poll_timer to 0 and carry on (~once every 50 days)
poll_timer = 0;
}else if(now > poll_timer + POLL_MILLIS){
// refresh
poll_timer = now;
Serial.println("fetching data");
httpRequest();
}
ws2812fx.service();
}
void set_busy() {
Serial.println("busy");
ws2812fx.setBrightness(100);
ws2812fx.setColor(RED);
ws2812fx.setSpeed(10000);
ws2812fx.setMode(15);
}
void set_available() {
Serial.println("available");
ws2812fx.setBrightness(80);
ws2812fx.setColor(GREEN);
ws2812fx.setSpeed(5000);
ws2812fx.setMode(FX_MODE_STATIC);
}
void set_connecting() {
Serial.println("connecting");
ws2812fx.setBrightness(50);
ws2812fx.setColor(BLUE);
ws2812fx.setMode(FX_MODE_STATIC);
}
void set_interuptible() {
Serial.println("interuptible");
ws2812fx.setBrightness(100);
ws2812fx.setColor(ORANGE);
ws2812fx.setSpeed(1200);
ws2812fx.setMode(FX_MODE_RUNNING_LIGHTS);
}
void set_unknown() {
Serial.println("unknown");
ws2812fx.setBrightness(100);
ws2812fx.setSpeed(200);
ws2812fx.setMode(FX_MODE_RAINBOW_CYCLE);
}
void set_off() {
Serial.println("off");
ws2812fx.setBrightness(0);
ws2812fx.setMode(FX_MODE_STATIC);
}
void set_status(int new_status) {
if (light_status == new_status) {
return;
}
light_status = new_status;
switch(light_status){
case STATUS_AVAILABLE:
set_available();
break;
case STATUS_BUSY:
set_busy();
break;
case STATUS_INTERUPTIBLE:
set_interuptible();
break;
case STATUS_OFF:
set_off();
break;
case STATUS_UNKNOWN:
set_unknown();
break;
default:
set_unknown();
}
}