Use static IP in local NETWORK when connecting to Arduino IoT Cloud

Hi, I managed to configure my MKR WIFI 1010 as a client to the Arduino IoT Cloud and at the same time as a Server within my local network. To manage the connection to my WiFi router I use the Arduino Cloud methods:

This is the thingProperties file:

#include "ArduinoIoTCloudTCP.h"
// Code generated by Arduino IoT Cloud, DO NOT EDIT.

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char SSID[]     = "myWIFI";    // Network SSID (name)
const char PASS[]     = "myWiFiPassword";    // Network password (use for WPA, or use as key for WEP)

void onLed1Change();
void onLed2Change();

bool led1;
bool led2;

void initProperties(){
  ArduinoCloud.addProperty(led1, READWRITE, ON_CHANGE, onLed1Change);
  ArduinoCloud.addProperty(led2, READWRITE, ON_CHANGE, onLed2Change);
}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

And this is how I connect my board to WiFi in the sketch file:

#include <WiFiNINA.h>
#include "thingProperties.h"

int status = WL_IDLE_STATUS;
WiFiServer server(80);
WiFiClient wifiClient;

int ledPin = 2;
int connectedToWiFi = 5;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(connectedToWiFi, OUTPUT);

  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  ArduinoCloud.addCallback(ArduinoIoTCloudEvent::CONNECT, doThisOnConnect);
  ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, doThisOnSync);
  ArduinoCloud.addCallback(ArduinoIoTCloudEvent::DISCONNECT, doThisOnDisconnect);
  
  /*
     The following function allows you to obtain more information related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get. The default is 0 (only errors). Maximum is 4
 */
  setDebugMessageLevel(4);
  ArduinoCloud.printDebugInfo();
  ArduinoCloud.getConnection();

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  server.begin();
  // you're connected now, so print out the status:
  printWifiStatus();

}

With this code my MKR Wifi 1010 can talk to the Arduino cloud and be a server within local network, the problem is that the local IP it gets is random, so if the router is restarted, I would have to look for the IP through terminal. And since I don't manage the connection through WiFiNINA library but instead I use the ArduinoCloud methods, I have not been able to make the IP static in my local network. Is this possible?

Thanks

So I finally found the problem. In order to set a static IP and manage the connection to WiFi through Arduino cloud library without interfering with the MQTT connection, you need to use the WiFi.config(IP, DNS, GATEWAY, SUBNET) function but giving all 4 parameters, and the key here is to give your router's gateway IP as the 3rd parameter. In my case, for an ARRIS router, one of the default gateways is 192.168.1.254 So this is how I did it in setup() function, first I start the server with static IP and then let the Arduino Cloud manage the rest of the connection.

//The gateway of the router is necessary to config a static IP, this will change depending on the router
  WiFi.config(IPAddress(192, 168, 1, 100), //IP
     IPAddress(8,8,8,8), //DNS
     IPAddress(192,168,1,254), //GATEWAY
     IPAddress(255,255,255,0));//SUBNET
  server.begin();

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

Hello @xxxlepexxx

You should not do this:

WiFi.config(IPAddress(192, 168, 1, 100), //IP

You are giving your MKR WiFi1010 a static IP address but not telling your router that the address is reserved on your local network. Worse, 100 is in the middle of the address range on your network. You now have an IP address assigned to your device that the router might also assign to something else using DHCP. The best way to do this is to go into your router configuration and assign the desired IP address to the MAC addresses of the MKR1010, and let the router give the IP address out to it using DHCP. That way the router knows what IP address it has and won't assign it to anything else. Don't use an IP address in the middle of the range. Change the DHCP pool to start from, say, 10, then use the lowest free IP address for the MKR1010.

Duplicate IP addresses cause very weird problems that are hard to track down unless you suspect that's what you are looking for.

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