Hi, I’m back again 
The “belive” troubled me a little bit. So I started to roam through the git sources of https://github.com/spark/firmware, searching for an definite answer:
- Is WiFi.setStaticIP(…) writing each time the settings to the flash, or not?
I think I traced down the correct function and it seems to be, that it is writing the configuration EACH time I call WiFi.setStaticIP(…). As I’m not experienced in photon photon firmware programming, the next question is: Am I right, or do I missed something?
This will be a question to a more experienced person than me.
What I found is in the file firmware\hal\src\photon\wlan_hal.cpp (line 788-800):
// That will be needed by wlan_set_ipaddress(..)
void assign_if_set(dct_ip_address_v4_t& dct_address, const HAL_IPAddress* address)
{
if (address && is_ipv4(address))
{
dct_address = address->ipv4;
}
}
// That is called by WiFi.setStaticIP(..) and seems to be responsible for writing to the flash.
void wlan_set_ipaddress(const HAL_IPAddress* host, const HAL_IPAddress* netmask,
const HAL_IPAddress* gateway, const HAL_IPAddress* dns1,
const HAL_IPAddress* dns2, void* reserved)
{
const static_ip_config_t* pconfig = wlan_fetch_saved_ip_config();
static_ip_config_t config;
memcpy(&config, pconfig, sizeof(config));
assign_if_set(config.host, host);
assign_if_set(config.netmask, netmask);
assign_if_set(config.gateway, gateway);
assign_if_set(config.dns1, dns1);
assign_if_set(config.dns2, dns2);
dct_write_app_data(&config, DCT_IP_CONFIG_OFFSET, sizeof(config));
}
If I’m right, this is the function that gets finally called by WiFi.setStaticIP(…). Below you find the same function with my thoughts…
void wlan_set_ipaddress(const HAL_IPAddress* host, const HAL_IPAddress* netmask,
const HAL_IPAddress* gateway, const HAL_IPAddress* dns1,
const HAL_IPAddress* dns2, void* reserved)
{
// We read the current IP setting, that is very nice. It will set us in a position, where
// we can finally decide if we realy need to write the new configuratino to flash, or not.
const static_ip_config_t* pconfig = wlan_fetch_saved_ip_config();
// Create a new config object. Right now it's empty.
static_ip_config_t config;
// copy over the previously readed config settings.
memcpy(&config, pconfig, sizeof(config));
// assign_if_set(..) is defined in the same file, above this function.
// It will basically check if both parameters are vaild. If so, it
// will assign the HAL_IPAddress to the appropriate config object (as uint32_t).
assign_if_set(config.host, host);
assign_if_set(config.netmask, netmask);
assign_if_set(config.gateway, gateway);
assign_if_set(config.dns1, dns1);
assign_if_set(config.dns2, dns2);
// This function will directly write the config object to the flash.
dct_write_app_data(&config, DCT_IP_CONFIG_OFFSET, sizeof(config));
}
Okay, so far I see no situation where the config will not be written to the flash.
From my point of view, it’s a pity, because we already loaded the existing configuration from flash.
A quick comparison would not hurt in this case, or am I wrong? Maybe I miss something?
My approach would be something like this:
...
assign_if_set(config.dns2, dns2);
if ((config.host == pconfig->host) && (config.netmask == pconfig->netmask) && (config.gateway == pconfig->gateway) && (config.dns1 == pconfig->dns1) && (config.dns2 == pconfig->dns2))
return;
dct_write_app_data(&config, DCT_IP_CONFIG_OFFSET, sizeof(config));
...
Maybe someone more experienced in the community, can post some suggestions.
Best regards
Pix