-
Notifications
You must be signed in to change notification settings - Fork 2
/
WirelessMD.ino
363 lines (276 loc) · 7.53 KB
/
WirelessMD.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include <ESP8266WebServer.h>
#include <FS.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include "config.h"
IPAddress local_IP(192, 168, 4, 22);
IPAddress gateway(192, 168, 4, 9);
IPAddress subnet(255, 255, 255, 0);
ESP8266WebServer server (80);
struct wifiConfig {
String ssid;
String password;
};
//This function takes the parameters passed in the URL(the x and y coordinates of the joystick)
//and sets the motor speed based on those parameters.
void handleJSData()
{
boolean yDir;
int x = server.arg(0).toInt() * 10;
int y = server.arg(1).toInt() * 10;
int aSpeed = abs(y);
int bSpeed = abs(y);
//set the direction based on y being negative or positive
if ( y < 0 ) {
yDir = 0;
}
else {
yDir = 1;
}
//adjust to speed of each each motor depending on the x-axis
//it slows down one motor and speeds up the other proportionately
//based on the amount of turning
aSpeed = constrain(aSpeed + x / 2, 0, 1023);
bSpeed = constrain(bSpeed - x / 2, 0, 1023);
//use the speed and direction values to turn the motors
//if either motor is going in reverse from what is expected,
//just change the 2 digitalWrite lines for both motors:
//!ydir would become ydir, and ydir would become !ydir
Serial.print("x=");
Serial.print(aSpeed);
Serial.print("y=");
Serial.println(bSpeed);
//MotorA
digitalWrite(AIN1, !yDir);
digitalWrite(AIN2, yDir);
analogWrite(PWMA, aSpeed);
//MotorB
digitalWrite(BIN1, !yDir);
digitalWrite(BIN2, yDir);
analogWrite(PWMB, bSpeed);
//return an HTTP 200
server.send(200, "text/plain", "");
}
void stopDriver(){
analogWrite(PWMA, 0);
analogWrite(PWMB, 0);
}
struct wifiConfig get_id_pass(String filepath)
{
wifiConfig ap;
//Serial.println(filepath);
File file = SPIFFS.open(filepath, "r");
ap.ssid = file.readStringUntil('\n');
ap.password = file.readStringUntil('\n');
file.close();
return ap;
}
void reset_controller()
{
Serial.println("Reset pressed");
server.send(200, "text/plain", "");
ESP.restart();
}
void initGpio()
{
pinMode(PWMA, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
}
void writeToFile(String id, String pass)
{ String t_location = "/wifi/temp.txt";
File temp = SPIFFS.open(SAPfile, "w");
Serial.print("id length:");
Serial.println(id.length());
Serial.print("password length:");
Serial.println(pass.length());
if (!temp) {
Serial.println("Error opening file for writing");
return;
}
else
{
temp.println(id);
//temp.print(',');
temp.println(pass);
temp.close();
}
SPIFFS.gc();
}
void getSapdata()
{
Serial.println(server.args());
String ssid = server.arg("ssid");
String password = server.arg("password");
Serial.println(ssid);
Serial.println(password);
server.send(200, "text/plain", "");
writeToFile(ssid, password);
}
void apconnect()
{
if (WiFi.getMode() != WIFI_AP)
{
server.stop();
set_apmode();
beginWebserver(1);
}
}
void staconnect()
{
if (WiFi.getMode() != WIFI_STA) {
server.stop();
set_stamode();
beginWebserver(1);
}
}
void beginWebserver(bool mode_flag)
{
set_dns();
initGpio();
server.serveStatic("/", SPIFFS, "/home.html");
//call handleJSData function when this URL is accessed by the js in the html file
server.on("/home.html", reset_controller);
server.serveStatic("/mode/", SPIFFS, "/mode.html");
server.serveStatic("/ap_home/", SPIFFS, "/ap_home.html");
server.serveStatic("/ap_home/view/", SPIFFS, "/displayap.html");
server.serveStatic("/sap_home/view/", SPIFFS, "/displaysap.html");
server.serveStatic("/sap_home/", SPIFFS, "/sap_home.html");
server.serveStatic("/sap_home/form/", SPIFFS, "/form.html");
server.on("/ap_home/ap_home.html", apconnect);
server.on("/sap_home/form/form.html", getSapdata);
server.on("/sap_home/sap_home.html", staconnect);
server.serveStatic("/control/", SPIFFS, "/joystick.html");
server.serveStatic("/control/virtualjoystick.js", SPIFFS, "/virtualjoystick.js");
//call handleJSData function when this URL is accessed by the js in the html file
server.on("/control/jsData.html", handleJSData);
server.begin();
}
bool filecheck(String filepath) {
return SPIFFS.exists(filepath);
}
void AP_mode(struct wifiConfig apSettings)
{
WiFi.mode(WIFI_OFF);
Serial.print("SSID:");
Serial.println(apSettings.ssid);
Serial.print("PASSWORD:");
Serial.println(apSettings.password);
WiFi.mode(WIFI_AP);
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
WiFi.setAutoConnect(false);
WiFi.setAutoReconnect(false); // if wifi attempts to (re)connect to a previous router it kills the access point
WiFi.softAP(apSettings.ssid, apSettings.password, 10, false, 1);
Serial.print("Setting AP ... ");
Serial.print("AP IP address = ");
Serial.println(WiFi.softAPIP());
}
void wait_for_dns() {
while (1) {
delay(1000);
Serial.println("[ERROR] MDNS responder did not setup for ap mode ");
}
}
void set_dns()
{
if (WiFi.getMode() == WIFI_AP) {
if (!MDNS.begin("wirelessmd", WiFi.softAPIP())) {
Serial.println("[ERROR] MDNS responder did not setup for ap mode ");
wait_for_dns();
}
else Serial.println("DNS server successfully created for AP mode");
MDNS.addService("http", "tcp", 80);
}
else {
if (!MDNS.begin("wirelessmd")) {
Serial.println("[ERROR] MDNS responder did not setup STA mode");
wait_for_dns();
}
else Serial.println("DNS server successfully created for STA mode");
}
}
void set_apmode()
{
struct wifiConfig apSettings;
if (!filecheck(APfile) )
{
Serial.println("failed to open AP and SAP settings defaulting ssid and password");
apSettings.ssid = "ESP8266";
apSettings.password = "Helloworld";
AP_mode(apSettings);
}
else
{
apSettings = get_id_pass(APfile);
AP_mode(apSettings);
}
}
bool set_stamode()
{
struct wifiConfig apSettings;
bool mode_flag = 0;
int retries = 30;
Serial.println("running ESP in SoftAP mode");
apSettings = get_id_pass(SAPfile);
Serial.print("connecting ESP to:");
Serial.print(apSettings.ssid);
Serial.print("PASSWORD:");
Serial.print(apSettings.password);
delay(2000);
WiFi.mode(WIFI_STA);
WiFi.setAutoConnect(true);
WiFi.setAutoReconnect(true); // if wifi attempts to (re)connect to a previous router it kills the access point
//WiFi.begin(id,pass); //Connect to your WiFi router
WiFi.begin(apSettings.ssid.c_str(), apSettings.password.c_str()); //Connect to your WiFi router
while (WiFi.status() != WL_CONNECTED && retries > 0) {
delay(1000);
Serial.print(".");
retries -= 1;
}
Serial.println();
if (retries == 0) {
Serial.println("failed to connect to network in SoftAp mode chnaging to ap mode");
}
else {
mode_flag = 1;
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
//set ap mode if sta mode is failed
if (WiFi.status() != WL_CONNECTED) {
set_apmode();
}
return mode_flag;
}
void setup()
{
bool mode_flag = 0;
WiFi.mode(WIFI_OFF);
Serial.begin(115200);
delay(2000);
if (!SPIFFS.begin())
{
Serial.println("SPIFFS Mount failed");
ESP.restart();
}
else
{
Serial.println("SPIFFS Mount succesfull");
}
if (!filecheck(SAPfile)) {
Serial.println("failed to open SoftAP changing mode to AP");
}
else {
mode_flag = set_stamode();
}
beginWebserver(mode_flag);
}
void loop()
{
MDNS.update();
server.handleClient();
}