Fixed IP on Arduino OPTA

You are having difficulty configuring the OPTA WI-FI IP to fix it, so you will find information for ESP32 and ESP8266 platforms.
In this form, someone can help me with a code that may be able to alter the IP to a fixed value and then configure the same to obtain the automatic form.

If you want a static IP address configure it in the router. Let the board get an IP address via DHCP and use the router to give only one particular IP address to that board based on its MAX address.

1 Like

Thank you very much for the answer, but out of curiosity theoretically via programming can you tell me if it would be possible to configure a fixed IP?

I don't know anything about that board, but I would assume it is possible, but I can't tell you how. If you do put a static IP address in the board then you have to make sure that it is outside the DHCP range in the router, otherwise you risk the router giving that address to another device. I promise you if that happens you will get weird and difficult to trace problems on your network.

Reserving the address in the router is the standard way to do this.

1 Like

Great, thanks a lot for the information.

Hello @hellowordbr ,

You can use the following code to set a static IP address to your Arduino Opta WiFi.

#include <SPI.h>
#include <WiFi.h>


#define ip "192.168.250.8"  // static IP address
#define subnet "255.255.255.0"
#define gateway "192.168.250.1"



const char ssid[] = SECRET_SSID;        // your network SSID (name)
const char pass[] = SECRET_PASS;    

int status = WL_IDLE_STATUS;

void setup()
{  


  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while(true);  // don't continue
  }

WiFi.config(ip, subnet, gateway);

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // print your WiFi shield's IP address:
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop () {}

Best,

1 Like

Thanks!
If I don't put the following information in the code, will OPTA configure the IP as dynamic?

#define ip "192.168.250.8" // static IP address
#define subnet "255.255.255.0
#define gateway "192.168.250.1

Hi @hellowordbr ,

The sketch will allow you to assign a static IP address. Otherwise, the router will assign a dynamic IP.
If you want to target a particular IP address, try using an IP scanner, choose an available IP, then assign the selected IP address via the sketch above.

Best,

Here is the code I use to config static IP with OPTA

#include <SPI.h>
#include <PortentaEthernet.h>
#include <Ethernet.h>

static const IPAddress STATIC_IP(192, 168, 1, 10);

void setup() {
        Serial.println("Initialize Ethernet with static IP:");
        while (Ethernet.begin(STATIC_IP) == 0) {
            if (Ethernet.linkStatus() == LinkOFF) {
                Serial.println("Ethernet cable is not connected.");
            }           
            Serial.println("Failed to configure Ethernet, retry in 10s");
            delay(10);
        }

        Serial.print("Use IP: ");
        Serial.println(Ethernet.localIP());
}

If you want to use DHCP and want to have a fixed IP, most router will have the function to assign IP based on MAC address. Just find the mac address of your controller and configure it in your router's. There is a OPTA tutorial about how to get MAC address.

2 Likes

Thanks for your help, I will test both codes received.