-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
342 lines (305 loc) · 8.46 KB
/
main.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
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
#include <Arduino.h>
#include <TimeLib.h>
#include <TimeAlarms.h>
#include "main.h"
#include "laser.h"
#include "config.h"
#define AWAKE 4 //Despertar WIFI
#define FACTORY 7 //factory RESET/AP RN131
#define INT_ANT "0"
#define EXT_ANT "1"
#define OPEN "0"
#define WEP "1"
#define WPA1 "2"
#define WPA2 "4"
#define WEP64 "8"
AlarmId id;
char* mySSID = SSID_NAME;
char* myPassword = WPA2_PASS;
char* wifiEncript = WPA2;
char* antenna = ANTENA_CONFIG;
boolean connected;
#define buffer_length 32
static char buffer[buffer_length];
void sckBegin() {
Serial.begin(115200);
Serial1.begin(9600);
pinMode(AWAKE, OUTPUT);
pinMode(FACTORY, OUTPUT);
}
boolean sckFindInResponse(const char *toMatch,
unsigned int timeOut = 1000) {
int byteRead;
unsigned long timeOutTarget; // in milliseconds
for (unsigned int offset = 0; offset < strlen(toMatch); offset++) {
timeOutTarget = millis() + timeOut; // Doesn't handle timer wrapping
while (!Serial1.available()) {
// Wait, with optional time out.
if (timeOut > 0) {
if (millis() > timeOutTarget) {
return false;
}
}
delay(1); // This seems to improve reliability slightly
}
byteRead = Serial1.read();
//Serial.print((char)byteRead);
delay(1); // Removing logging may affect timing slightly
if (byteRead != toMatch[offset]) {
offset = 0;
// Ignore character read if it's not a match for the start of the string
if (byteRead != toMatch[offset]) {
offset = -1;
}
continue;
}
}
return true;
}
void sckRecovery()
{
Serial.println("Reseting...");
digitalWrite(FACTORY, HIGH);
delay(1000);
digitalWrite(FACTORY, LOW);
delay(1000);
digitalWrite(FACTORY, HIGH);
delay(1000);
digitalWrite(FACTORY, LOW);
delay(1000);
digitalWrite(FACTORY, HIGH);
delay(1000);
digitalWrite(FACTORY, LOW);
delay(1000);
digitalWrite(FACTORY, HIGH);
delay(1000);
digitalWrite(FACTORY, LOW);
delay(1000);
digitalWrite(FACTORY, HIGH);
delay(1000);
digitalWrite(FACTORY, LOW);
delay(1000);
Serial1.println();
sckFindInResponse("<WEB_APP", 3000);
sckReset();
Serial.println("Successfully reset");
//digitalWrite(FACTORY, HIGH);
sckEnterCommandMode();
}
void sckSkipRemainderOfResponse(unsigned int timeOut) {
unsigned long time = millis();
while (((millis()-time)<timeOut))
{
if (Serial1.available())
{
byte temp = Serial1.read();
//Serial.write(temp);
time = millis();
}
}
}
boolean sckSendCommand(const __FlashStringHelper *command,
boolean isMultipartCommand = false,
const char *expectedResponse = "AOK") {
Serial1.print(command);
delay(20);
if (!isMultipartCommand) {
Serial1.flush();
Serial1.println();
// TODO: Handle other responses
// (e.g. autoconnect message before it's turned off,
// DHCP messages, and/or ERR etc)
if (!sckFindInResponse(expectedResponse, 3000)) {
return false;
}
//sckFindInResponse(expectedResponse);
}
return true;
}
boolean sckSendCommand(const char *command,
boolean isMultipartCommand = false,
const char *expectedResponse = "AOK") {
Serial1.print(command);
delay(20);
if (!isMultipartCommand) {
Serial1.flush();
Serial1.println();
// TODO: Handle other responses
// (e.g. autoconnect message before it's turned off,
// DHCP messages, and/or ERR etc)
if (!sckFindInResponse(expectedResponse, 3000)) {
return false;
}
//findInResponse(expectedResponse);
}
return true;
}
#define COMMAND_MODE_ENTER_RETRY_ATTEMPTS 2
#define COMMAND_MODE_GUARD_TIME 250 // in milliseconds
boolean sckEnterCommandMode() {
for (int retryCount = 0; retryCount < COMMAND_MODE_ENTER_RETRY_ATTEMPTS; retryCount++)
{
delay(COMMAND_MODE_GUARD_TIME);
Serial1.print(F("$$$"));
delay(COMMAND_MODE_GUARD_TIME);
Serial1.println();
Serial1.println();
if (sckFindInResponse("\r\n<", 1000))
{
return true;
}
}
return false;
}
boolean sckSleep() {
sckEnterCommandMode();
sckSendCommand(F("sleep"));
}
boolean sckReset() {
//sckEnterCommandMode();
sckSendCommand(F("factory R"), false, "Set Factory Defaults"); // Store settings
sckSendCommand(F("save"), false, "Storing in config"); // Store settings
delay(1000);
sckSendCommand(F("reboot"), false, "*READY*");
}
boolean sckUpdate() {
if (sckEnterCommandMode())
{
sckSendCommand(F("ftp update"));
delay(1000);
if (sckFindInResponse("FTP OK.", 10000))
{
return true;
}
}
else return false;
}
boolean sckExitCommandMode() {
for (int retryCount = 0; retryCount < COMMAND_MODE_ENTER_RETRY_ATTEMPTS; retryCount++)
{
if (sckSendCommand(F("exit"), false, "EXIT"))
{
return true;
}
}
return false;
}
boolean sckConnect()
{
if (!sckReady())
{
if(sckEnterCommandMode())
{
sckSendCommand(F("set wlan join 1")); // Disable AP mode
sckSendCommand(F("set ip dhcp 1")); // Enable DHCP server
//sckSendCommand(F("set ip proto 10")); //Modo TCP y modo HTML
//sckSendCommand(F("set ip local 80"));
sckSendCommand(F("set wlan auth "), true);
sckSendCommand(wifiEncript);
boolean mode = true;
if ((wifiEncript==WEP)||(wifiEncript==WEP64)) mode=false;
sckSendCommand(F("set wlan ssid "), true);
sckSendCommand(mySSID);
if (mode) sckSendCommand(F("set wlan phrase "), true); // WPA1, WPA2, OPEN
else sckSendCommand(F("set wlan key "), true);
sckSendCommand(myPassword);
sckSendCommand(F("set wlan ext_antenna "), true);
sckSendCommand(antenna);
Serial.println("setup mode");
sckSendCommand(F("save"), false, "Storing in config"); // Store settings
sckSendCommand(F("reboot"), false, "*READY*");
Serial.println("after reboot");
return true;
}
else return false;
}
else return true;
}
uint32_t baud[7]={2400, 4800, 9600, 19200, 38400, 57600, 115200};
void sckRepair()
{
if(!sckEnterCommandMode())
{
boolean repair = true;
for (int i=6; ((i>=0)&&repair); i--)
{
Serial1.begin(baud[i]);
Serial.println(baud[i]);
if(sckEnterCommandMode())
{
sckReset();
repair = false;
}
Serial1.begin(9600);
}
}
}
boolean sckReady()
{
if(!sckEnterCommandMode())
{
sckRepair();
}
if (sckEnterCommandMode())
{
Serial1.println(F("join"));
if (sckFindInResponse("Associated!", 8000))
{
sckSkipRemainderOfResponse(3000);
sckExitCommandMode();
return(true);
}
}
else return(false);
}
char* itoa(int32_t number)
{
byte count = 0;
uint32_t temp;
if (number < 0) {temp = number*(-1); count++;}
while ((temp/10)!=0)
{
temp = temp/10;
count++;
}
int i;
if (number < 0) {temp = number*(-1);}
else temp = number;
for (i = count; i>=0; i--)
{
buffer[i] = temp%10 + '0';
temp = temp/10;
}
if (number < 0) {buffer[0] = '-';}
buffer[count + 1] = 0x00;
return buffer;
}
void setup() {
pin_init();
sckBegin();
//sckConnect();
//sckRepair();
sckConnect();
setTime(8,00,0,12,22,18); // set time to Saturday 8:00:00am Dec 2 2018
Alarm.alarmRepeat(8,30,0, turnOn); // 8:30am every day
Alarm.alarmRepeat(17,45,0, tunrOn); // 5:45pm every day
//sckEnterCommandMode();
}
void loop() {
if(check_autoplay())
{
autoplay_randomPos();
}
//handle_connection(0);
if (Serial.available())
{
int inByte = Serial.read();
Serial1.write(inByte);
}
if (Serial1.available()) {
String line = Serial1.readStringUntil('\n');
checkCommand(line);
Serial.println(line);
Serial.println("");
}
}