matherp
Posts: 276
Joined: Tue May 02, 2017 10:54 am

Pico_W static IP address?

Wed Apr 05, 2023 7:34 am

I can't see anywhere in the docs how to connect in station mode with a static IP address. Is it possible? How?
I know it can be set in the router but have a user who needs it defined in the Pico to allow hotswap replacement of units

Thanks

peterharperuk
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 206
Joined: Tue Jan 05, 2021 11:38 am

Re: Pico_W static IP address?

Wed Apr 05, 2023 9:18 am

I don't think you can do it yet. You need this change which isn't in the sdk yet. You could update lib/cyw43-driver to this commit and it might work ok.

https://github.com/georgerobotics/cyw43 ... 2388571716

matherp
Posts: 276
Joined: Tue May 02, 2017 10:54 am

Re: Pico_W static IP address?

Wed Apr 05, 2023 9:25 am

Doesn't that code only allow the static address to be defined in the build? I need to be able to define it at run time so the user can program it in from the application console

peterharperuk
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 206
Joined: Tue Jan 05, 2021 11:38 am

Re: Pico_W static IP address?

Wed Apr 05, 2023 11:07 am

yes - it's defined with a build flag.

matherp
Posts: 276
Joined: Tue May 02, 2017 10:54 am

Re: Pico_W static IP address?

Wed Apr 05, 2023 11:13 am

That's no use. For my user I must be able to define the static IP at runtime

mschnell
Posts: 428
Joined: Wed Jul 28, 2021 10:33 am
Location: Krefeld, Germany

Re: Pico_W static IP address?

Wed Apr 05, 2023 11:28 am

And how to configure same ?
(I once used USP broadcast for this on a wired LAN)
-Michael

matherp
Posts: 276
Joined: Tue May 02, 2017 10:54 am

Re: Pico_W static IP address?

Wed Apr 05, 2023 11:43 am

And how to configure same ?
USB console

hippy
Posts: 14339
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Pico_W static IP address?

Wed Apr 05, 2023 4:17 pm

matherp wrote:
Wed Apr 05, 2023 9:25 am
Doesn't that code only allow the static address to be defined in the build? I need to be able to define it at run time so the user can program it in from the application console
This is my biggest gripe with the Pico SDK - and more so with MicroPython. It's fine doing things in the way they mandate things being done, but if one wants to go beyond that it's often not easy.

In most cases the routines one might want to change to achieve some otherwise unsupported functionality aren't 'weak', can't simply be over-ridden and supplanted by providing a routine of the same name one self. It usually means cloning a whole file locally and changing that, then cloning other source files and build files to enure that gets used. Before you know it 'huge chunks of the SDK' have been cloned which is not just a lot of effort, but means source can diverge from SDK changes making it a nightmare to maintain.

The alternative is to hack files within the SDK but that that's not ideal either.

aalm
Posts: 36
Joined: Sun Apr 11, 2021 7:23 pm

Re: Pico_W static IP address?

Thu Apr 06, 2023 3:32 am

hippy wrote:
Wed Apr 05, 2023 4:17 pm
Before you know it 'huge chunks of the SDK' have been cloned which is not just a lot of effort, but means source can diverge from SDK changes making it a nightmare to maintain.

The alternative is to hack files within the SDK but that that's not ideal either.
That's what git is for, and it does make it rather simple, when used properly with branches and rebasing etc..

edit: I mean, none of that "cloning out of tree" is necessary, and yes, it creates that horrible situation where the maintenance is never going to stop.
Like transporting water with a bucket from a well to another :D .

harrycambs
Posts: 24
Joined: Thu Mar 23, 2023 10:52 am

Re: Pico_W static IP address?

Thu Apr 06, 2023 12:01 pm

Suggest you have a look at the lwip documentation, as it is the ip stack used in the picosdk and its well supported by many other microcontolers.

incognitum
Posts: 1361
Joined: Tue Oct 30, 2018 3:34 pm

Re: Pico_W static IP address?

Thu Apr 06, 2023 4:38 pm

matherp wrote:
Wed Apr 05, 2023 9:25 am
Doesn't that code only allow the static address to be defined in the build?
Well, if you can set it to the non-routable 0.0.0.0 that way, then hopefully the SDK is at least no longer configuring a hard coded address that may belong to somebody else on the network at initialization...

Should then be able to change the address manually later through the normal lwip functions.
Suggest you play a bit with constructs like:

Code: Select all

        struct ip_addr ip;
        IP4_ADDR(&ip, 1, 2, 3, 4);
        netif_set_ipaddr(netif_default, &ip);
Could be that you have to wait until the interface is up though, so may need to do something like:

Code: Select all

static void my_status_callback(struct netif *netif)
{
    if (netif_is_link_up(netif) && !netif_ip_addr(netif))
    {
        /* Link went up, but doesn't have proper IP yet, set it now */
        struct ip_addr ip;
        IP4_ADDR(&ip, 1, 2, 3, 4);
        netif_set_ipaddr(netif_default, &ip);
    }
}

int main()
{
   [...]
   netif_set_status_callback(netif_default, my_status_callback);
   [...]
}
(Also make sure you remove LWIP_DHCP in your lwipopts)

matherp
Posts: 276
Joined: Tue May 02, 2017 10:54 am

Re: Pico_W static IP address?

Thu Apr 06, 2023 5:17 pm

(Also make sure you remove LWIP_DHCP in your lwipopts)
That's another compile time constant which is no use. I need a single executable that is configurable through the USB console by the end user

OPTION WIFI "SSID", "password" [,IPaddress, gateway, mask, dns]

where the parameters in brackets are optional and if not specified DHCP is used (trivial to do on the ESP by the way!)

incognitum
Posts: 1361
Joined: Tue Oct 30, 2018 3:34 pm

Re: Pico_W static IP address?

Thu Apr 06, 2023 5:24 pm

matherp wrote:
Thu Apr 06, 2023 5:17 pm
where the parameters in brackets are optional and if not specified DHCP is used (trivial to do on the ESP by the way!)
Yeah, that do is a problem with the Pico SDK.
It always assumes a DHCP client code should be started on initialization if you compiled DHCP support it.

ronter
Posts: 429
Joined: Thu Nov 12, 2015 3:11 am

Re: Pico_W static IP address?

Thu Jun 08, 2023 2:59 pm

I also was looking for the ability to set a static IP address for the picoW at runtime when I found this thread. My impression after reading it was that it could only be done statically at compile time

But... @incognitum's post led me to the lwip documentation and it seemed possible.

So... I found the way to set the static IP from a configuration file that I store in the pico FAT file system; it could also be done interactive with a console CLI I guess..

The configuration looks like this...

Code: Select all

net enable .hostname=PicoW_436FAA24 .ip=192.168.1.24 .gw=192.168.1.1 .mask=255.255.255.0
net wifi add .ssid=ASUS_2.4G .password=MyPaSSwOrd_6549
net telnet .port=23
net tftp .port=69
This gets read at startup, and a "configuration" structure in RAM is populated.

In the network initialization code..

Code: Select all

	// initialize the wifi hardware
    	if  ( cyw43_arch_init()  != PICO_OK )  return false;
	
	// set station mode
	cyw43_arch_enable_sta_mode();

	bool _bUseDHCP =	( 	( config_ram.configurationNetwork.configurationNetIPaddr.addr == 0)
				||	( config_ram.configurationNetwork.configurationNetGWaddr.addr == 0 )
				||	( config_ram.configurationNetwork.configurationNetMask.addr == 0 ) );

	if ( !_bUseDHCP )
	{
#if NO_SYS == 1
		cyw43_arch_lwip_begin();
#endif
		// turn off DHCP
		dhcp_stop(cyw43_state.netif);
		// set the IP addr, gateway & net mask
		netif_set_addr( cyw43_state.netif,
						&config_ram.configurationNetwork.configurationNetIPaddr,
						&config_ram.configurationNetwork.configurationNetMask,
						&config_ram.configurationNetwork.configurationNetGWaddr);
#if NO_SYS == 1
		cyw43_arch_lwip_end();
#endif
	}

	// set the host name to configuration value
	netif_set_hostname(cyw43_state.netif, config_ram.configurationNetwork.configurationNetName );

	// disable power saving
	cyw43_wifi_pm(&cyw43_state, CYW43_NO_POWERSAVE_MODE);

     // get the mac address
     memcpy ( our_mac_addr, cyw43_state.mac, 6 );
I did not disable DHCP in lwipopts.h.

After that initialization, I do a wifi scan to find the strongest signal, then connect to it.

This, so far, seems to work fine connecting directly to my router. I still need to try it outside when it connects through a repeater. The repeaters that I have replace the actual MAC with a virtual MAC, and it's not an option that I can disable. This means at the router, the IP assigned to the picoW will be different based on the path. Sometimes the router says the picoW was given an IP address, but I couldn't connect to it. Looking at the repeater, it shows a different IP address ( no DHCP enabled in repeater ); and that address was successful to connect. :shock: This may explain some of the strange disappearances I had experienced with the picoW when I only tried the router's alleged IP.

Static IP solves all of this foofoo.

matherp
Posts: 276
Joined: Tue May 02, 2017 10:54 am

Re: Pico_W static IP address?

Wed Jun 14, 2023 8:24 am

ronter

Thanks for the post. One question: after setting up the address etc. what is the command to actually connect to the wifi. I've tried using cyw43_arch_wifi_connect_timeout_ms and this is failing. I assume I should be using a netif command?

Thanks

UPDATE

It now seems to be some sort of confusion in the router. The router shows the DHCP address but the Pico has changed to the static address and can be pinged on it. I assume this is something to do with it starting with DHCP enabled and then this being turned off. I can live with this but it is confusing.

ronter
Posts: 429
Joined: Thu Nov 12, 2015 3:11 am

Re: Pico_W static IP address?

Wed Jun 14, 2023 4:13 pm

matherp wrote:
Wed Jun 14, 2023 8:24 am
ronter

Thanks for the post. One question: after setting up the address etc. what is the command to actually connect to the wifi. I've tried using cyw43_arch_wifi_connect_timeout_ms and this is failing. I assume I should be using a netif command?

Thanks

UPDATE

It now seems to be some sort of confusion in the router. The router shows the DHCP address but the Pico has changed to the static address and can be pinged on it. I assume this is something to do with it starting with DHCP enabled and then this being turned off. I can live with this but it is confusing.
I also use cyw43_arch_wifi_connect_timeout_ms().

Ahh confusion, yes very! My situation is even more frustrating because I mostly connect through a repeater that perverts the MAC address.

I would set up the picoW indoors where it attached to my router directly, and use DHCP. I could access it with that address shown on the router. Then, I put it outside where it connected through a repeater that provided the router with a different MAC, but it wouldn't connect to the routers shown IP. I only then discovered through the repeater's web interface that it believed a different IP address - even though its own DHCP was off. That's when I knew I had to find a way to implement static IP.

Just ignore everything your lying router says, and use the static IP you set up. Has worked every time for me. In the code I posted, the DHCP is stopped before the picoW even attempts to access the WiFi. I think the router is baffled by the fact something has appeared on the network that it didn't assign an IP address.

Return to “SDK”