Cannot get P2 to connect on WiFi using Static IP

Hi,

I recently got some P2's and trying to get my P1 code ported to P2. When I setup the Static IP Config and perform WiFi.Connect(), my P2 is not connecting on the desired static IP. It is connecting to WiFi at any IP address - I think like DHCP. My code is posted below:

void SetupWiFi()
{
    // logs
    // WiFi.off();
    WiFi.on();
    Particle.process();
    Log.info("Initializing WiFi. ");
    V_GATEWAY_STATIC_IP = 254; 
    IPAddress V_RouterIp({192,168,1,1});
    IPAddress V_GatewayStaticIp({V_RouterIp[0], V_RouterIp[1], V_RouterIp[2], V_GATEWAY_STATIC_IP});
    IPAddress V_SubnetMask({255, 255, 255, 0});
    IPAddress V_Dns({8, 8, 8, 8}); 
    
    Network.setConfig(NetworkInterfaceConfig()
    // .source(NetworkInterfaceConfigSource::STATIC)
    .address(V_GatewayStaticIp, V_SubnetMask)
    .gateway(V_RouterIp)
    .dns(V_Dns));
    Particle.process();

    WiFi.off();
    delay(100);
    
    Particle.process();

    // WiFi.off();
    WiFi.on();
    delay(100);
    Particle.process();
    Log.info("Using Static IP. ");  
    WiFi.setConfig(NetworkInterfaceConfig().source(NetworkInterfaceConfigSource::STATIC));
    
    
    WiFi.connect();
    Particle.process();
    F_WiFiConnecting = true;
    wd.checkin();
    
    V_WiFiConnectionAttempt++;
    Log.info("Attempting WiFi connection. Attempt: " + String(V_WiFiConnectionAttempt));
    
    V_CheckWiFiConnectivityDelaySec = System.uptime();
}

void Fn_WiFiConnecting()
{
    // Serial.print(".");
    Particle.process();
    if(WiFi.ready())
    {
        V_CheckWiFiConnectivityDelaySec = System.uptime();
        F_StaticIPAssigned = false;        
        F_WiFiConnecting = false;

        Serial.println();
        Particle.process();

        // finding the network wifi router ip address to which the gateway is trying to connect 
        IPAddress V_GATEWAY_IP_ADDR = WiFi.localIP();

        Log.info("Gateway IP Address: " + String(V_GATEWAY_IP_ADDR));
        Particle.process();

        if(V_GATEWAY_IP_ADDR[3] == V_GATEWAY_STATIC_IP)
        {
            Log.info("Static Ip Connected Successfully");
            V_WiFiConnectionAttempt = 0;

            F_StaticIPAssigned = true;
        }
        else
        {
            Log.info("Static Ip NOT Connected Successfully");
        }
    }
}

Any help to get my P2's to connected successfully over Static IP. Thanks.

Is there a reason you commented out the .source()? I think it's required. It's required, if a source is not specified all of the configuration options are ignored.

Also you should use WiFi.setConfig() not Network.setConfig().

There's a full example in the pull request that added static IP address support that may be helpful.

Also make sure your device has Device OS 5.3.0 on it already, including all system dependencies, because if you're missing something like a bootloader, the device won't run your code until the dependencies are resolved, and will attempt to go online using the current network configuration, which is presumably DHCP, to get the dependencies OTA.

@rickkas7 Thanks a charm!

I implemented the example code from the link pull request and it worked.

Then I came back to my code and replaced the config part with code below:

auto conf = NetworkInterfaceConfig()
                .source(NetworkInterfaceConfigSource::STATIC, AF_INET)
                .address(V_GatewayStaticIp, V_SubnetMask)
                .gateway(V_RouterIp)
                .dns(V_RouterIp)
                .dns(V_Dns);
    WiFi.setConfig(conf);

and its working now. Will test the code on a fresh P2 and verify that the above changes in my code are the reason its working and not because I ran the example code (as the network configurations are stored in flash they are getting retrieved regardless of what changes I did in my code).

Will update here in few hours. Nonetheless. Thanks for this!

I got a chance to verify my hypothesis that since the example code had run successfully the code with above changes gave the result of static IP as the config was stored in the flash.

I was able to make it work by adding the .profile() to the config parameter as mentioned in the example code. So the config is stored against the profile I believe.

Below is the working function for the same:

void SetupWiFi()
{
    WiFi.setCredentials("TestNetwork", "password");
    Particle.process();
    Log.info("Initializing WiFi. ");
    V_GatewayStaticIPLastByte = 123;
    IPAddress V_RouterIp({192,168,0,1});
    IPAddress V_GatewayStaticIp({V_RouterIp[0], V_RouterIp[1], V_RouterIp[2], V_GatewayStaticIPLastByte});
    IPAddress V_SubnetMask({255, 255, 255, 0});
    IPAddress V_Dns({8, 8, 8, 8});

    auto conf = NetworkInterfaceConfig()
                .source(NetworkInterfaceConfigSource::STATIC, AF_INET)
                .address(V_GatewayStaticIp, V_SubnetMask)
                .gateway(V_RouterIp)
                .dns(V_RouterIp)
                .dns(V_Dns);
                .profile("TestNetwork");
    WiFi.setConfig(conf);

    Particle.process();
   
    Log.info("Using Static IP. ");      
    
    WiFi.connect();
    Particle.process();
    F_WiFiConnecting = true;
    
    V_WiFiConnectionAttempt++;
    Log.info("Attempting WiFi connection. Attempt: " + String(V_WiFiConnectionAttempt));    
}

void Fn_WiFiConnecting()
{
    Particle.process();
    if(WiFi.ready())
    {
        F_StaticIPAssigned = false;    
        F_WiFiConnecting = false;

        Particle.process();

        // finding the network wifi router ip address to which the gateway is trying to connect 
        IPAddress V_ROUTER_IP_ADDR = WiFi.gatewayIP();
        IPAddress V_GATEWAY_IP_ADDR = WiFi.localIP();
        IPAddress V_SUBNETMASK = WiFi.subnetMask();
        WiFi.BSSID(bssid);

        Log.info("Router IP Address: " + String(V_ROUTER_IP_ADDR));
        Log.info("Gateway IP Address: " + String(V_GATEWAY_IP_ADDR));
        Log.info("Router Subnet Address: " + String(V_SUBNETMASK));
        Log.info("Router MAC Address: %02X:%02X:%02X:%02X:%02X:%02X", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
        Particle.process();

        if(V_GATEWAY_IP_ADDR[3] == V_GatewayStaticIPLastByte)
        {
            Log.info("Static Ip Connected Successfully");
            V_WiFiConnectionAttempt = 0;

            F_StaticIPAssigned = true;
        }
        else
        {
            Log.info("Static Ip NOT Connected Successfully");
        }
    }
}

To run the SetupWiFi() function once and running Fn_WiFiConnecting() in loop till P2 is connected on WiFi.

Thanks a lot @rickkas7 !

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.