-
Notifications
You must be signed in to change notification settings - Fork 0
/
WiFi_handler.cpp
106 lines (100 loc) · 2.93 KB
/
WiFi_handler.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
#include "WiFi_handler.h"
WiFiHandler::WiFiHandler(EEPROMHandler* eeprom) {
this->eeprom = eeprom;
if(this->WiFiSwitchMode()) {
Serial.print("Connected\n");
} else {
Serial.print("Connect fail\n");
}
}
WiFiHandler::~WiFiHandler(void) {
this->WiFiShutdown();
}
bool WiFiHandler::WiFiSwitchMode(void) {
if(isDebug) {
Serial.print("WiFiSwitchMode()\n");
}
uint8_t mode = WiFi.getMode();
this->WiFiShutdown();
if(mode == WIFI_AP) {
Serial.print("Try connect to access point\n");
if( this->WiFiConnectToAP() ) {
Serial.print("Connected to access point\n");
return true;
}
}
Serial.print("Try create access point\n");
return this->WiFiCreateAP();
}
bool WiFiHandler::WiFiShutdown(void) {
if(isDebug){
Serial.print("WiFiShutdown(void)\n");
}
for ( int j = 0; j < WIFI_CNT_TRY; j++ ) {
if(WiFi.getMode() == WIFI_STA) {
Serial.print("Try disconnect as station\n");
if(WiFi.disconnect(true)) {
return true;
}
} else {
Serial.print("Try disconnect as access point\n");
if(WiFi.softAPdisconnect(true)) {
return true;
}
}
delay(WIFI_DELAY_TRY);
}
return false;
}
bool WiFiHandler::WiFiCreateAP(void) {
Serial.print("WiFiCreateAP(void)\n");
char* ssid = (char*)this->eeprom->getWifiAPSsid();
char* pass = (char*)this->eeprom->getWifiAPPass();
Serial.print(ssid);
Serial.print("\n");
Serial.print(pass);
Serial.print("\n");
IPAddress localIP(192,168,1,1); //TODO set from eeprom
IPAddress gateway(192,168,0,1); //TODO set from eeprom
IPAddress subnet(255,255,255,0); //TODO set from eeprom
WiFi.mode(WIFI_AP);
for ( int j = 0; j < 0; j++ ) {
delay(WIFI_DELAY_TRY);
if( WiFi.softAPConfig(localIP, gateway, subnet) ) {
Serial.print("Configuration was setted\n");
break;
} else {
Serial.print("Configuration not setted\n");
}
}
return WiFi.softAP(ssid, pass);
}
bool WiFiHandler::WiFiConnectToAP(void) {
Serial.print("WiFiConnectToAP(void)\n");
Serial.print((char*)this->eeprom->getWifiStSsid());
Serial.print("\n");
Serial.print((char*)this->eeprom->getWifiStPass());
Serial.print("\n");
if(!this->eeprom->isWifiStValid()) {
Serial.print("Not valid configuration\n");
return false;
}
char* ssid = (char*)this->eeprom->getWifiStSsid();
char* pass = (char*)this->eeprom->getWifiStPass();
WiFi.mode(WIFI_STA);
WiFi.setAutoReconnect(true);
WiFi.begin(ssid, pass, 0);
for ( int j = 0; j < WIFI_CNT_TRY; j++ ) {
delay(WIFI_DELAY_TRY);
if (WiFi.status() == WL_CONNECTED) {
Serial.print("=====\n");
WiFi.printDiag(Serial);
Serial.print("=====\n");
return true;
}
Serial.print(WiFi.status());
Serial.print("\n");
}
Serial.print("Connect WiFi failed ...\n");
return false;
}