-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c.ino
183 lines (146 loc) · 4.73 KB
/
main.c.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
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <FS.h>
#include "CommandParser.h"
Infrastructure::CommandParser commandParser(Serial);
String ssid = String();
String password = String();
bool isFSInitalized = false;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.println("Booting");
commandParser.addCommandDefinition(String("connect"), connectCommandHandler);
commandParser.addCommandDefinition(String("disconnect"), disconnectCommandHandler);
commandParser.addCommandDefinition(String("format"), formatCommandHandler);
commandParser.addCommandDefinition(String("status"), statusCommandHandler);
commandParser.addCommandDefinition(String("ssid"), ssidCommandHandler);
commandParser.addCommandDefinition(String("password"), passwordCommandHandler);
Serial.println("Parser initialised");
isFSInitalized = SPIFFS.begin();
Serial.println("Ready");
}
void loop() {
commandParser.process();
yield();
}
void ssidCommandHandler(Stream & stream) {
char ssidBuffer[64];
parseString(stream, ssidBuffer, sizeof(ssidBuffer));
if (strlen(ssidBuffer) > 0) {
stream.println("SSID configured");
ssid = String(ssidBuffer);
} else {
stream.println("SSId must not be empty");
}
}
void passwordCommandHandler(Stream & stream) {
char passwordBuffer[64];
parseString(stream, passwordBuffer, sizeof(passwordBuffer));
if (strlen(passwordBuffer) > 0) {
stream.println("Password configured");
password = String(passwordBuffer);
} else {
stream.println("Password must not be empty");
}
}
void connectCommandHandler(Stream & stream) {
if ((ssid.length() > 0) && (password.length() > 0)) {
disconnectCommandHandler(stream);
connectWiFi(stream, ssid, password);
}
else {
stream.println("For a connection SSID and password must be configured");
}
}
void disconnectCommandHandler(Stream & stream) {
if (WiFi.status() == WL_CONNECTED) {
WiFi.disconnect(true);
stream.println("Disconnected");
}
}
void formatCommandHandler(Stream & stream) {
if (SPIFFS.format()) {
stream.println("File system formatted");
if (isFSInitalized == false) {
isFSInitalized = SPIFFS.begin();
}
} else {
stream.println("Formatting file system failed");
}
}
void statusCommandHandler(Stream & stream) {
if (WiFi.status() == WL_CONNECTED) {
stream.printf("Connected to '%s' using ip address '%s'\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
} else {
stream.println("Disconnected");
}
if (isFSInitalized) {
stream.println("File system is formatted");
} else {
stream.println("File system is not formatted");
}
stream.printf("SSID configured to '%s'\n", ssid.c_str());
if (password.length() > 0) {
stream.println("Password is configured");
} else {
stream.println("Password is not configured");
}
stream.printf("Sesor reading is %d\n", getSensorReading());
}
int getSensorReading() {
return analogRead(A0);
}
void connectWiFi(Stream & stream, String ssid, String password)
{
stream.println("Connecting to " + quoteString(ssid) + String(" using mac address ") + WiFi.macAddress());
WiFi.mode(WIFI_STA);
WiFi.begin(ssid.c_str(), password.c_str());
for (int i = 1; (i < 60 && WiFi.status() != WL_CONNECTED); i++) {
delay(1000);
}
if (WiFi.status() == WL_CONNECTED) {
stream.println(String("IP address is ") + String(WiFi.localIP().toString()));
stream.println(String("Connection to ") + quoteString(ssid) + String(" was successful."));
} else {
stream.println(String("Connection to ") + quoteString(ssid) + String(" failed"));
}
}
String quoteString(String string) {
return String("\"") + string + String("\"");
}
void parseString(Stream & stream, char * buffer, size_t size) {
int index = 0;
bool stringStartFound = false;
bool stringEndFound = false;
while (!stringStartFound) {
int nextChar = stream.read();
switch (nextChar) {
case '"':
stringStartFound = true;
break;
default:
yield();
break;
}
}
while (!stringEndFound) {
int nextChar = stream.read();
switch (nextChar) {
case -1:
yield();
break;
case '"':
stringEndFound = true;
break;
default:
if (index < size - 1) {
buffer[index] = nextChar;
buffer[index + 1] = 0;
index++;
}
break;
}
}
}