-
Notifications
You must be signed in to change notification settings - Fork 0
/
conectarWifiPorAP_Vmysql.ino
380 lines (363 loc) · 10.8 KB
/
conectarWifiPorAP_Vmysql.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
//#include <MySQL_Encrypt_Sha1.h>
//#include <MySQL_Packet.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
WiFiServer server(80);
bool wifiConect=0; //leer la eeprom
bool passwordSave=0;
const char *ssid = "CAILL420";
const char *password = "420420420";
const char* host = "160.153.36.4";//"192.168.43.137";//"192.168.43.109";//"172.20.10.2";//"192.168.0.16";
const int httpsPort = 3306;
String search_next_word(String string,String Word,String fin);
//void AP_on(void);
String Token="";//leer EEPROM
String ID="";//leer EEPROM
String postData(String data);
IPAddress server_addr(192,168,20,60);//IPAddress server_addr(192,168,1,104); // IP of the MySQL *server* here
char user[] = "tomivm"; // MySQL user login username
char passwordMySQL[] = "Lofeagtopa1"; // MySQL user login password
// Sample query
bool postID=EEPROM.read(postIDPos); //leer EEPROM
WiFiClient client;
MySQL_Connection conn((Client *)&client);
String response[2][20];
byte colsNumber=0;
void AP_on (void) //encender el modo AP para poder conectar nuestro caill a la red WiFi
{
u8g2.clearDisplay();
u8g2.clearBuffer();
u8g2.drawStr( 2, 15,"\tRed Wifi:");
u8g2.drawStr( 60, 25,"CAILL420");
u8g2.drawStr( 4, 35,"Clave: ");
u8g2.drawStr( 60, 47,"420420420");
u8g2.drawStr( 50, 55,"cancelar");
u8g2.sendBuffer();
WiFi.mode(WIFI_AP_STA);
Serial.print("Configuring access point...");
WiFi.softAP("CAILL420","420420420");
server.begin();
IPAddress myIP = WiFi.softAPIP();
Serial.println(myIP);
Serial.println("HTTP server started");
wifiConect=1;
passwordSave=0;
WiFiClient client;
//WiFiClient client = server.available();
String clave = "";
String ssidWiFi = "";
while(!client){
client = server.available();
testear_botones();
if(enter)
{
enter=0;
return;
}
}
Serial.println("somebody has connected :)");
String request = client.readString();
//ssid=Fibertel WiFi718,clave=0141650064,
Serial.println(request);
ssidWiFi = search_next_word(request,"ssid=",",");
clave = search_next_word(request,"clave=",",");
Serial.println(ssidWiFi);
Serial.println(clave);
client.println("conectando");
if (clave=="NO"){
WiFi.begin(ssidWiFi.c_str());
}
else {
WiFi.begin(ssidWiFi.c_str(),clave.c_str());
}
bool conect=1;
static int timeOut=0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
timeOut++;
if(timeOut==35){
WiFi.disconnect();
client.println("contraseña erronea");
Serial.print("Contraseña erronea");
timeOut=0;
conect=0;
break;
}
Serial.print(timeOut);
Serial.print(".");
}
Serial.print(conect);
if(conect==1)
{
//passwordSave=1;
//escribir en la eEprom
client.print("wifi conectado");
client.println();
//delay(1500);
//client.stop();
server.stop();
// WiFi.mode(WIFI_STA);
Serial.println("");
Serial.println("WiFi connected");
// Serial.println("IP address: ");
// Serial.println(WiFi.localIP());
// // Use WiFiClientSecure class to create TLS connection
// //postData("title=foo&body=bar&userId=1&t=50");
if(postID==1)
{
strcpy (INSERT_DATA,"INSERT into caill.CAILLlist (idcaill) value ('%s')");
String convert = WiFi.macAddress();
convert.toCharArray(idcaill,35);
sprintf(query, INSERT_DATA,idcaill);//idcaill);
while(!insertData())
postID=0;
EEPROM.write(postIDPos , postID);
EEPROM.commit();
u8g2.clearDisplay() ;
u8g2.clearBuffer();
u8g2.drawStr(3, 1,"ya estas registrado :)");
delay(1500);
}
}
}
// String response = "";
// response=postData("title=PING&body=getToken");
// if(response == "error")
// {
// Serial.println("error de servidor");
// }
// else
// {
// Token=search_next_word(response,"Token",",");
// ID=search_next_word(response,"ID",",");
// getToken=0; //guardar en eeprom
// Serial.print(Token);
// }
// }
// else
// {
// String response = "";
// response = postData("title=PING");
// if(response == "error")
// Serial.println("error de servidor");
// }
bool insertData(void)
{
// if (conn.connect(server_addr, 3000, user, passwordMySQL)) {
// delay(1000);
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
// Save
// Execute the query
cur_mem->execute(query);
// Note: since there are no results, we do not need to read any data
// Deleting the cursor also frees up memory used
delete cur_mem;
Serial.println("Data recorded.");
// return 1;
// }
// else
// {
// Serial.println("Connection failed.");
// return 0;
// }
// conn.close();
}
void select (void)
{
Serial.println("> Running SELECT with dynamically supplied parameter");
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
// Supply the parameter for the query
// Here we use the QUERY_POP as the format string and query as the
// destination. This uses twice the memory so another option would be
// to allocate one buffer for all formatted queries or allocate the
// memory as needed (just make sure you allocate enough memory and
// free it when you're done!).
sprintf(query, QUERY_POP, idcaill);
// Execute the query
cur_mem->execute(query);
// Fetch the columns and print them
column_names *cols = cur_mem->get_columns();
byte y=0;
colsNumber=cols->num_fields;
for (int f = 0; f < cols->num_fields; f++) {
Serial.print(cols->fields[f]->name);
response[0][f]=cols->fields[f]->name;
Serial.print(response[0][f]);
if (f < cols->num_fields-1) {
Serial.print(',');
}
}
Serial.println();
y++;
// Read the rows and print them
row_values *row = NULL;
do {
row = cur_mem->get_next_row();
if (row != NULL) {
for (int f = 0; f < cols->num_fields; f++) {
Serial.print(row->values[f]);
response[y][f]=row->values[f];
if (f < cols->num_fields-1) {
Serial.print(',');
}
}
y++;
Serial.println();
}
} while (row != NULL);
delete cur_mem;
// if (y==1)
// {
// Serial.println("no hay cambio");
// return;
// }
}
bool connectionOpen(void){
if(conn.connect(server_addr, 3306, user, passwordMySQL))
{
delay(1000);
return 1;
}
else
return 0;
}
bool WiFiIsConected (void){
if(WiFi.status() == WL_CONNECTED) {
return 1;
}
else{
return 0;
}
}
void connectionClose(void){
conn.close();
}
String postData(String data)
{
String PostData = "";
WiFiClient client;
PostData = data;
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort))
{
Serial.println("connection failed");
return "error" ;
}
String url = "/";
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n"+
"Content-Length: " );
client.println(PostData.length());
client.println("\r\n"+
PostData);
client.print("Connection: close\r\n\r\n");
Serial.println("request sent");
while (client.connected())
{
String line = client.readStringUntil('\n');
if (line == "\r")
{
Serial.println("headers received");
break;
}
}
String line = client.readStringUntil('\n');
if (line.startsWith("{\"state\":\"success\""))
{
Serial.println("esp8266/Arduino CI successfull!");
}
else
{
Serial.println("esp8266/Arduino CI has failed");
}
Serial.println("reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
return line;
}
void SetAll(void)
{
byte x=1;
while(x < colsNumber)
{
switch (modo) {
case 1:
if(response[0][x] == "min temperature")
{
EEPROM.write(tempMinVPos,response[1][x].toInt());
Serial.println();
}
if(response[0][x] == "max temperature")
{
EEPROM.write(tempMaxVPos,response[1][x].toInt());
Serial.println(response[1][x].toInt());
Serial.println("tempemaxvcam");
}
if(response[0][x] == "min humedity")
{
EEPROM.write(humedadMinVPos,response[1][x].toInt());
Serial.println("humidityvcam");
}
if(response[0][x] == "max humedity")
{
EEPROM.write(humedadMaxVPos,response[1][x].toInt());
Serial.println("humiditymaxvcam");
}
configuradov=0;
break;
case 2:
if(response[0][x] == "min temperature")
{
EEPROM.write(tempMinFPos,response[1][x].toInt());
Serial.println("tempeminfcam");
}
if(response[0][x] == "max temperature")
{
EEPROM.write(tempMaxFPos,response[1][x].toInt());
Serial.println("tempemaxvcam");
}
if(response[0][x] == "min humedity")
{
EEPROM.write(humedadMinFPos,response[1][x].toInt());
Serial.println("humidityfcam");
}
if(response[0][x] == "max humedity")
{
EEPROM.write(humedadMinFPos,response[1][x].toInt());
Serial.println("humiditymaxcam");
}
configuradof=0;
break;
}
if(response[0][x] == "name")
{
nombrePerfil = response[1][x];
}
EEPROM.commit();
x++;
}
}
String search_next_word(String string,String Word,String fin)
{
int posicion=0;
if((posicion = string.indexOf(Word))== -1);
int offset = Word.length();
//Serial.println(posicion);
int Stop = string.indexOf(fin,posicion);
//Serial.println(string.substring(posicion+offset,Stop));
return string.substring(posicion+offset,Stop);
}