-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconexion.ino
72 lines (62 loc) · 2.07 KB
/
conexion.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
void connectWIFI(){ // Connect to Wi-Fi // try to connect to existing network
int connect_timeout;
WiFi.setHostname(hostname.c_str()); //define hostname
Serial.println("Begin wifi...");
ssid = nvs.getString("ssid", "");
password = nvs.getString("password", "");
if (ssid == "" || password == ""){
modo_wifi_cliente = false;
Serial.println("No values saved for ssid or password");
Serial.println("\nCreating access point...");
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(IPAddress(192, 168, 4, 1), IPAddress(192, 168, 4, 1), IPAddress(255, 255, 255, 0));
WiFi.softAP("Anakino_control");
connect_timeout = 20;
do {
delay(250);
Serial.print(",");
connect_timeout--;
}
while(connect_timeout);
}
////////// MODO CLIENTE . ///////////////////////////////////////
else {
modo_wifi_cliente= true;
Serial.println("Modo Cliente");
Serial.println("\nTry to connect to: ");
WiFi.setHostname(hostname.c_str()); //define hostname
WiFi.mode(WIFI_STA);
delay(2000);
WiFi.begin(ssid.c_str(), password.c_str());
Serial.println(ssid.c_str());
Serial.println(password.c_str());
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print('.');
if (contador_2 == 60) {
ESP.restart(); // Restart ESP
}
contador_2++;
delay(1000);
}
Serial.print("Connected to AP");
Serial.print("RRSI: ");
Serial.println(WiFi.RSSI());
}
Serial.print("Modo: ");
Serial.println(WiFi.getMode() == WIFI_AP ? "Station" : "Client");
Serial.print("IP address: ");
Serial.println(WiFi.getMode() == WIFI_AP ? WiFi.softAPIP() : WiFi.localIP());
delay(1000);
}
void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("Disconnected from WiFi access point");
Serial.print("WiFi lost connection. Reason: ");
Serial.println(info.wifi_sta_disconnected.reason);
Serial.println("Trying to Reconnect");
WiFi.begin(ssid.c_str(), password.c_str());
contador_2++;
if (contador_2 == 5) {
ESP.restart(); // Restart ESP
}
}