Skip to content

Commit df58781

Browse files
authored
Merge pull request #7 from davirxavier/bugfix/wifi
Bugfix/wifi
2 parents de5d5eb + 32bc173 commit df58781

File tree

4 files changed

+163
-43
lines changed

4 files changed

+163
-43
lines changed

include/esp-config-defines.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
#include "LittleFS.h"
1212

13-
#define ENABLE_LOGGING
14-
#ifdef ENABLE_LOGGING
13+
#ifdef ESP_CONFIG_PAGE_ENABLE_LOGGING
1514
#define LOGH() Serial.print("[ESP-CONFIG-PAGE] ")
1615
#define LOG(str) LOGH(); ESP_CONFIG_PAGE::serial->print(str)
1716
#define LOGN(str) LOGH(); ESP_CONFIG_PAGE::serial->println(str)

include/esp-config-page-ota.h

Lines changed: 123 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,26 @@
55
#ifndef ESP_CONFIG_PAGE_OTA_H
66
#define ESP_CONFIG_PAGE_OTA_H
77

8+
#ifdef ESP32_CONFIG_PAGE_USE_ESP_IDF_OTA
9+
#include <esp_ota_ops.h>
10+
#include <esp_partition.h>
11+
#warning "Using ESP-IDF OTA API instead of Arduino's"
12+
#endif
13+
814
namespace ESP_CONFIG_PAGE
915
{
16+
#ifdef ESP32_CONFIG_PAGE_USE_ESP_IDF_OTA
17+
inline size_t writeOffset = 0;
18+
inline const esp_partition_t *otaPartition = nullptr;
19+
inline esp_ota_handle_t otaHandle = 0;
20+
#endif
1021

22+
#ifndef ESP32_CONFIG_PAGE_USE_ESP_IDF_OTA
1123
#ifdef ESP32
1224
#define GET_UPDATE_ERROR_STR Update.errorString()
1325
#elif ESP8266
1426
#define GET_UPDATE_ERROR_STR Update.getErrorString().c_str()
27+
#endif
1528
#endif
1629

1730
inline void ota(bool isFilesystem)
@@ -45,6 +58,43 @@ namespace ESP_CONFIG_PAGE
4558
WiFiUDP::stopAll();
4659
#endif
4760

61+
#ifdef ESP32_CONFIG_PAGE_USE_ESP_IDF_OTA
62+
if (isFilesystem)
63+
{
64+
writeOffset = 0;
65+
66+
otaPartition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "spiffs");
67+
if (otaPartition == NULL) {
68+
LOGN("Error when starting update.");
69+
server->send(500, "text/plain", "Error starting update, filesystem partition is null");
70+
return;
71+
}
72+
73+
esp_err_t err = esp_partition_erase_range(otaPartition, 0, otaPartition->size);
74+
if (err != ESP_OK) {
75+
LOGN("Error when starting update.");
76+
server->send(500, "text/plain", "Error when erasing filesystem partition");
77+
return;
78+
}
79+
}
80+
else
81+
{
82+
otaPartition = esp_ota_get_next_update_partition(NULL);
83+
if (otaPartition == NULL)
84+
{
85+
LOGN("Error when starting update.");
86+
server->send(500, "text/plain", "Start update error, next update partition is null");
87+
return;
88+
}
89+
90+
esp_err_t err = esp_ota_begin(otaPartition, OTA_SIZE_UNKNOWN, &otaHandle);
91+
if (err != ESP_OK) {
92+
LOGN("Error when starting update.");
93+
server->send(500, "text/plain", "Error when setting up firmware ota update");
94+
return;
95+
}
96+
}
97+
#else
4898
#ifdef ESP32
4999
uint32_t maxSpace = UPDATE_SIZE_UNKNOWN;
50100
#elif ESP8266
@@ -64,19 +114,88 @@ namespace ESP_CONFIG_PAGE
64114
LOGN("Error when starting update.");
65115
server->send(400, "text/plain", GET_UPDATE_ERROR_STR);
66116
}
117+
#endif
67118
}
68119
else if (upload.status == UPLOAD_FILE_WRITE)
69120
{
70121
LOGN("Update write.");
122+
123+
#ifdef ESP32_CONFIG_PAGE_USE_ESP_IDF_OTA
124+
if (isFilesystem)
125+
{
126+
if (!otaPartition)
127+
{
128+
LOGN("Error when writing to update.");
129+
server->send(500, "text/plain", "Error writing to partition, is null");
130+
return;
131+
}
132+
133+
esp_err_t err = esp_partition_write(otaPartition, writeOffset, upload.buf, upload.currentSize);
134+
if (err != ESP_OK) {
135+
LOGN("Error when writing to update.");
136+
server->send(500, "text/plain", String(err));
137+
return;
138+
}
139+
140+
writeOffset += upload.currentSize;
141+
}
142+
else
143+
{
144+
if (otaHandle == 0)
145+
{
146+
LOGN("Error when writing to update.");
147+
server->send(500, "text/plain", "Ota handle is null");
148+
return;
149+
}
150+
151+
esp_ota_write(otaHandle, (const void*) upload.buf, upload.currentSize);
152+
}
153+
#else
71154
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize)
72155
{
73156
LOGN("Error when writing update.");
74157
server->send(400, "text/plain", GET_UPDATE_ERROR_STR);
75158
}
159+
#endif
76160
}
77161
else if (upload.status == UPLOAD_FILE_END)
78162
{
79163
LOGN("Update ended.");
164+
165+
#ifdef ESP32_CONFIG_PAGE_USE_ESP_IDF_OTA
166+
if (isFilesystem)
167+
{
168+
server->send(200, "text/plain", "");
169+
esp_restart();
170+
}
171+
else {
172+
if (otaHandle == 0)
173+
{
174+
LOGN("Error when writing to update.");
175+
server->send(500, "text/plain", "Ota handle is null");
176+
esp_restart();
177+
return;
178+
}
179+
180+
esp_err_t err = esp_ota_end(otaHandle);
181+
if (err != ESP_OK) {
182+
LOGF("Ota update unsuccessful, err: %x\n", err);
183+
server->send(500, "text/plain", String("Ota update unsuccessful, err: ") + err);
184+
esp_restart();
185+
return;
186+
}
187+
188+
err = esp_ota_set_boot_partition(otaPartition);
189+
if (err != ESP_OK) {
190+
LOGF("Error setting ota partition as new boot partition: %x\n", err);
191+
server->send(500, "text/plain", "Error setting ota partition as new boot partition.");
192+
esp_restart();
193+
return;
194+
}
195+
196+
LOGN("Update successful");
197+
}
198+
#else
80199
if (Update.end(true))
81200
{
82201
server->send(200);
@@ -86,14 +205,14 @@ namespace ESP_CONFIG_PAGE
86205
LOGN("Error finishing update.");
87206
server->send(400, "text/plain", GET_UPDATE_ERROR_STR);
88207
}
208+
#endif
89209
}
90-
yield();
210+
delay(10);
91211
}
92212

93213
inline void otaEnd()
94214
{
95-
server->sendHeader("Connection", "close");
96-
server->send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
215+
server->send(200, "text/plain", "");
97216
LOGN("OTA update finished, restarting.");
98217
delay(100);
99218
ESP.restart();
@@ -114,7 +233,7 @@ namespace ESP_CONFIG_PAGE
114233
server->on(F("/config/update/filesystem"), HTTP_POST, []()
115234
{
116235
VALIDATE_AUTH();
117-
otaEnd();
236+
// otaEnd();
118237
}, []()
119238
{
120239
VALIDATE_AUTH();

include/esp-config-page-wireless.h

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,24 @@ namespace ESP_CONFIG_PAGE
1212
IPAddress apIp = IPAddress(192, 168, 1, 1);
1313
String dnsName = "esp-config.local";
1414
int lastConnectionError = -1;
15-
bool apStarted = false;
16-
bool connected = false;
15+
unsigned long connectionTimeoutCounter;
16+
unsigned long connectionTimeoutMs = 45000;
17+
18+
/**
19+
* Sets the wifi connection timeout in milliseconds. Default is 45 seconds.
20+
*/
21+
inline void setConnectionTimeout(unsigned long connectionTimeoutMs)
22+
{
23+
ESP_CONFIG_PAGE::connectionTimeoutMs = connectionTimeoutMs;
24+
}
1725

1826
/**
1927
* Returns true if the wifi connection is ready or false if otherwise.
2028
* @return boolean
2129
*/
2230
inline bool isWiFiReady()
2331
{
24-
return WiFi.status() == WL_CONNECTED && !apStarted;
32+
return WiFi.status() == WL_CONNECTED;
2533
}
2634

2735
/**
@@ -40,18 +48,13 @@ namespace ESP_CONFIG_PAGE
4048
return;
4149
}
4250

43-
#ifdef ESP32
44-
WiFi.mode(WIFI_MODE_APSTA);
45-
#elif ESP8266
46-
WiFi.mode(WIFI_AP_STA);
47-
#endif
48-
51+
WiFi.mode(WIFI_STA);
4952
WiFi.setAutoReconnect(true);
5053
WiFi.persistent(true);
5154

5255
if (force)
5356
{
54-
WiFi.disconnect(false, true);
57+
WiFi.disconnect(false, false);
5558
WiFi.begin(wifiSsid, wifiPass);
5659
}
5760
else
@@ -63,7 +66,12 @@ namespace ESP_CONFIG_PAGE
6366
int result = WiFi.waitForConnectResult(timeoutMs);
6467
LOGF("Connection result: %d\n", result);
6568

66-
if (result != WL_CONNECTED)
69+
if (result == WL_CONNECTED)
70+
{
71+
LOGF("Connected to AP successfully, IP address: %s\n", WiFi.localIP().toString().c_str());
72+
lastConnectionError = -1;
73+
}
74+
else
6775
{
6876
LOGN("Connection error.");
6977
lastConnectionError = result;
@@ -82,7 +90,7 @@ namespace ESP_CONFIG_PAGE
8290
tryConnectWifi(force, 10000);
8391
}
8492

85-
inline void setWiFiCredentials(String& ssid, String& pass)
93+
inline void setWiFiCredentials(const String& ssid, const String& pass)
8694
{
8795
wifiSsid = ssid;
8896
wifiPass = pass;
@@ -214,41 +222,35 @@ namespace ESP_CONFIG_PAGE
214222

215223
addServerHandler((char*) F("/config/wifi"), HTTP_GET, wifiGet);
216224
addServerHandler((char*) F("/config/wifi"), HTTP_POST, wifiSet);
225+
connectionTimeoutCounter = millis() - connectionTimeoutMs;
226+
WiFi.setAutoReconnect(true);
217227
}
218228

219229
inline void wirelessLoop()
220230
{
221231
int status = WiFi.status();
232+
int mode = WiFi.getMode();
222233

223-
if (!apStarted && lastConnectionError != -1)
234+
if (mode == WIFI_STA)
224235
{
225-
LOGF("Connection error %d, starting AP.\n", lastConnectionError);
226-
227-
#ifdef ESP32
228-
WiFi.mode(WIFI_MODE_APSTA);
229-
#elif ESP8266
230-
WiFi.mode(WIFI_AP_STA);
231-
#endif
236+
if (status == WL_CONNECTED && millis() - connectionTimeoutCounter > 1000)
237+
{
238+
connectionTimeoutCounter = millis();
239+
}
232240

233-
WiFi.softAP(apSsid, apPass);
234-
LOGF("Server IP is %s.\n", apIp.toString().c_str());
235-
apStarted = true;
236-
connected = false;
241+
if (status != WL_CONNECTED && millis() - connectionTimeoutCounter > connectionTimeoutMs)
242+
{
243+
LOGN("Connection timeout reached, starting ap.");
244+
WiFi.mode(WIFI_AP_STA);
245+
WiFi.softAP(apSsid, apPass);
246+
LOGF("Ap started, server IP is %s.\n", apIp.toString().c_str());
247+
}
237248
}
238-
239-
if (!connected && status == WL_CONNECTED)
249+
else if (mode == WIFI_AP_STA && status == WL_CONNECTED)
240250
{
241-
LOGF("Connected successfully to wireless network, IP: %s.\n", WiFi.localIP().toString().c_str());
242-
lastConnectionError = -1;
243-
apStarted = false;
244-
connected = true;
245-
246-
LOGN("Disabling AP.");
247-
#ifdef ESP32
248-
WiFi.mode(WIFI_MODE_STA);
249-
#elif ESP8266
251+
LOGF("Connected to network successfully, ip address: %s\n", WiFi.localIP().toString().c_str());
252+
LOGN("Disabling AP");
250253
WiFi.mode(WIFI_STA);
251-
#endif
252254
}
253255
}
254256
}

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "esp-config-page",
3-
"version": "1.4.3",
3+
"version": "1.4.4",
44
"keywords": "wifi,wi-fi,esp,esp8266,esp32,espressif8266,espressif32,nodemcu,wemos,arduino",
55
"description": "Dynamic and modular configuration webpage for ESP8266 and ESP32 boards using the arduino framework. Features automatic wi-fi setup, OTA updates, web triggered actions, environment variables configuration, logging monitoring and more.",
66
"frameworks": "arduino",

0 commit comments

Comments
 (0)