Author Topic: Static Ip  (Read 1984 times)

davide

  • Newbie
  • *
  • Posts: 21
Static Ip
« on: November 11, 2016, 10:51:11 am »
Does anybody know if it's possible to set a static Ip for the Oak? How?

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Static Ip
« Reply #1 on: November 11, 2016, 10:46:22 pm »
I was going to say that I thought the only way you could do it would be via setting a rule on your router/modem/whatever your wifi thingy is, and simply assigning a fixed IP to the MAC address of the Oak, but there is a way... ;)

You basically need the following four lines in your code

Code: [Select]
IPAddress ip(192, 168, 0, 12); //set the static IP
IPAddress gateway(192,168,0,1); //set the gateway (modem, router IP)
IPAddress subnet(255,255,255,0); //set the subnet mask
 
WiFi.config(ip,gateway,subnet);

There should have been a version of that command that only need the IP, but unfortunately there isn't, so you need to provide both the IP address you want, the gateway, and the subnet mask. I just tried this out on my on setup, and the Oak moved from a DHCP address to the manually assigned static IP. If you wanted to do it properly, you would have to run the Oak in SYSTEM_MODE(MANUAL) mode so that you can control when it connects to the wifi, and thus starts with a static IP, but that brings some other hassles with it. Just run those four lines (or similar) right at the start of the setup block, and it will be as near as you can easily get. Interestingly, I instantly got a message on particle saying the device was offline and then online, but the Oak kept running without any issues - and didn't reboot or anything.

A full code example can be found here.
« Last Edit: November 11, 2016, 11:20:45 pm by PeterF »

davide

  • Newbie
  • *
  • Posts: 21
Re: Static Ip
« Reply #2 on: November 12, 2016, 01:16:56 am »
I'll try it but I'm sure it will work. You are great!!! Thank you