-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoBeyond.cpp
219 lines (191 loc) · 7.13 KB
/
goBeyond.cpp
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
213
214
215
216
217
218
219
#include <Arduino.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include "goBeyond.h"
#include "arduino_secrets.h"
#include "config.h"
extern const int mtrLeftFwdPin;
extern const int mtrLeftRevPin;
extern const int mtrRightFwdPin;
// Auxiliary variables to store the current output state
String mtrLeftFwdState = "off";
String mtrLeftRevState = "off";
String mtrRightFwdState = "off";
String mtrRightRevState = "off";
String fwdState = "off";
String revState = "off";
String leftState = "off";
String rightState = "off";
// Variable to store the HTTP request
WiFiServer server(80);
void goBeyond::setup() {
ssid = SECRET_SSID;
password = SECRET_PASS;
currentTime = millis();
previousTime = 0;
Serial.begin(115200);
motorSetup();
wifiSetup();
}
// Initialize the output variables as outputs
void goBeyond::motorSetup() {
pinMode(mtrLeftFwdPin,OUTPUT);
pinMode(mtrLeftRevPin,OUTPUT);
pinMode(mtrRightFwdPin,OUTPUT);
pinMode(mtrRightRevPin,OUTPUT);
digitalWrite(mtrLeftFwdPin,LOW);
digitalWrite(mtrLeftRevPin,LOW);
digitalWrite(mtrRightFwdPin,LOW);
digitalWrite(mtrRightRevPin,LOW);
}
// Initializes the WiFi settings
void goBeyond::wifiSetup() {
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
// Enables or disables an action based on the button
void goBeyond::adjustAction(String header) {
if (header.indexOf("off") >= 0) {
disableAction(header);
} else if (header.indexOf("on") >= 0) {
enableAction(header);
}
}
void goBeyond::disableAction(String header) {
if (header.indexOf("GET /Forward/") >= 0) {
Serial.println("GPIO 9 and 5 off");
fwdState = "off";
digitalWrite(mtrLeftFwdPin, LOW);
digitalWrite(mtrRightFwdPin, LOW);
} else if (header.indexOf("GET /LeftTurn/") >= 0) {
Serial.println("GPIO 10 and 5 off");
leftState = "off";
digitalWrite(mtrLeftRevPin, LOW);
digitalWrite(mtrRightFwdPin, LOW);
} else if (header.indexOf("GET /RightTurn/") >= 0) {
Serial.println("GPIO 9 and 6 off");
rightState = "off";
digitalWrite(mtrLeftFwdPin, LOW);
digitalWrite(mtrRightRevPin, LOW);
} else if (header.indexOf("GET /Reverse/") >= 0) { //Backwards
Serial.println("GPIO 10 and 6 on");
revState = "off";
digitalWrite(mtrLeftRevPin, LOW);
digitalWrite(mtrRightRevPin, LOW);
}
}
void goBeyond::enableAction(String header) {
if (header.indexOf("GET /Forward/") >= 0) {
Serial.println("GPIO 9 and 5 on");
fwdState = "on";
digitalWrite(mtrLeftFwdPin, HIGH);
digitalWrite(mtrRightFwdPin, HIGH);
} else if (header.indexOf("GET /LeftTurn/") >= 0) {
Serial.println("GPIO 10 and 5 off");
leftState = "on";
digitalWrite(mtrLeftRevPin, HIGH);
digitalWrite(mtrRightFwdPin, HIGH);
} else if (header.indexOf("GET /RightTurn/") >= 0) {
Serial.println("GPIO 9 and 6 off");
rightState = "on";
digitalWrite(mtrLeftFwdPin, HIGH);
digitalWrite(mtrRightRevPin, HIGH);
} else if (header.indexOf("GET /Reverse/") >= 0) { //Backwards
Serial.println("GPIO 10 and 6 on");
revState = "on";
digitalWrite(mtrLeftRevPin, HIGH);
digitalWrite(mtrRightRevPin, HIGH);
}
}
// Responsible for how the server is displayed
void goBeyond::displayServer(WiFiClient client) {
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #417DC1; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #8B4513;}</style></head>");
client.println("<body><h1>Corbeau & Smaragdine Junior Design Team</h1>");
displayAction(client);
client.println("</body></html>");
client.println();
}
void goBeyond::displayAction(WiFiClient client) {
client.println("<p>Drive Forward - State " + fwdState + "</p>");
if (fwdState=="off") {
client.println("<p><a href=\"/Forward/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/Forward/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("<p>Turn Left - State " + leftState + "</p>");
if (leftState=="off") {
client.println("<p><a href=\"/LeftTurn/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/LeftTurn/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("<p>Turn Right - State " + rightState + "</p>");
if (rightState=="off") {
client.println("<p><a href=\"/RightTurn/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/RightTurn/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("<p>Drive in Reverse - State " + revState + "</p>");
if (revState=="off") {
client.println("<p><a href=\"/Reverse/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/Reverse/off\"><button class=\"button button2\">OFF</button></a></p>");
}
}
void goBeyond::loop() {
WiFiClient client = server.available();
if (client) {
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client.");
String currentLine = "";
while (client.connected() && currentTime - previousTime <= timeoutTime) {
currentTime = millis();
if (client.available()) {
char c = client.read();
Serial.write(c);
header += c;
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
adjustAction(header);
displayServer(client);
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}