Problem using a static IP with ESP8266 with Arduino IDE

I figure I am missing something simple, but I was trying to create a simple server with the adafruit Huzzah and after setting the board and libraries etc, I found "Hello Server" under the examples for the board. If I compile and load the example, it works fine, however it is using DHCP to get an IP address. Looking in the reference for WiFi WiFi - Arduino Reference there is an example for WiFi.config for setting a static IP address. When I follow the example, the call to WiFi.config(ip) breaks with the error message "HelloServer_hummer1:45: error: no matching function for call to 'ESP8266WiFiClass::config(IPAddress&)'" - if I comment out the line for WiFi.config(ip), then it compiles and loads without any problem (other than using DHCP for the IP address. Here is the basic code I have (my additions have "mjf" in the comment line)

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

// enter correct SSID and password below for my network
const char* ssid = "SSID here";
const char* password = "PASSWORD here";

IPAddress ip(192, 168, 2, 150); // mjf - added static IP address definition

// Create an instance of the server
// specify the port to listen on as an argument
ESP8266WebServer server(80);

const int led = 13;

void handleRoot() {
  digitalWrite(led, 1);
  server.send(200, "text/plain", "hello from esp8266 and Hummer 1");
  digitalWrite(led, 0);
}

void handleNotFound(){
  digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

void setup(void){
  pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  Serial.begin(115200);
  WiFi.config(ip);  // mjf - configure for static IP
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);

  server.on("/inline", [](){
    server.send(200, "text/plain", "this works as well");
  });

  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");
}

void loop(void){
  server.handleClient();
}

What is the simple piece of the puzzle that I am missing ... please? :slight_smile:

I am not sure why you think that the documentation for the Arduino WiFi class will apply to the ESP8266WiFiClass library.

Looks like I was running into a couple of issues - the first was based on other comments I had seen about "once you get the ESP8266 configured in board manager, you can just treat it like a regular Arduino". Apparently that call does exist in the ESP8266WiFi library, HOWEVER, there are apparently 2 versions of it - the older one that seems to install by default that expects 3 parameters and the newer version that expects 4 parameters. There are also some issues reported by others as to whether or not you have to put the WiFi.config() before or after the WiFi.begin() statement. Logic (and the Arduino docs say it also) that putting the config before the begin will cause the board to set the IP before connecting. Unfortunately, there seem to be a number of reports from people that indicate that when the put the .config before the .begin, it does not work as it should but putting it after the .begin sets the IP address as expected. Somewhere in here, the phase of the moon also seems to be involved. More research is required ... a Googling we will go !!

It looks like as of version 2.3.0 of he WiFi library, what is being expected for the parameters is
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 =
* (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);*
so you need to supply the IP_address, gateway and subnet with dns1 and dns2 being optional paramters (this is from the ESP8266WiFiSTA.h file).