Wireless config using static IP


I have been using this code for a while now, but since I’m re-writing large portions, I thought I would run this important snippet by everyone. I’m initializing my wireless using static IP addressing:

IPAddress myAddress(192,168,1,2);
IPAddress gateway(192,168,1,1);    
IPAddress netmask(255,255,255,0);
IPAddress dns(8,8,8,8);
WiFi.setStaticIP(myAddress, netmask, gateway, dns);
WiFi.setCredentials("SSID", "password", WPA2, WLAN_CIPHER_AES);

I think everything is correct, but I’m not 100% sure I need the initial WiFi.off() before setting the IP address.

Does my code need any tweaks to make it more bulletproof? In case it matters, I’m running non-threaded in SEMI-AUTOMATIC mode.


WiFi.off()/on() are for turning off/on the WiFi module i.e. power saving. So there should be any need to fudz with that.

Notice that your photon remembers the 5 last set credentials and will attempt to make a connection to any of these stored in memory. If you want to make sure it goes to this one specific address only, do a clearCredentials() at some point. This should whipe out all the credentials previously stored (if any) and the call to setCredentials() later on is then guaranteed to connect to the network you specified with that call.

hope that makes sense to you.


@joost, thanks for the reply.

I simplified my actual code with the simple WiFi.setCredentials shown above. In reality I read through all of the configured AP’s, and if I don’t find the one I’m looking for I add it.


As for WiFI.off() I’m inclined to say you should not turn the WiFi module off when you want to write any settings to it.

For the DNS, you could try instead - it’s supposed to be faster than Google and should not keep any records (for more than a few hours) of your DNS requests.

Also since these settings are sticky, you may want to wrap them in a conditional block to only set them when you need to and not unconditionally (e.g. store some flag to EEPROM to check).


@ScruffR Oh are these settings stored in the actual module itself and not persisted in eeprom/flash somewhere? Just being interested, I haven’t taken a nose dive into the particle OS that deep


@ScruffR, funny thing is that my code works just fine, even after turning off the radio, setting the static IP, then turning it back on. However, I removed both and nothing changed so I’m good.

I wasn’t aware of the DNS server. Thanks for the heads up.

When you say the settings are “sticky”, do you mean the static IP config? Right now it’s only set once in setup(). Could that cause a problem long term?


Yup, once set these settings stay there till you change them - irrespective of power cycles.
It shouldn’t pose a problem - unless you rapidly reset the device to run through setup() again - on a Photon but the Core did have more problems with that.

@joost, some settings are stored in the WiFi module (e.g. WiFi credentials) others are stored in STM32 flash, but to be on the save side and not having to bother which is which, it’s easiest to have the WiFi module on. Having it on shouldn’t prevent any of these commands from working, but having it off might prevent some.


Ok. But it would be better to do static up with the—
particle setup
Command. Can this be done?