-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathWiFiHelper.cpp
194 lines (159 loc) · 5.75 KB
/
WiFiHelper.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
#include "GlobalDefine.h"
#include "WiFiHelper.h"
//#include <ESP8266HTTPClient.h> // includes WiFiClient.h
//#include <ESP8266WiFi.h>
#undef FOUND_BOARD
#ifdef ARDUINO_ARCH_ESP8266
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
extern "C" {
#include "user_interface.h"
#include "wpa2_enterprise.h"
}
#define FOUND_BOARD ESP8266
#endif
#ifdef ARDUINO_ARCH_ESP32
#include <HTTPClient.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include "esp_wpa2.h"
#define FOUND_BOARD ESP32
#endif
#ifdef ARDUINO_SAMD_MKRWIFI1010
#include <WiFiNINA.h>
#define FOUND_BOARD ARDUINO_SAMD_MKRWIFI1010
#endif
#ifndef FOUND_BOARD
#pragma message(Reminder "Error Target hardware not defined !")
#endif // ! FOUND_BOARD
String myMacAddress;
String HexString(byte buffer[], byte bufferSize) {
String res = "";
for (byte i = 0; i < bufferSize; i++) {
res += HEX_CHARS[(buffer[i] >> 4) & 0xF];
res += HEX_CHARS[buffer[i] & 0xF];
}
return res;
}
String wifiMacAddress() {
if (!myMacAddress || (myMacAddress == "")) {
myMacAddress = "";
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
myMacAddress = WiFi.macAddress(); // this returns 6 hex bytes, delimited by colons
#endif
#if defined(ARDUINO_SAMD_MKRWIFI1010)
byte mac[6];
WiFi.macAddress(mac);
myMacAddress = HexString(mac, 6);
#endif
myMacAddress.replace(":", "");
myMacAddress.replace("-", ""); // probably not used, but just in case they MAC address starts returning other well known delimiters such as dash
myMacAddress.replace(" ", ""); // or perhaps even a space
}
return myMacAddress;
}
void WiFiStart(bool EnterpriseMode) {
WIFI_DEBUG_PRINTLN(DEBUG_SEPARATOR);
WIFI_DEBUG_PRINT("wifiConnect: Connecting to ");
WIFI_DEBUG_PRINTLN(SECRET_WIFI_SSID);
WIFI_DEBUG_PRINTLN(DEBUG_SEPARATOR);
#ifdef ARDUINO_ARCH_ESP8266
WIFI_DEBUG_PRINTLN("ARDUINO_ARCH_ESP8266");
#endif
if (EnterpriseMode) {
// WPA2 Connection starts here
// Setting ESP into STATION mode only (no AP mode or dual mode)
WIFI_DEBUG_PRINTLN("Enterprise mode configured...");
#ifdef ARDUINO_ARCH_ESP8266
WiFi.disconnect(true);
// WPA2 Connection starts here
// Setting ESP into STATION mode only (no AP mode or dual mode)
WIFI_DEBUG_PRINTLN("Starting ESP8266 Enterprise WiFi...");
wifi_set_opmode(STATION_MODE);
struct station_config wifi_config;
memset(&wifi_config, 0, sizeof(wifi_config));
strcpy((char*)wifi_config.ssid, SECRET_WIFI_SSID);
wifi_station_set_config(&wifi_config);
wifi_station_clear_cert_key();
wifi_station_clear_enterprise_ca_cert();
wifi_station_set_wpa2_enterprise_auth(1);
wifi_station_set_enterprise_identity((uint8*)SECRET_EAP_ID, strlen(SECRET_EAP_ID));
wifi_station_set_enterprise_username((uint8*)SECRET_EAP_USERNAME, strlen(SECRET_EAP_USERNAME));
wifi_station_set_enterprise_password((uint8*)SECRET_EAP_PASSWORD, strlen(SECRET_EAP_PASSWORD));
wifi_station_connect();
// WPA2 Connection ends here
#endif
#ifdef ARDUINO_ARCH_ESP32
WiFi.disconnect(true);
WIFI_DEBUG_PRINTLN("Starting ESP32 Enterprise WiFi...");
WiFi.mode(WIFI_STA); // be sure to set mode FIRST
esp_wifi_sta_wpa2_ent_set_identity((uint8_t*)SECRET_EAP_ID, strlen(SECRET_EAP_ID)); //provide identity
esp_wifi_sta_wpa2_ent_set_username((uint8_t*)SECRET_EAP_USERNAME, strlen(SECRET_EAP_USERNAME)); //provide username
esp_wifi_sta_wpa2_ent_set_password((uint8_t*)SECRET_EAP_PASSWORD, strlen(SECRET_EAP_PASSWORD)); //provide password
esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT();
esp_wifi_sta_wpa2_ent_enable(&config);
WiFi.begin(SECRET_WIFI_SSID);
#endif
#ifdef ARDUINO_SAMD_MKRWIFI1010
WiFi.disconnect();
WIFI_DEBUG_PRINTLN("Starting WiFiNINA Enterprise WiFi...(not implemented in early WiFiNINA Versions, see Version 1.5.0 or later)");
WiFi.beginEnterprise(SECRET_WIFI_SSID, SECRET_EAP_USERNAME, SECRET_EAP_PASSWORD);
#endif // ARDUINO_SAMD_MKRWIFI1010
}
else {
WIFI_DEBUG_PRINTLN("Starting regular Wi-Fi...");
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
WiFi.mode(WIFI_STA);
#else
// station mode assumed.
#endif
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
WiFi.begin(SECRET_WIFI_SSID, SECRET_WIFI_PWD);
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
}
}
//*******************************************************************************************************************************************
// wifiConnect
//
// WiFi.begin with repeated attempts with TFT screen and optional serial progress indication
//
//*******************************************************************************************************************************************
int wifiConnect(int maxAttempts) {
int countAttempt = 0;
WiFiStart(IS_EAP); // see GlobalDefine.h to set Enterprise Access Point on or off
WIFI_DEBUG_PRINTLN("Starting WiFi Connection Loop...");
while (WiFi.status() != WL_CONNECTED) { // try to connect wifi for 6 sec then reset
// this tft code is not actualy DOING anything yet
WIFI_DEBUG_PRINT(".");
delay(250);
if (IS_EAP) {
// TODO - do we ever give up on EAP?
}
else
{
countAttempt++;
if (countAttempt > maxAttempts) {
countAttempt = 0;
WIFI_DEBUG_PRINTLN(F("WiFi Disconnect... "));
WiFi.disconnect();
delay(5000);
WIFI_DEBUG_PRINT(F("WiFi Retrying. "));
WIFI_DEBUG_PRINTLN(F(SECRET_WIFI_SSID));
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
WiFi.mode(WIFI_STA);
#else
// station mode assumed.
#endif
WiFi.begin(SECRET_WIFI_SSID, SECRET_WIFI_PWD);
// TODO reboot?
}
}
}
WIFI_DEBUG_PRINTLN("WiFi Connected!");
delay(5000); // TODO why wait?
Serial.println("MAC Address=" + wifiMacAddress());
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
WIFI_DEBUG_PRINT("wifiConnect: Done!");
return 0;
}