Esp8266 Fixed IP

Hi,
My code doesn’t respond to fix IP and do different IP why ??

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
IPAddress ip(10, 0, 0, 20);
const char* ssid = “ssid”;
const char* password = “passwd”;
WiFi.begin(ssid, password);

Does any one have any idea ??
I get IP 10.0.0.19

if I change the number it doesn’t influence ??
Thanks,
Roee

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

I fell into the same trap. Strangely enough you have to set the IP address AFTER the call to Wifi.begin()- this seems to be a quirk of the ESP8266 firmware. (Tested on Wemos mini D1)

I found the answer here SOLVED: Wifi.config not working? · Issue #128 · esp8266/Arduino · GitHub

You need to supply gateway and netmask as well as IP address and call config eg

WiFi.begin(ssid, password);
IPAddress ip(192,168,1,200);   
IPAddress gateway(192,168,1,254);   
IPAddress subnet(255,255,255,0);   
WiFi.config(ip, gateway, subnet);

Thank you - this actually worked for me!!! Thanks Roee :o :o

Hmmm. Depends on WHICH ESP8266 based device you are using, what libraries, I imagine.

With the libraries recommended by Sparkfun, and their ESP8266 "Thing" (their name for it), the code is...

  WiFi.config(ip, gateway, subnet);

  WiFi.mode(WIFI_STA);

  // WiFI.begin([ssid], [passkey]) initiates a WiFI connection
  // to the stated [ssid], using the [passkey] as a WPA, WPA2,
  // or WEP passphrase.
  WiFi.begin(WiFiSSID, WiFiPSK);

(You will have put values in things like "ip" and "gateway", etc before this.)

Details in the full sourcecode provided with ArduServer example at...

http://sheepdogguides.com/elec/misc/esp8266/Esp8266-Arduserver.htm

Although marked as ‘fixed’ I would add this solution, as putting the call to config() either before or after the call to begin() did not work for me. Spent ages googling fora to no avail then found this doc:

http://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/station-class.html#config

On reading, the solution(at least for me) was to loop waiting for a connection after begin() is called as the documentation states config() will fail if there is no connection:

// call WiFi.begin() here

while (WiFi.status() != WL_CONNECTED) {
    delay(500);

  }


// now you can call WiFi.config()

This example may be usefull. Its possible to change IP at runtime.

/*
 * Helio R. de Freitas Jr.
 * This example intend to help get IP from string, 
 * or string format from IP.
 * This is useful to change IP value at runtime 
 * 28/02/2019
 * São Paulo - Brazil
 * junior700.eletro@gmail.com
 */
#include <ESP8266WiFi.h>       //manipula o wifi
#include <DNSServer.h>         //Servidor de DNS permite acesso usando nome do site
#include <ESP8266WebServer.h>  //Usado para criação de servidor Web






IPAddress    apIP(42, 42, 42, 42);            //Defining a static IP address: local & gateway HOTSPOT /softAP




char strAddr[] = "127.0.0.1"; // IP at string format



void StrToIP(char *string, IPAddress *ptrAddress)
{
String octeto="";
char *c;
c = &string[0];
int len = strlen(string);
while(*c!='.')
{  
octeto+=*c;  
c++;
len--;
}
//Serial.print(octeto);
//Serial.print('.');
(*ptrAddress)[0]=(uint8_t)octeto.toInt();
c++;
len--;
octeto="";
while(*c!='.')
{
octeto+=*c;  
c++;
len--;
}
//Serial.print(octeto);
//Serial.print('.');
(*ptrAddress)[1]=(uint8_t)octeto.toInt();

c++;
len--;
octeto="";
while(*c!='.')
{
octeto+=*c;  
c++;
len--;
}
//Serial.print(octeto);
//Serial.print('.');
(*ptrAddress)[2]=(uint8_t)octeto.toInt();
c++;
len--;
octeto="";
for(int i=0; i<len; i++)
{
octeto+=*c;  
c++;
}
//Serial.println(octeto);
(*ptrAddress)[3]=(uint8_t)octeto.toInt();
//Serial.print(Ip[0]);
//Serial.print(" ");
//Serial.print(Ip[1]);
//Serial.print(" ");
//Serial.print(Ip[2]);
//Serial.print(" ");
//Serial.println(Ip[3]);

}


void IPtoStr(char *string, IPAddress IP)
{
 String c="";
 c += IP[0] ;
 c += '.';
 c += IP[1] ;
 c += '.';
 c += IP[2] ;
 c += '.';
 c += IP[3] ;
 c.toCharArray(string, (c.length()+1) );
 }



void setup() {
  Serial.begin(9600);
delay(10000);
  char strAddr1[20]; //new string
  Serial.print("Start value to the string = "); 
  Serial.println(strAddr); 
  Serial.print("Start value to the apIP = "); 
  Serial.println(apIP); 
  StrToIP(strAddr,&apIP); //conversion: String to IP
  Serial.print("Conversion: string to  apIP = ");
  Serial.println(apIP); 
  IPtoStr(strAddr1,apIP); //conversion IP to a new string
  Serial.print("Conversion: apIP to the new string  = "); 
  Serial.println(strAddr1); 
}

void loop() {
  
  

}