-
Notifications
You must be signed in to change notification settings - Fork 0
/
EasyWiFiHandler.ino
247 lines (211 loc) · 5.67 KB
/
EasyWiFiHandler.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
/*
This script was created to support a Wi-Fi module (e.g. ESP8266) in conjunction with other devices such as STM32 or Arduino.
It allows you to quickly and easily connect to a Wi-Fi network and send a GET request, the value of which will be returned to the microcontroller (e.g. STM32, Arduino)
Author: Domninik Krzywański
Website: wolfror.com
-------------------------
Ten skrypt został stworzony w celu obsługi modułu Wi-Fi (np. ESP8266) w połączeniu z innymi urządzeniami, takimi jak STM32 czy Arduino.
Umożliwia szybkie i łatwe nawiązywanie połączeń z siecią Wi-Fi oraz wysłanie zapytania GET, którego wartość zostanie zwrócona do mikrokontrolowe (np. STM32, Arduino)
Autor: Dominik Krzywański
Strona internetowa: wolfror.com
*/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
String getValue(String data, char separator, int index);
void setConf(String data);
boolean connect();
void getSettings();
String ssid = "";
String pass = "";
String hostname = "ESP";
boolean autoReconnect = false;
int numberTries = 10;
void setup() {
Serial.begin(115200);
digitalWrite(LED_BUILTIN, HIGH);
}
void loop() {
if(WiFi.status() == WL_CONNECTED)
{
digitalWrite(LED_BUILTIN, HIGH);
}
else
{
digitalWrite(LED_BUILTIN, LOW);
}
if (Serial.available()) {
String data = Serial.readStringUntil('\n');
data.trim();
if(WiFi.status() == WL_CONNECTED)
{
if (data.startsWith("http://") || data.startsWith("https://"))
{
HTTPClient http;
WiFiClient clientt;
http.begin(clientt,data);
int httpCode = http.GET();
if (httpCode > 0)
{
String response = http.getString();
Serial.println(response);
}
else
{
Serial.println("WE4: HTTP request error");
}
http.end();
}
else if(strcmp(data.c_str(), "ip") == 0)
{
Serial.println("WS4: IP: " + WiFi.localIP().toString());
}
else if(strcmp(data.c_str(), "close") == 0)
{
WiFi.disconnect();
Serial.println("WS3: Wi-Fi Disconnected");
}
else if(data.startsWith("get"))
{
getSettings();
}
else if(strcmp(data.c_str(), "ssid") == 0)
{
Serial.println("WS4: SSID: " + ssid);
}
else if(data.startsWith("state"))
{
Serial.print("WS1: ");
Serial.println(WiFi.status() == WL_CONNECTED ? "True" : "False");
}
else
{
Serial.println("WE3: Invalid command");
}
}
else if(data.startsWith("set"))
{
setConf(data);
}
else if(data.startsWith("get"))
{
getSettings();
}
else if(data.startsWith("state"))
{
Serial.print("WS1: ");
Serial.println(WiFi.status() == WL_CONNECTED ? "True" : "False");
}
else if(data.startsWith("conn"))
{
connect();
}
else
{
Serial.println("WE2: No connection to Wi-Fi");
}
}
}
void setConf(String data)
{
if(WiFi.status() == WL_CONNECTED)
{
Serial.println("WE5: Cannot change parameters. Wi-Fi connection is active");
return;
}
String tempSsid = getParameter(data, "-ssid");
String tempNt = getParameter(data, "-nt");
String tempPass = getParameter(data, "-pass");
String tempHostname = getParameter(data, "-hostname");
String tempAr = getParameter(data, "-ar");
if(strcmp(tempSsid.c_str(), "") != 0)
{
ssid = tempSsid;
}
if(strcmp(tempNt.c_str(), "") != 0)
{
numberTries = tempNt.toInt();
}
if(strcmp(tempPass.c_str(), "") != 0)
{
pass = tempPass;
}
if(strcmp(tempHostname.c_str(), "") != 0)
{
hostname = tempHostname;
}
if(strcmp(tempAr.c_str(), "") != 0)
{
autoReconnect = tempAr.toInt() == 1 ? true : false;
}
Serial.println("WS3: Parameters has been set");
}
boolean connect()
{
if(strcmp(ssid.c_str(), "") == 0 || strcmp(pass.c_str(), "") == 0)
{
Serial.println("WE6: Parameters to connect are missing");
}
WiFi.mode(WIFI_STA);
WiFi.setAutoReconnect(autoReconnect);
WiFi.hostname(hostname);
WiFi.begin(ssid, pass);
int tries = 0;
while (WiFi.status() != WL_CONNECTED) {
if(tries >= numberTries)
{
Serial.println("WE1: Failed to connect");
return false;
}
delay(500);
Serial.print(".");
tries++;
}
Serial.println("WS3: Connected to a Wi-Fi network");
getSettings();
return true;
}
void getSettings()
{
Serial.println("");
Serial.println("WS2:");
Serial.println("SSID: " + ssid);
Serial.print("Auto reconnect: ");
Serial.println(autoReconnect ? "True" : "False");
Serial.println("IP address: " + WiFi.localIP().toString());
Serial.println("Mac: " + WiFi.macAddress());
Serial.println("Hostname: " + hostname);
}
String getValue(String data, char separator, int index) {
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
String getParameter(String data, String startTag) {
int startIndex = data.indexOf(startTag);
if (startIndex != -1) {
startIndex += startTag.length() + 1;
int endIndex = data.indexOf(" -", startIndex);
if (endIndex == -1) {
endIndex = data.indexOf('\n', startIndex);
}
if (endIndex == -1) {
endIndex = data.indexOf('\r', startIndex);
}
if(endIndex == -1)
{
endIndex = data.length();
}
if (endIndex != -1) {
return data.substring(startIndex, endIndex);
}
}
return "";
}