-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi.ino
276 lines (254 loc) · 5.79 KB
/
wifi.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <FirebaseArduino.h>
#include <EEPROM.h>
#define FIREBASE_HOST "<your host key>"
#define FIREBASE_AUTH "<your auth key>"
#define wifi_connect WiFi.status() == WL_CONNECTED
String ssid, pass;
char* dssid = "test";
char* dpass = "123456789";
boolean hotspot = false, firebase = false, hotclient = false;
ESP8266WebServer server(80);
String st, content;
int n;
int statusCode;
void setup()
{
Serial.begin(9600);
EEPROM.begin(512);
readSsid();
readPass();
if (ssid.length() > 1)
connectToWifi();
else
{
error();
manual_hotspot();
createWebPage();
}
}
void loop()
{
if (WiFi.status() == WL_CONNECTED)
{
//Serial.println("loop/wificonnected");
if (Serial.available())
{
toggling();
}
else if (Firebase.getInt("change") == 1)
{
Serial.print(1);
delay(10);
if (Serial.available())
toggling();
Firebase.remove("change");
}
}
else if (!(WiFi.status() == WL_CONNECTED) && hotspot == false)
{
//Serial.println("loop/notconnected&&hotspot==false");
connectToWifi();
if (!(WiFi.status() == WL_CONNECTED))
{
error();
manual_hotspot();
createWebPage();
}
}
else if (hotspot == true)
{
//Serial.println("loop/hotspot==trure");
delay(50);
server.begin();
while (1)
{
//Serial.println("loop/hotspot==true/hotclient==true");
server.handleClient();
}
connectToWifi();
}
}
void readSsid()
{
//Serial.println("reading ssid from memory");
for (int i = 0; i < 32; i++)
ssid += char(EEPROM.read(i));
//Serial.println(">>>>>>");
//Serial.print(ssid);
//Serial.print("<<<<<<");
//Serial.println();
}
void readPass()
{
//Serial.println("reading password from memory");
for (int i = 32; i < 96; i++)
pass += char(EEPROM.read(i));
//Serial.println(">>>>>>");
//Serial.print(pass);
//Serial.print("<<<<<<");
//Serial.println();
}
void writeSsid()
{
//Serial.println("writting ssid to memory");
//Serial.println();
for ( int i = 0; i < ssid.length(); i++)
{
EEPROM.write(i, ssid[i]);
//Serial.print(ssid[i]);
}
EEPROM.commit();
}
void writePass()
{
//Serial.println("writting password to memory");
//Serial.println();
for ( int i = 32; (i - 32) < pass.length(); i++)
{
EEPROM.write(i, pass[i - 32]);
//Serial.print(pass[i - 32]);
}
EEPROM.commit();
}
void connectToWifi()
{
//Serial.println("in connect to wifi block");
WiFi.mode(WIFI_STA);
hotspot = false;
//Serial.println(">>>>>>>>");
//Serial.print(ssid.c_str());
//Serial.println(">>>>>>>>");
//Serial.println(">>>>>>>>");
//Serial.print(pass.c_str());
//Serial.println(">>>>>>>>");
const char *s = ssid.c_str(), *p = pass.c_str();
WiFi.begin(s, p);
delay(5000);
if (!(WiFi.status() == WL_CONNECTED))
delay(3000);
if (WiFi.status() == WL_CONNECTED)
{
//Serial.print("trying to connect to fireabse");
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
firebase = true;
}
}
void error()
{
//Serial.println("in error block");
st = "<ol>";
if (!(WiFi.status() == WL_CONNECTED))
{
WiFi.mode(WIFI_STA);
WiFi.disconnect();
n = WiFi.scanNetworks();
if (n == 0)
st += "no network found";
else
{
for (int i = 0; i < n; i++)
{
st += "<li>";
st += WiFi.SSID(i);
st += " (";
st += WiFi.RSSI(i);
st += ")";
st += (WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*";
st += "</li>";
}
}
}
st += "</ol>";
}
void manual_hotspot()
{
//Serial.println("in manual hotspot block");
WiFi.softAP(dssid, dpass);
Serial.println(WiFi.softAPIP());
hotspot = true;
}
void createWebPage()
{
//Serial.println("creating web page");
server.on("/", []() {
IPAddress ip = WiFi.softAPIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
content = "<!DOCTYPE HTML>\r\n<html>Hello from ESP8266 at ";
content += ipStr;
content += "<p>";
content += st;
content += "</p><form method='get' action='setting'><label>SSID: </label><input name='ssid' length=32><input name='pass' length=64><input type='submit'></form>";
content += "</html>";
server.send(200, "text/html", content);
});
server.on("/setting", []() {
ssid = server.arg("ssid");
pass = server.arg("pass");
//Serial.println("Ssid read from webpage is");
//Serial.print(ssid);
//Serial.println("Password read from web is");
//Serial.print(pass);
if (ssid.length() > 0 && pass.length() > 0)
{
for (int i = 0; i < 96; i++) EEPROM.write(i, 0);
writeSsid();
writePass();
content = "{\"Success\":\"saved to eeprom... \"}";
statusCode = 200;
hotclient = false;
delay(1000);
ESP.restart();
}
else
{
content = "{\"Error\":\"404 not found\"}";
statusCode = 404;
hotclient = true;
}
server.send(statusCode, "application/json", content);
});
}
void toggling()
{
//Serial.println("toggling block");
int val = Serial.read() - '0';
if (val == 1)
{
Firebase.setString("switch", "true");
if (Firebase.failed())
{
Firebase.setString("switch", "true");
if (Firebase.failed())
{
if (!(WiFi.status() == WL_CONNECTED))
{
error();
manual_hotspot();
createWebPage();
return;
}
}
}
}
else if (val == 0)
{
Firebase.setString("switch", "false");
if (Firebase.failed())
{
Firebase.setString("switch", "true");
if (Firebase.failed())
{
if (!(WiFi.status() == WL_CONNECTED))
{
error();
manual_hotspot();
createWebPage();
return;
}
}
}
}
}