Skip to content

Commit aea3074

Browse files
committed
Affichage sur le webserveur de la dernière IP attribuée en tant que client
1 parent a99b103 commit aea3074

File tree

6 files changed

+49
-2
lines changed

6 files changed

+49
-2
lines changed

lightkit/software/data/wifi_settings.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ <h2>Wifi</h2>
114114
Si le module ne parvient pas à se connecter au réseau existant avec les paramètres fournis, il basculera en mode Point d'Accès après le délais indiqué ci-dessus.
115115
</p>
116116

117+
<label for="wifiClientLastIP">Dernière IP :</label><br>
118+
<input type="text" id="wifiClientLastIP" name="wifiClientLastIP" autocomplete="off" placeholder="Aucune IP enregistrée" readonly><br>
119+
<p class="classic_form_help">
120+
IP attribuée au module lors de sa dernière connexion en tant que client.
121+
</p>
122+
117123
</div>
118124
</div>
119125

@@ -169,6 +175,7 @@ <h2>Wifi</h2>
169175
document.getElementById('wifiClientSSID').value = json["client"]["ssid"];
170176
document.getElementById('wifiClientPassword').value = json["client"]["password"];
171177
document.getElementById('wifiClientDelayBeforeAPFallbackMs').value = json["client"]["delayBeforeAPFallbackMs"] / 1000;
178+
document.getElementById('wifiClientLastIP').value = json["client"]["lastIp"];
172179
}
173180

174181
function read_wifi_form(json)
@@ -197,6 +204,7 @@ <h2>Wifi</h2>
197204
json["client"]["ssid"] = document.getElementById('wifiClientSSID').value;
198205
json["client"]["password"] = document.getElementById('wifiClientPassword').value;
199206
json["client"]["delayBeforeAPFallbackMs"] = document.getElementById('wifiClientDelayBeforeAPFallbackMs').value * 1000;
207+
// wifiClientLastIP is readonly
200208

201209
var params = 'v=' + JSON.stringify(json);
202210

lightkit/software/platformio.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
;
88
; Please visit documentation for the other options and examples
99
; https://docs.platformio.org/page/projectconf.html
10+
;
11+
; Note:
12+
; export envid=board_ring && pio run -e $envid -t upload && pio run -e $envid -t uploadfs && pio device monitor -e $envid
1013

1114
[env]
1215
monitor_speed = 115200

lightkit/software/src/flash/flash.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#define EEPROM_USED_SIZE 256
1717

1818
/** Increment this each time flash_settings_t is incompatible with previous version */
19-
#define FLASH_STRUCT_VERSION 3
19+
#define FLASH_STRUCT_VERSION 4
2020

2121
typedef struct {
2222
wifi_handle_t wifiHandle;

lightkit/software/src/web/web_server.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ static void handle_get_wifi_settings(void)
386386
json["client"]["ssid"] = wifiHandle->client.ssid;
387387
json["client"]["password"] = wifiHandle->client.password;
388388
json["client"]["delayBeforeAPFallbackMs"] = wifiHandle->client.delayBeforeAPFallbackMs;
389+
IPAddress lastIp = IPAddress(wifiHandle->client.lastIp);
390+
json["client"]["lastIp"] = lastIp.toString();
389391

390392
serializeJson(json, jsonString);
391393
server.send(200, "text/plain", jsonString);

lightkit/software/src/wifi/wifi.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ wifi_handle_t defaultWifiSettings = {
4747
.client = {
4848
{ .ssid = WIFI_DEFAULT_CLIENT_SSID },
4949
{ .password = WIFI_DEFAULT_CLIENT_PASSWORD },
50-
.delayBeforeAPFallbackMs = WIFI_DEFAULT_DELAY_AP_FALLBACK
50+
.delayBeforeAPFallbackMs = WIFI_DEFAULT_DELAY_AP_FALLBACK,
51+
.lastIp = IP_TO_U32(0, 0, 0, 0)
5152
}
5253
};
5354
// clang-format on
@@ -246,6 +247,36 @@ static int32_t wifi_start_scan(void)
246247
return 0;
247248
}
248249

250+
/**
251+
* @brief Check the current IP address of Client mode
252+
* and save it to flash if this is a new one
253+
* @return 0: OK, -1: error
254+
*/
255+
static int32_t wifi_save_current_ip(void)
256+
{
257+
uint32_t curIp;
258+
IPAddress lastIp = IPAddress(wifiHandle->client.lastIp);
259+
260+
// Do nothing if not client
261+
if (wifiHandle->mode != MODE_CLIENT) {
262+
return -1;
263+
}
264+
265+
// Get current ip adress and compare
266+
curIp = (uint32_t) WiFi.localIP();
267+
if (curIp == wifiHandle->client.lastIp) {
268+
return 0;
269+
}
270+
271+
log_info("Saving new IP Address: %s (Previous was %s)",
272+
WiFi.localIP().toString().c_str(),
273+
lastIp.toString().c_str());
274+
275+
// Save it in flash
276+
wifiHandle->client.lastIp = curIp;
277+
return flash_write();
278+
}
279+
249280
// =====================
250281
// PUBLIC FUNCTIONS
251282
// =====================
@@ -402,9 +433,11 @@ void wifi_main(void)
402433
wifi_fallback_as_ap();
403434
}
404435
} else {
436+
// Did we just get connected ?
405437
if (_isunset(STATUS_WIFI, STATUS_WIFI_IS_CO)) {
406438
_set(STATUS_WIFI, STATUS_WIFI_IS_CO);
407439
wifi_print();
440+
wifi_save_current_ip();
408441
}
409442
}
410443
}

lightkit/software/src/wifi/wifi.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ typedef struct {
7878
char password[WIFI_PASSWORD_MAX_LEN];
7979

8080
uint32_t delayBeforeAPFallbackMs; // Number of ms when cannot connect as client after boot before rebooting in AP mode
81+
uint32_t lastIp; // Last valid IP address used last time we got a successful connection as client
8182
} client;
8283
} wifi_handle_t;
8384

0 commit comments

Comments
 (0)