Added ability to specify static IP for WiFi STA connection

This commit is contained in:
Mark Qvist 2025-11-17 18:47:18 +01:00
parent 90539caee9
commit c467b82014
4 changed files with 49 additions and 0 deletions

View file

@ -69,6 +69,8 @@
#define CMD_WIFI_SSID 0x6B
#define CMD_WIFI_PSK 0x6C
#define CMD_WIFI_CHN 0x6E
#define CMD_WIFI_IP 0x84
#define CMD_WIFI_NM 0x85
#define CMD_BOARD 0x47
#define CMD_PLATFORM 0x48

View file

@ -1190,6 +1190,34 @@ void serial_callback(uint8_t sbyte) {
}
}
#endif
} else if (command == CMD_WIFI_IP) {
#if HAS_WIFI
if (sbyte == FESC) { ESCAPE = true; }
else {
if (ESCAPE) {
if (sbyte == TFEND) sbyte = FEND;
if (sbyte == TFESC) sbyte = FESC;
ESCAPE = false;
}
if (frame_len < CMD_L) cmdbuf[frame_len++] = sbyte;
}
if (frame_len == 4) { for (uint8_t i = 0; i<4; i++) { eeprom_update(config_addr(ADDR_CONF_IP+i), cmdbuf[i]); } }
#endif
} else if (command == CMD_WIFI_NM) {
#if HAS_WIFI
if (sbyte == FESC) { ESCAPE = true; }
else {
if (ESCAPE) {
if (sbyte == TFEND) sbyte = FEND;
if (sbyte == TFESC) sbyte = FESC;
ESCAPE = false;
}
if (frame_len < CMD_L) cmdbuf[frame_len++] = sbyte;
}
if (frame_len == 4) { for (uint8_t i = 0; i<4; i++) { eeprom_update(config_addr(ADDR_CONF_NM+i), cmdbuf[i]); } }
#endif
} else if (command == CMD_BT_CTRL) {
#if HAS_BLUETOOTH || HAS_BLE
if (sbyte == 0x00) {

2
ROM.h
View file

@ -56,6 +56,8 @@
#define CONFIG_SIZE 256
#define ADDR_CONF_SSID 0x00
#define ADDR_CONF_PSK 0x21
#define ADDR_CONF_IP 0x42
#define ADDR_CONF_NM 0x46
//////////////////////////////////
#endif

View file

@ -74,6 +74,23 @@ void wifi_remote_start_ap() {
void wifi_remote_start_sta() {
WiFi.mode(WIFI_STA);
uint8_t ip[4]; bool ip_ok = true;
for (uint8_t i = 0; i < 4; i++) { ip[i] = EEPROM.read(config_addr(ADDR_CONF_IP+i)); }
if (ip[0]==0x00 && ip[1]==0x00 && ip[2]==0x00 && ip[3]==0x00) { ip_ok = false; }
if (ip[0]==0xFF && ip[1]==0xFF && ip[2]==0xFF && ip[3]==0xFF) { ip_ok = false; }
uint8_t nm[4]; bool nm_ok = true;
for (uint8_t i = 0; i < 4; i++) { nm[i] = EEPROM.read(config_addr(ADDR_CONF_NM+i)); }
if (nm[0]==0x00 && nm[1]==0x00 && nm[2]==0x00 && nm[3]==0x00) { nm_ok = false; }
if (nm[0]==0xFF && nm[1]==0xFF && nm[2]==0xFF && nm[3]==0xFF) { nm_ok = false; }
if (ip_ok && nm_ok) {
IPAddress sta_ip(ip[0], ip[1], ip[2], ip[3]);
IPAddress sta_nm(nm[0], nm[1], nm[2], nm[3]);
WiFi.config(sta_ip, sta_ip, sta_nm);
}
delay(100);
if (wr_ssid[0] != 0x00) {
if (wr_psk[0] != 0x00) { WiFi.begin(wr_ssid, wr_psk); }