Static IP is not working

Hello, I’ve got a problem im not able to solve:
I want to connect my Photon to an Ad-Hoc WiFi network hosted by a raspberry pi 3b.
I collected some code snippets here and there on this forum, but I don’t get this to work.

My code which is for the WiFi is this:

Serial.println("[WIFI] Clearing credentials");
WiFi.clearCredentials();

Serial.println("[WIFI] Turning off");
WiFi.off();
	
Serial.println("[WIFI] Setting credentials");
WiFi.setCredentials(SSID, PASSWORD);

Serial.println("[WIFI] Setting static");
IPAddress myAddress(10, 2, 1, 2);
IPAddress netmask(255, 255, 255, 0);
IPAddress gateway(10, 2, 1, 1);
IPAddress dns(10, 2, 1, 1);
WiFi.setStaticIP(myAddress, netmask, gateway, dns);
WiFi.useStaticIP();

Serial.println("[WIFI] Trying to connect");
WiFi.on();
delay(50);
WiFi.connect();
waitUntil(WiFi.ready);
delay(1000);
Serial.print("\rConnected to ");
Serial.println(WiFi.SSID());

In the loop, im executing this code when a button is pressed:

request.hostname = "10.2.1.1";
request.port = 3000;
request.path = "/event?id=0";
http.get(request, response, headers);

I’m using the HttpClient library.

Thanks.

I’d suggest you have the WiFi module switched on when you want to program it (at least for WiFi.setCredentials() it needs to be on.
Is your WiFi network actually present when you set credentials?

1 Like

As @ScruffR said, this call to setCredentials() does not contain information to set them for a network that is not currently in view. You need to pass all four arguments like this section from the doc:

// for hidden and offline networks on the Photon, the security cipher is also needed
// Cipher options are WLAN_CIPHER_AES, WLAN_CIPHER_TKIP and WLAN_CIPHER_AES_TKIP
WiFi.setCredentials(ssid, password, auth, cipher);
WiFi.setCredentials("SSID", "PASSWORD", WPA2, WLAN_CIPHER_AES);

Yes, it’s visible. I can connect with my PC without entering the SSID (or anything; it even appears on my phone).

Serial.println("[WIFI] Clearing credentials");
WiFi.clearCredentials();

Serial.println("[WIFI] Turning off");
WiFi.off();
Serial.println("[WIFI] Turning on");
WiFi.on();
delay(50);

Serial.println("[WIFI] Setting credentials");
WiFi.setCredentials(SSID, PASSWORD, UNSEC);

Serial.println("[WIFI] Setting static");
IPAddress myAddress(10, 2, 1, 2);
IPAddress netmask(255, 255, 255, 0);
IPAddress gateway(10, 2, 1, 1);
IPAddress dns(10, 2, 1, 1);

WiFi.setStaticIP(myAddress, netmask, gateway, dns);
WiFi.useStaticIP();

Serial.println("[WIFI] Trying to connect");
WiFi.connect();
waitUntil(WiFi.ready);
delay(1000);
Serial.print("\rConnected to ");
Serial.println(WiFi.SSID());

Now my code looks like this (I put the WiFi.on() above the setCredentials)
It’s still stuck at the “blinking green”-state

According to the documentation, this is the syntax for an unsecured (open) network:

// Connects to an unsecured network.
WiFi.setCredentials(ssid);

You’re still passing a password, which isn’t used in an open network.

And like @ScruffR said, I would completely remove the WiFi.off(). Here is how I would write the first part of your code:

Serial.println("[WIFI] Clearing credentials");
WiFi.clearCredentials();
delay(50);

This is the exact code I use:

#if (PLATFORM_ID == PLATFORM_SPARK_CORE)
 #define SSID_COUNT             7
#elif ((PLATFORM_ID == PLATFORM_PHOTON_PRODUCTION) || (PLATFORM_ID == PLATFORM_P1))
 #define SSID_COUNT             5
#endif


WiFiAccessPoint ap[SSID_COUNT];
bool foundSSID = false;
int apFound;


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.useStaticIP(); 

apFound = WiFi.getCredentials(ap, SSID_COUNT);
if (apFound)
{
   for (int x = 0; x < apFound; x++) 
   {
      if (strncmp(ap[x].ssid, "MYSSID", 6) == 0)           
      {
          foundSSID = true;
          break;
      }
    }
}
    
if (!foundSSID)
    WiFi.setCredentials("MYSSID", "MYPASSWORD", WPA2, WLAN_CIPHER_AES);

WiFi.connect();
waitUntil(WiFi.ready);                      
Particle.connect();
waitUntil(Particle.connected);
1 Like

You may also want to rethink the approach to unconditionally clear credentials and reprogramm the whole lot over and over.
All these settings are sticky, so they will stay with your device even after you remove power. Hence I’d store a “flag” in EEPROM once you had set everything up and don’t do it again, until you explicitly need to do it again, which can be initiated by clearing the flag.

Also if things are not working as expected, you can always let your phantasy play and consider what actions could change the behaviour. Assuming that a feature isn’t working as intended should be the last assumption - it’s much more likely that the way how a working feature is applied prevents it from working.

Having said this, one thing to creatively try would be to “power-cycle” the WiFi modem after you set it up.

Maybe my title is a bit misleading :grin: I’m not trying to say that a feature is not working correctly because of the way it’s implemented by the “creators”, I’m trying to say it’s not working because I’m not able to implement it correctly. Just to clear that up.

Maybe it can help that it’s an unsecured network.

An I tried to be creative, switched around some code here and there… But I’m not getting it to work and im getting frustrated… :sweat:

@photonfan, have you checked that the IP will work with your Pi? Is the Pi acting as a DHCP server dishing out addresses? You need to be looking upstream and not just on the Photon.

If so, you should not provide a password as you indicate in your code

This is what the docs state
https://docs.particle.io/reference/firmware/photon/#setcredentials-

// Connects to an unsecured network.
WiFi.setCredentials(ssid);
WiFi.setCredentials("My_Router_Is_Big");

However, maybe try things step-by-step.
Go back to WiFi.useDynamicIP() and try to connect with your credentials.
Once you have established you can connect that way, then you can try out the static IP route.

1 Like

I can not connect by Dynamic, because the RP has no DHCP Server running.

Hello there,

I’m trying to connect my Particle Photon to my Ad-Hoc WiFi-Network hosted on a Raspberry Pi 3B without a running DHCP-Server and that’s why I have to connect with a static IP.

This is my second Forum-Thread, because I tried to fix it myself, but didn’t get it to work.

Till now I’ve got this code:

WiFi.on();

if(WiFi.hasCredentials())
    WiFi.clearCredentials();

WiFiCredentials credentials(SSID);
WiFi.setCredentials(credentials);

IPAddress rmtAddress(10, 2, 1, 15);
IPAddress rmtGateway(10, 2, 1, 1);
IPAddress rmtNetmask(255, 255, 255, 0);
IPAddress rmtDns(10, 2, 1, 1);
WiFi.setStaticIP(rmtAddress, rmtNetmask, rmtGateway, rmtDns);
WiFi.useStaticIP();

WiFi.setHostname("photon-trigger-" + ID);

WiFi.connect();

and I’m running the Photon in SEMI_AUTOMATIC Mode.

I appreciate any help.

I moved this post here, since it’s still the same issue and we don’t want to have orphaned/abandoned threads just because a new thread was started for no reason.

BTW, have you been through the threads that already dealt with similar issues.
You will only get the same answers that were given in those - especially when in these threads the “issue” was actually solved.
https://community.particle.io/search?q=static%20ip

One tip I gave in one of these threads was to try WiFi.off() after you stored the settings, wait for 100ms and start with WiFi.on() again.
You may also need to use WiFi.setCredentials() the way you were told further up this thread.

And finally I repeat myself

Yes, you stated that your RPi has no DHCP enabled, but how about back-tracking even further and forget about the RPi for the moment and try to get the rest working with a “proper” WiFi AP which has DHCP, you can still use static IP on these devices to get a hang of how to do it correctly.
Just remove as many variables from your equation as you can.

1 Like

I now did everything you guys told me to do (or I’m missing something out again…), but it’s still not working:

void setupNetwork() {
    WiFi.clearCredentials();
    delay(100);

    WiFi.setCredentials(SSID);
    delay(100);

    IPAddress rmtAddress(10, 2, 1, 15);
    IPAddress rmtGateway(10, 2, 1, 1);
    IPAddress rmtNetmask(255, 255, 255, 0);
    IPAddress rmtDns(10, 2, 1, 1);
    WiFi.setStaticIP(rmtAddress, rmtNetmask, rmtGateway, rmtDns);
    delay(50);
    WiFi.useStaticIP();
    delay(200);
}

void enableNetwork() {
    WiFi.on();
    delay(250);

    WiFi.connect();
    delay(250);
}

void setup() {
    setupNetwork();
    enableNetwork();
}

I’m not quite buying that.
In your most recent code I can’t see any trace of

nor

which was then repeated more direct

So I came up with some code that actually does all that - and it works as expected

SYSTEM_MODE(SEMI_AUTOMATIC)

const int eeBase = 0x00;
const int magicNumber = 0xcafecafe;

struct SETTINGS {
    int  magic;
} settings;

void setupNetwork() {
    EEPROM.get(eeBase, settings);
    WiFi.on();

    if (settings.magic == magicNumber 
    ||  WiFi.hasCredentials()) 
        return;
    
    IPAddress rmtAddress(10, 2,  1, 15);
    IPAddress rmtGateway(10, 2,  1,   1);
    IPAddress rmtNetmask(255, 255, 255,   0);
    IPAddress rmtDns(10, 2, 1, 1);
    WiFi.useStaticIP();
    WiFi.setStaticIP(rmtAddress, rmtNetmask, rmtGateway, rmtDns);
    WiFi.setCredentials("testSSID");
    
    WiFi.off();
    delay(100);

    settings.magic = magicNumber;
    EEPROM.put(eeBase, settings);
}

void enableNetwork() {
    WiFi.on();
    WiFi.connect();
}

void setup() {
    setupNetwork();
    enableNetwork();
}

void loop() {
    Serial.printlnf("SSID: %s, localIP: %s", (const char*)WiFi.SSID(), (const char*)WiFi.localIP().toString());
    delay(1000);
}

Since - as already said - I consider it bad practice to unconditionally clear credentials on every startup, I have left that part out and you just need to clear the credentials by holding SETUP 10+ seconds to clear them manually in order to actually perform the setup procedure.
And it’s also only done once, unless you change the magic number, to re-enable the setup function.

2 Likes