Setting a static IP on an ESP8266

I am trying to set a static IP address on an ESP8266. According to the example I found at

https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/station-class.html#start-here

I should be able to do with with WiFi.config(ip) where ip is the static ip address. When I try and call WiFi.config I get

:\Users\ka5co\Documents\Arduino\scannetworkexample\scannetworkexample.ino: In function 'void setup()':
scannetworkexample:47:21: error: no matching function for call to 'ESP8266WiFiClass::config(IPAddress&)'
WiFi.config(ip);

password is set earlier in the code. I am including ESP8266WiFi.h and no others just as in the example.

The section of my code, in setup(), where I am trying to do this is:

Serial.println("Found 2");
      Serial.println(WiFi.SSID(1).c_str());
      Serial.println(WiFi.RSSI(1));
      char* ssid    = "Starbase1 2.5";
      WiFi.config(ip);
      WiFi.begin(ssid, password);

Are there multiple versions of ESP8266WiFi.h or do I have a wrong syntax?

Thanks.

The example uses three parameters you provide only one.

WiFi.config(staticIP, gateway, subnet);

If that does not solve your problem can you post a complete test sketch that shows the issue? Often the issue is in the part not shown.

Did not post the complete sketch because the error is the function is not found. I went digging into the ESP8266.h lib I have and did not find it so was thinking there is an other/newer version. Here is the complete sketch. Thanks

// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this


#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Wire.h>

const long utcOffsetInSeconds = 3600;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};


// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;

struct {
  char msg[6];
  uint16_t buttonState;
  int DSTemp;
} dataPacket;


void setup() {
//  Wire.begin();        // join i2c bus (address optional for master)
//  Serial.begin(9600);  // start serial for output
//  // Connect to Wi-Fi network with SSID and password
//
//WiFi.scanNetworks();
//  
//  Serial.print("Connecting to ");
//  Serial.println(ssid);
//  WiFi.config(ip);
//  WiFi.begin(ssid, password);
//  while (WiFi.status() != WL_CONNECTED) {
//    delay(500);
//    Serial.print(".");
//  }
//  // Print local IP address and start web server
//  Serial.println("");
//  Serial.println("WiFi connected.");
//  Serial.println("IP address: ");
//  Serial.println(WiFi.localIP());

// Setup to brint to monitor
  Serial.begin(115200);
  Serial.println();

// Start network connection
  const char* password = "*******";
  IPAddress ip(192, 168, 1, 202);
  int networksFound;
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

//  First scan for networks and save
networksFound=WiFi.scanNetworks();

for (int i = 0; i < networksFound; i++)
  {

    Serial.printf("%d: %s, Ch:%d (%ddBm) %s\n", i + 1, WiFi.SSID(i).c_str(), WiFi.channel(i), WiFi.RSSI(i), WiFi.encryptionType(i) == ENC_TYPE_NONE ? "open" : "");
  }

  
  if(abs(WiFi.RSSI(0))<abs(WiFi.RSSI(1))){
    Serial.println("found 1");
    Serial.println(WiFi.SSID(0).c_str());
    Serial.println(WiFi.RSSI(0));
    char* ssid   = "BigShed";
     WiFi.begin(ssid, password);
   }
    else{
      Serial.println("Found 2");
      Serial.println(WiFi.SSID(1).c_str());
      Serial.println(WiFi.RSSI(1));
      char* ssid    = "Starbase1 2.5";
      WiFi.config(ip);
      WiFi.begin(ssid, password);
    }//end if

     while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
     }

  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  //WiFi.config(ip);
  Serial.println("");
  Serial.println("IP changed");
  Serial.println("New IP address: ");
  Serial.println(WiFi.localIP());


  server.begin(); 

  timeClient.begin();
}

void loop() {
  // Get current time
  timeClient.update();

//  Serial.print(daysOfTheWeek[timeClient.getDay()]);
//  Serial.print(", ");
//  Serial.print(timeClient.getHours());
//  Serial.print(":");
//  Serial.print(timeClient.getMinutes());
//  Serial.print(":");
//  Serial.println(timeClient.getSeconds());

 // Get data from MEGA2560
  int byteCount = Wire.requestFrom(8, sizeof(dataPacket));    // request data from slave device #8
  byte *ptr = (byte*) &dataPacket;

  if ( byteCount != sizeof(dataPacket)) {
    Serial.print( "Received bad packet.  Expected " );
    Serial.print(sizeof(dataPacket));
    Serial.print( " bytes but got " );
    Serial.println( byteCount );
  }
  else {
    for ( byte i = 0; i < sizeof(dataPacket); ++i ) {
      byte c = Wire.read(); // receive a byte as character
      ptr[i] = c;
    }
//    Serial.println( "Received:");
//    Serial.print( "msg: " );
//    Serial.println( dataPacket.msg);
//    Serial.print( "Button State: " );
//    Serial.println( dataPacket.buttonState, BIN);
//    Serial.print( "DSTemp: " );
//    Serial.println( dataPacket.DSTemp );
  }

//  delay(1000);

WiFiClient client = server.available();   // Listen for incoming clients



if (client) {                             // If a new client connects,
  

}// End if(client)

} //End Loop

Hi,
I use wifiManager because password and other in code BRRRRRRHHHHH.:slight_smile:

For static ip address I’d rather use the modem router for set it !!! am I wrong? is it no possible?

Hi,

First I apologies for the multiple responses. Here is a shorter test sketch that demonstrates the problem. Actually two problems. The first is I am having problems setting the ssid within an if-else statement. I can set it but it does not pass out of the if-else statement. probably doing something simple wrong but can’t see it. The second is trying to call WiFi.config. That is where I get the function not found error. Thanks again.

#include "ESP8266WiFi.h"

//void prinScanResult(int networksFound)
//{
//  Serial.printf("%d network(s) found\n", networksFound);
//  for (int i = 0; i < networksFound; i++)
//  {
//    Serial.printf("%d: %s, Ch:%d (%ddBm) %s\n", i + 1, WiFi.SSID(i).c_str(), WiFi.channel(i), WiFi.RSSI(i), WiFi.encryptionType(i) == ENC_TYPE_NONE ? "open" : "");
//  }
//}


void setup()
{
  const char* password = "*******";
//  String Starbases1_SSID = "Starbase 2.5";
//  String BigShed_SSID = "BigShed";
  String ssid    = "abc123"; 
  IPAddress ip(192, 168, 1, 202);
  int networksFound;
  Serial.begin(115200);
  Serial.println();

  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

//  WiFi.scanNetworksAsync(prinScanResult);
networksFound=WiFi.scanNetworks();

for (int i = 0; i < networksFound; i++)
  {

    Serial.printf("%d: %s, Ch:%d (%ddBm) %s\n", i + 1, WiFi.SSID(i).c_str(), WiFi.channel(i), WiFi.RSSI(i), WiFi.encryptionType(i) == ENC_TYPE_NONE ? "open" : "");
  }

  if(abs(WiFi.RSSI(1))<abs(WiFi.RSSI(0))){
    // access point 1 is stronger look sor which SSID
    Serial.println("found 1");
    Serial.println(WiFi.SSID(1).c_str());
    Serial.println(WiFi.RSSI(1));
    String ssid   = WiFi.SSID(1);
    Serial.println(ssid); 
    }
  else{
    Serial.println("found 0");
    Serial.println(WiFi.SSID(0).c_str());
    Serial.println(WiFi.RSSI(0));
    String ssid    = WiFi.SSID(0).c_str();  
    Serial.println(ssid);    
    }//end if-else

    //String ssid    = "Starbase1 2.5"; 
    Serial.println(ssid);    
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
     }

  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  WiFi.config(ip);
  Serial.println("");
  Serial.println("IP changed");
  Serial.println("New IP address: ");
  Serial.println(WiFi.localIP());

    
}// End Setup


void loop() {}

I added the following lines to your second/shorter test sketch and the code compiles without any other issues.

IPAddress gateway(192,168,1,9);
IPAddress subnet(255,255,255,0);

...

WiFi.config(ip, gateway, subnet);

The ESP8266.h header file is the right one. It includes a couple of other header files. The one your are looking for is ESP8266WiFiSTA.h. There you can find:

//The argument order for ESP is not the same as for Arduino. However, there is compatibility code under the hood
//to detect Arduino arg order, and handle it correctly. Be aware that the Arduino default value handling doesn't
//work here (see Arduino docs for gway/subnet defaults). In other words: at least 3 args must always be given.
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);

Worked. Thanks.

One of the things I love about Arduino is there is such a wide array of devices to choose from based on your need. One of the frustrating things, for me, is that each one can have a slightly different set of commends or format for the same command. I am old school trained where there is a "book" of commands with all the inputs/outputs defined. But I will adapt and overcome.

Again thanks.

The reason you are having problems setting the ssid within an if-else statement is that you have more than one variable called ssid. If you havea global variable ssdi and then declare another varaible ssid within the scope of the if only the one in the if will be set and that is discarded as soon as you exit the if statement.