-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwifi.cpp
More file actions
165 lines (130 loc) · 3.97 KB
/
wifi.cpp
File metadata and controls
165 lines (130 loc) · 3.97 KB
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
#include <Arduino.h>
#include <Preferences.h>
#include <WiFi.h>
#include "constants.h"
#include "debug.h"
#include "wifi.h"
// Set your Static IP address
IPAddress local_IP(192, 168, 5, 1);
// Set your Gateway IP address
IPAddress gateway(192, 168, 5, 1);
IPAddress subnet(255, 255, 255, 0);
// IPAddress primaryDNS(8, 8, 8, 8); // optional
// IPAddress secondaryDNS(8, 8, 4, 4); // optional
wifi::wifi() {
}
bool wifi::connected() {
return WiFi.status() == WL_CONNECTED;
}
void wifi::disable() {
Serial.println(F("Disable network WiFi..."));
WiFi.softAPdisconnect(true);
WiFi.mode(WIFI_OFF);
Serial.println(F("...network WiFi disabled successful."));
}
bool wifi::enabled() {
return WiFi.getMode() != WIFI_OFF;
}
String wifi::ipAddress() {
return _ipAddress;
}
void wifi::loop() {
// if (WiFi.status() == WL_CONNECTED) {
// Serial.print(F("WiFi connected..."));
// }
}
String wifi::password() {
return _password;
}
void wifi::reset() {
Serial.println(F("Reset network WiFi..."));
_reset();
Serial.println(F("...network WiFi reset successful."));
}
void wifi::save(const char * password, const char * ssId) {
Serial.println(F("Saving network WiFi preferences..."));
// TODO
// disable();
// TODO
// Preferences preferences;
// preferences.begin(PREFERENCE_KEY, false);
// _password = password;
// preferences.putString(PREFERENCE_KEY_WIFI_PASSWORD, _password);
// _ssId = ssId;
// preferences.putString(PREFERENCE_KEY_WIFI_SSID, _ssId);
// preferences.end();
// TODO
// start();
Serial.println(F("...network WiFi preferences saved successful."));
}
String wifi::ssid() {
return _ssId;
}
void wifi::setup() {
Serial.println(F("\nSetup network WiFi..."));
_reset();
Serial.println(F("...network WiFi setup successful."));
}
void wifi::start() {
Serial.println(F("Start network WiFi..."));
debug("password", _password);
WiFi.softAP(_ssId, _password);
// if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
if (!WiFi.softAPConfig(local_IP, gateway, subnet)) {
Serial.println(F("WiFi: STA Failed to configure"));
Serial.println(F("...network WiFi failed."));
return;
}
_ipAddress = WiFi.softAPIP().toString();
Serial.println(F(""));
Serial.print(F("WiFi IP address: "));
Serial.println(_ipAddress);
Serial.println(F(""));
Serial.println(F("...network WiFi started successful."));
}
void wifi::_determineSsid() {
char macAddress[14];
snprintf(macAddress, 14, "%llX", ESP.getEfuseMac());
// debug(F("macAddress"), macAddress); //
String macAddressAsStr = macAddress ; // convert from char to String
// debug(F("macAddressAsStr"), macAddressAsStr); //
int strLen = macAddressAsStr.length();
// debug(F("Length of the string %i characters \r\n"), macAddressAsStr);
String macAddressF = "";
String temp;
int i = 0; while ( i < 6) {
temp = macAddressAsStr.substring(strLen - 2 - 2 * i, strLen - 2 * i);
macAddressF = macAddressF + temp + ":";
if (i > 2)
_ssId = _ssId + temp;
i++ ;
}
macAddressF = macAddressF.substring(0,17); // remove last :
// Serial.print(F("macAddress="));
// Serial.println(macAddressF);
// Serial.print(F("_ssId="));
// Serial.println(_ssId);
// String serialnumber = "-" ;
// int j = 3;
// while ( j < 6) {
// serialnumber = serialnumber + ssid1.substring(strLen - 2 - 2 * j, strLen - 2 * j);
// j++;
// }
// debug(F("serialnumber"), serialnumber);
Serial.println(F(""));
Serial.print(F("WiFi SSID: "));
Serial.println(_ssId);
Serial.println(F(""));
}
void wifi::_reset() {
_determineSsid();
Preferences preferences;
preferences.begin(PREFERENCE_KEY, false);
_ssId = preferences.getString(PREFERENCE_KEY_WIFI_SSID, _ssId);
preferences.putString(PREFERENCE_KEY_WIFI_SSID, _ssId);
_password = preferences.getString(PREFERENCE_KEY_WIFI_PASSWORD, "password123");
// _password = "password123";
preferences.putString(PREFERENCE_KEY_WIFI_PASSWORD, _password);
preferences.end();
}
wifi _wifi;