Static IP (not static dynamic) [RESOLVED]

How do you configure the sparkcore to use a static ip address?

i don’t mean a static dynamic using mac addresses, i mean true static ip’s that need no router config.

I’m writing a TCPServer and was a bit surprised that none of the IPAddress functions actually set the IP, as Network.localIP(); does not match what i set.

The TI CC3000 WiFi module does this in a very funny way by stuffing the IP address into the MAC address and the current core firmware does not handle this (which I think is perfectly reasonable). You can do it yourself with the instructions here and calling netapp_dhcp and reseting the TI CC3000.

http://processors.wiki.ti.com/index.php/CC3000_Basic_Wi-Fi_example_application_for_MSP430#Static_IP_Configuration

Since the core always has to talk to the gateway anyway, I think it makes more sense to define a static address in your router’s DHCP server.

1 Like

i don’t agree, its not very portable having to configure ether tables, or what if i don’t want to run a dhcp server, or i don’t have control of the router (e.g. corporate)?

i’ll have a look at the TI docs though, thanks for the link.

edit: i tried it, but for some reason the ip address was backwards! adafruit comments on how to do it with their cc3300 breakout: https://forums.adafruit.com/viewtopic.php?f=31&t=47004

2 Likes

Sorry to dig up an old thread. I’m thinking this could be fixed in the firmware and made configurable using the existing means of wifi configuration. It seems to be a fairly often requested feature.

netapp_dhcp() is currently called as part of the CC3000 initialization, where all values are set to 0. (activating DHCP.)

I’m wondering if static IP can be supported by storing the values that are passed to netapp_dhcp() in persistent storage? Say, somewhere in the reserved areas of flash. That way, on startup the cc3000 is configured using the values from flash - either all 0s for DHCP, or configured IP addresses for static.

Then we extend the existing tools (serial config, smartconfig, spark cli etc…) to get/set the static IP/DHCP setting by reading/writing the appropriate flash location.

@mdma i’ve been playing with just that todayas i’ve seen its been done on arduino and msp430/mbed.

i’ve managed to assign a static ip/netmask/gateway/dns by putting this in setup() - note that the ip address octets are backwards and in lowercase hex:

    unsigned long pucSubnetMask[1] = {0x00ffffff}; // 255.255.255.0
    unsigned long pucIP_Addr[1] = {0x2a00a8c0}; // 192.168.0.42
    unsigned long pucIP_DefaultGWAddr[1] = {0xfe00a8c0}; // 192.168.0.254
    unsigned long pucDNS[1] = {0x08080808}; // 8.8.8.8

    netapp_dhcp(pucIP_Addr, pucSubnetMask, pucIP_DefaultGWAddr, pucDNS);   
    wlan_stop();
    delay(200);
    wlan_start(0);

i’ve proven it by assigning an ip outside of my dhcp range. so all we need now is a pretty way of calling it and configuring it via usb/cli and i guess smartconfig.

2 Likes

Cool. Have you been able to ping the spark externally? and the spark can connect to the cloud? It might also be worth printing out the network IP to serial.

Just so you know, since these are integer values, not strings, the case doesn’t make any difference - they are converted to the same 32-bit literal values by the compiler.

They are hex, because you pack 4 bytes into one 4-byte value - each pair of hex digits is 1 byte so hex makes it easy. The values are inverted because of the cpu architecture (little-endian).

But looking at the docs, I’m surprised you have to do it like that. If you later find the spark isn’t getting the static IP, could you try something like this?

unsigned long ip[] = {192, 168, 2, 51};
unsigned long subnet[] = {255, 255, 255, 0};
unsigned long gateway[] = {192, 168, 2, 1};
unsigned long dns[] = {192, 168, 2, 1};

long status = netapp_dhcp(ip, subnet, gateway, dns);

wlan_stop();
delay(20);
wlan_start(0);
1 Like

i tried putting the individual octets into an array and it didn’t like it.

yes i can ping the core, yes it can connect to the cloud - i’ve been sending it tweets too :wink: and Serial.println(WiFi.localIP()); etc. works too - that’s how i debugged it

p.s. thanks for the explanations @mdma

Cool, nice one and congrats you got it working! Just wanted to be sure! :slight_smile:

It should be fairly straightforward to make the IP configurable, and add this to the serial config, spark cli and maybe smartconfig too.

But one thing puzzles me, why does the firmware call netaddr_dhcp() on each startup, since the values given are persisted to the cc3000 eeprom, so this method only needs calling if the values changed. This would seem to generate unnecessary wear on the cc3000’s eeprom. cc’ing @satishgn who probably knows the reason behind it!

hmm, i take it back, for some reason it doesn’t seem to work if i enable the cloud - still gets a dhcp address, but i guess that’s a symptom of doing this from the sketch rather than earlier in the library - its getting a dhcp address before entering setup()

anyway, its just a proof-of-concept, so we know it can be done (oh and it can still connect to the internet if i disable the cloud), someone just needs to add it into the libs rather than the sketch and that should sort the cloud issues. plus some exception handling :wink:

correct @mdma netapp_dhcp() need not be called everytime. I guess that was added in way back while debugging the cause of CFOD bug months ago. Certainly needs to invoke the same conditionally.

Thanks for the confirmation! Perhaps it’s best to put the netapp_dhcp() call in with the wifi configuration, so it’s only involved when wifi details are changed.

Its been a while since this thread was active. @mdma Is this still the accepted way to assign a static IP?

    unsigned long pucSubnetMask[1] = {0x00ffffff}; // 255.255.255.0    
// NOTE: the bytes are enterd in reversed pairs
    unsigned long pucIP_Addr[1] = {0x2a00a8c0}; // 192.168.0.42
    unsigned long pucIP_DefaultGWAddr[1] = {0xfe00a8c0}; // 192.168.0.254
    unsigned long pucDNS[1] = {0x08080808}; // 8.8.8.8

    netapp_dhcp(pucIP_Addr, pucSubnetMask, pucIP_DefaultGWAddr, pucDNS);   
    wlan_stop();
    delay(200);
    wlan_start(0);

And are the other steps still needed. (what @bko mentioned about reseting the TI CC3000. ) or can I just plug this code into my .ino file? My problem is that I would like to operate this from home, I could port forward using the MAC address, but at work I have a set internal IP connected to an external IP, and then from my cells hotspot (OK this last one may never work since I would need some kind of control over my service provider ) Any suggestions? I am leaning towards @bko suggestion to have my Cores TCP server connect to an external Nodejs server to handle the communication.

Just to be clear: this method will only work on the TI CC3000, not on the new Photon etc.

So on the Photon, can the static IP be set???

yes come on, its crazy that we had to resort to a hack to get this working on the core, it needs to be done properly for the photon.

netapp_dhcp() no longer seems to work at all in the develop or hal branches, i assume its been replaced by something not cc3300-specific, if so what with?

do we have a portable way to assign a static ip on both devices yet?

Hi @sej7278

I don’t see anything in the HAL layer to allow you to set static IP addresses. Perhaps @mdma could have a look.

If you want to open or comment on an issue on github, that would get it on their backlog.

ok thanks, i’ll wait to see what @mdma says before raising a github issue.

there used to be a very colourful website listing the backlog (not github) can anyone remember that, sorry i’ve been away from the community for a while.

If you add #include "netapp.h" that will make it compile. Previously on the core the whole stack was included by default, including the cc3000 headers. This causes quite a few issues - delay() vs Delay() and TRUE FALSE being defined and used (when people should be using true and false.)

They were removed when making the HAL to be sure our code was truly platform agnostic. Now we have a separate user module, these can be added back into the user namespace so that user apps get full access to the underlying platform (on the Core.) I will add this in the develop branch shortly.

1 Like

@mdma, it sounds like including “netapp.h” only applies to the Core. Does the Photon suppot static IPs?