Skip to content

Commit 07693a6

Browse files
committed
Ajout de la fonction mDNS permettant d'accéder au module par son nom plutôt que par son IP.
La configuration du nom est réalisée sur la même page web que le wifi.
1 parent 5d82c21 commit 07693a6

File tree

7 files changed

+102
-3
lines changed

7 files changed

+102
-3
lines changed

lightkit/software/data/wifi_settings.html

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,22 @@
1919
<h1 id="title">Lightkit</h1>
2020
<div id="content">
2121
<p id="status_msg" class="message_box">Erreur</p>
22-
<div id="part_select_file">
22+
<div id="part_config_wifi">
2323
<h2>Wifi</h2>
2424
<p>
2525
Configuration des paramètres pour la connexion wifi.
2626
</p>
2727
<form id="wifi_form" class="classic_form">
2828

29+
<div class="frame">
30+
<label for="wifiModuleName">Nom du module :</label><br>
31+
<input type="text" id="wifiModuleName" name="wifiModuleName" autocomplete="off" maxlength="15" placeholder="Ex: Lightkit"><br>
32+
<p class="classic_form_help">
33+
Permet d'accéder à ce site web en tapant le nom du module dans un navigateur.<br>
34+
(ex: "Lightkit.local")
35+
</p>
36+
</div>
37+
2938
<div class="frame">
3039
<label>Mode :</label><br>
3140
<input type="radio" id="wifiModeAP" name="wifiMode" value="ap" onchange="wifi_mode_changed()">
@@ -280,6 +289,15 @@ <h2>Wifi</h2>
280289
});
281290
}
282291

292+
function send_module_name()
293+
{
294+
var params = 'v=' + document.getElementById('wifiModuleName').value;
295+
296+
cgi_request('/set_module_name', params, function(textReply) {
297+
// Nothing here
298+
});
299+
}
300+
283301
/**
284302
* Start function to load the page
285303
*/
@@ -291,13 +309,19 @@ <h2>Wifi</h2>
291309

292310
document.getElementById('wifiFormSubmit').onclick = function(event) {
293311
event.preventDefault()
312+
send_module_name();
294313
read_wifi_form();
295314
}
296315
document.getElementById('wifiFormDefault').onclick = function(event) {
297316
event.preventDefault()
317+
send_module_name();
298318
use_default();
299319
}
300320

321+
cgi_request('/get_module_name', '', function(textReply) {
322+
document.getElementById('wifiModuleName').value = textReply;
323+
});
324+
301325
cgi_request('/get_wifi_settings', '', function(textReply) {
302326
var json = JSON.parse(textReply);
303327
write_wifi_form(json);
@@ -308,7 +332,6 @@ <h2>Wifi</h2>
308332
// Refresh scans only after getting settings
309333
refresh_scan_list();
310334
});
311-
312335
}
313336
</script>
314337
</body>

lightkit/software/src/cmd/cmd.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,35 @@ void cmd_reset_module(void)
4141
script_delayed_reset(1000);
4242
}
4343

44+
/**
45+
* @brief Set the module name used for mDNS
46+
*
47+
* @param newName The new name to use (len < MODULE_NAME_SIZE_MAX)
48+
* @return OK: 0, Too short: -1
49+
*/
50+
int cmd_set_module_name(String newName)
51+
{
52+
// Copy the new name
53+
strncpy(flashSettings.moduleName, newName.c_str(), MODULE_NAME_SIZE_MAX);
54+
55+
// Always terminate the string
56+
flashSettings.moduleName[MODULE_NAME_SIZE_MAX - 1] = '\0';
57+
58+
flash_write();
59+
60+
return 0;
61+
}
62+
63+
/**
64+
* @brief Get the current module name
65+
*
66+
* @return The module name as a String
67+
*/
68+
String cmd_get_module_name(void)
69+
{
70+
return String(flashSettings.moduleName);
71+
}
72+
4473
void cmd_print_status(void)
4574
{
4675
log_raw("APPLI: 0x%02X\n", STATUS_APPLI);

lightkit/software/src/cmd/cmd.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
void cmd_print_help(void);
1414
void cmd_reset_module(void);
15+
int cmd_set_module_name(String newName);
16+
String cmd_get_module_name(void);
1517
void cmd_print_status(void);
1618
void cmd_set_status_led(uint8_t isEnabled);
1719
void cmd_set_brightness_auto(bool newValue);

lightkit/software/src/flash/flash.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
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 2
19+
#define FLASH_STRUCT_VERSION 3
2020

2121
typedef struct {
2222
wifi_handle_t wifiHandle;
2323
stripled_params_t stripledParams;
24+
char moduleName[MODULE_NAME_SIZE_MAX];
2425

2526
// -- Used for flash purpose --
2627
uint8_t crc;

lightkit/software/src/global.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
#define I_U 1 /** Input pull-up */
4949
#define I_A 2 /** Input analog */
5050

51+
/** Maximum size of module name ('\0' included) */
52+
#define MODULE_NAME_SIZE_MAX 16
53+
5154
/**
5255
* This is the configuration for the board
5356
* TEMP_DOMOTICZ

lightkit/software/src/ota/ota.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,31 @@
88
#include <ArduinoOTA.h>
99

1010
#include "file_sys/file_sys.hpp"
11+
#include "flash/flash.hpp"
1112
#include "global.hpp"
1213
#include "ota.hpp"
1314

1415
extern uint32_t tick;
1516
uint32_t otaTick = 0;
1617

18+
static int ota_configure_mdns(void)
19+
{
20+
// Check if module name is set
21+
if (flashSettings.moduleName[0] == '\0') {
22+
return -1;
23+
}
24+
25+
log_info("Using \"%s\" as module name", flashSettings.moduleName);
26+
27+
// mDNS is enabled by default in both ESP32 and ESP8266 libraries
28+
ArduinoOTA.setHostname(flashSettings.moduleName);
29+
return 0;
30+
}
31+
1732
int ota_init(void)
1833
{
34+
ota_configure_mdns();
35+
1936
ArduinoOTA.setPort(OTA_PORT);
2037
ArduinoOTA.setPassword(OTA_PWD);
2138

lightkit/software/src/web/web_server.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,24 @@ static void handle_get_wifi_scans(void)
431431
server.send(200, "text/plain", jsonString);
432432
}
433433

434+
static void handle_get_module_name(void)
435+
{
436+
server.send(200, "text/plain", cmd_get_module_name());
437+
}
438+
439+
static void handle_set_module_name(void)
440+
{
441+
if (!server.hasArg("v")) {
442+
handle_bad_parameter();
443+
return;
444+
}
445+
446+
log_info("Using \"%s\" as new module name", server.arg("v"));
447+
448+
cmd_set_module_name(server.arg("v"));
449+
handle_get_module_name();
450+
}
451+
434452
int web_server_init(void)
435453
{
436454
server.begin();
@@ -491,6 +509,12 @@ int web_server_init(void)
491509
server.on("/get_wifi_scans", HTTP_GET, []() {
492510
handle_get_wifi_scans();
493511
});
512+
server.on("/set_module_name", HTTP_GET, []() {
513+
handle_set_module_name();
514+
});
515+
server.on("/get_module_name", HTTP_GET, []() {
516+
handle_get_module_name();
517+
});
494518

495519
// --- File management ---
496520
server.onNotFound([]() {

0 commit comments

Comments
 (0)