-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwifi_manager.cpp
171 lines (137 loc) · 4.17 KB
/
wifi_manager.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
/*
* @Author: Jakub Witowski
* @Project name: iBeacon
* @File name: wifi_manager.cpp
*/
/* ==================================================================== */
/* ========================== include files =========================== */
/* ==================================================================== */
#include "wifi_manager.h"
/* ==================================================================== */
/* ======================== global variables ========================== */
/* ==================================================================== */
/* WiFi handler */
WiFi_Manager wifi;
/* NvM handler */
extern Nvm_Manager eeprom;
/* Web server handler */
extern Server_Manager server;
/* SSID and Password of the AP */
String ap_ssid;
String ap_pass;
/* WiFi establishing connection related flag */
bool establish_failed = false;
/* WiFi connection lost related flag */
bool connection_lost = false;
/* ==================================================================== */
/* ============================ functions ============================= */
/* ==================================================================== */
/*
* WiFi connect function
* - This function connects using the credentials stored in EEPROM
* - This function starts the websocket whec connection is estabilished
*/
void WiFi_Manager::WiFi_Connect()
{
/* Read SSID and PASSWORD for AP from NvM */
eeprom.Nvm_CredentialsRead(EEPROM_AP_CREDENTIALS_START_ADDR, ap_ssid, ap_pass);
/* Connect to the access point
*
* clear WIFI Credentials
* avoid to store WIFI configuration in Flash
* ensure WiFi mode is Station
*/
WiFi.disconnect(true);
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
Serial.printf("WIFI -> SSID: %s\r\n", ap_ssid.c_str());
Serial.printf("WIFI -> PASSWORD: %s\r\n", ap_pass.c_str());
WiFi.begin(ap_ssid.c_str(), ap_pass.c_str());
Serial.printf("WIFI -> Connecting\r\n");
WiFi.setAutoReconnect(true);
while(WIFI_IS_DISCONNECTED())
{
/* Wait for establishing the connection */
if(false != establish_failed)
{
/* Timeout occurred */
break;
}
delay(5);
}
if(WIFI_IS_DISCONNECTED())
{
/* Connecting broken due to timeout occurred */
Serial.printf("WIFI -> Connection timeout\r\n");
}
else
{
/* WiFi is connected */
Serial.printf("WIFI -> Connected\r\n");
WiFi_Restore();
}
}
/*
* WiFi restorenect function
* - This function starts Server
* - This function should be called when WiFi connection is established
*/
void WiFi_Manager::WiFi_Restore()
{
/* No timeout -> WiFi.status() == WL_CONNECTED */
char buf[16];
/* Get IP address */
IPAddress ip = WiFi.localIP();
/* Convert ip addres to char* and store it in the 'buf' */
sprintf(buf, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
printf("WIFI -> IP address: %s\r\n\r\n", buf);
/* Start server */
server.Server_Init();
}
/*
* Establish connection failed event
* - This function is called when establishing wifi connection is failed (only when wifi is initialized)
* - Time for establishing is declared in tmr_manager.h (TMR_ESTABLISH_CONNECTION_TIMEOUT_MS)
*/
void WiFi_Manager::WiFi_establish_connection_timeout_event()
{
establish_failed = true;
}
/*
* Reconnect failed event
* - This function is called when wifi reconnecting is failed
* - Time for reconnection is declared in tmr_manager.h (TMR_RECONNECT_TIMEOUT_MS)
*/
void WiFi_Manager::WiFi_reconnect_failed_timeout_event()
{
Serial.printf("WIFI -> Reconnect failed: RESET\r\n");
WiFi.disconnect();
ESP.restart();
/* Wait until the reset occurs */
while(1);
}
/*
* Set Connection lost flag
* - This function should be called to indicate lost WiFi connection
*/
void WiFi_Manager::WiFi_set_connection_lost_flag()
{
connection_lost = true;
}
/*
* Clear Connection lost flag
* - This function should be called to indicate restore WiFi connection
*/
void WiFi_Manager::WiFi_clear_connection_lost_flag()
{
connection_lost = false;
}
/*
* Get Connection lost flag
* - This function returns WiFi connection lost flag
*/
bool WiFi_Manager::WiFi_get_connection_lost_flag()
{
return connection_lost;
}
/* EOF */