[SOLVED] Getting ESP8266-12E working as webserver with fixed IP not dynamic IP

Hi

I have the following code working on the ESP8266-12E, which I am using as a webserver to serve a web page that lets me turn on and off my 433MHz plug sockets around the home using my phone.

Currently this webserver IP is assigned dynamically and I have to open a serial terminal to find out what the assigned IP is or use the fing app on my phone.

It would be easier to have the IP fixed.

The dynamically assigned IP address is assigned quickly, typically within 4 seconds of switching the ESP8266-12E on. I have attempted to modify the code. My modified code would occasionally connect to the modem router and assign the fixed IP address I wish to use, 192.168.1.125, but only occasionally. I did a load of googleing and the solution to this one is no longer obvious to me. Head is spinning. Anyone know how to modify this code to reliably and quickly, after switch on, assign a fixed IP? Thanks

The dynamically assigned IP address code, which works well is below:

//#include 
//#include 
#include 
#include 
#include 

RCSwitch mySwitch = RCSwitch();
MDNSResponder mdns;

// Replace with your network credentials
const char* ssid = "SSID";
const char* password = "password";

ESP8266WebServer server(80);

String webPage = "";

void setup(void){
  webPage += "
";  webPage += "Building (far end) ";  //  webPage += "Sockets 3 ";  webPage += "All On   All Off ";  webPage += "";  webPage += "
";  mySwitch.enableTransmit(0);      // Transmitter is connected to ESP8266 pin #D3  mySwitch.setPulseLength(200);    // Set pulse length    mySwitch.setRepeatTransmit(15);  // number of full 24-bit pulse repeats    pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN / pin beside USB port as an output  digitalWrite(LED_BUILTIN, HIGH);  // turn led off  delay(1000);  Serial.begin(115200);  WiFi.begin(ssid, password);  Serial.println("");  // Wait for connection  while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }  // Blink led when connected  int a = 0; while( a < 5 ) {  a++;  digitalWrite(LED_BUILTIN, LOW);  delay(100);   digitalWrite(LED_BUILTIN, HIGH);   delay(100);  }    Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP());    if (mdns.begin("esp8266", WiFi.localIP())) { Serial.println("MDNS responder started"); }    server.on("/",        [](){ server.send(200, "text/html", webPage); });  //  server.on("/s3-all-on",  [](){ server.send(200, "text/html", webPage); mySwitch.send("000110110100010100001101");  delay(1000);  });  server.on("/s3-all-off", [](){ server.send(200, "text/html", webPage); mySwitch.send("000110110100010100001100");  delay(1000);  });  //      server.begin();  Serial.println("HTTP server started"); } void loop(void){  server.handleClient(); }

[SOLVED] I don't know if this is the most efficient way, or indeed the best way, but this is how I got it going. (Some of the non networking related code has changed)

#include 
#include 
#include 

RCSwitch mySwitch = RCSwitch();
MDNSResponder mdns;

// Replace with your network credentials
const char* ssid = "SSID";
const char* password = "password";

IPAddress ip(192, 168, 1, 125);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
//IPAddress DNS(192, 168, 1, 249);

ESP8266WebServer server(80);

String webPage = "";

void setup(void){
  webPage += "
";   webPage += "Automation ";   //   webPage += "The Loo (end) ";   webPage += "The Loo On   The Loo Off ";   //   webPage += "";   webPage += "
";   mySwitch.enableTransmit(0);      // Transmitter is connected to ESP8266 pin #D3   pinMode(LED_BUILTIN, OUTPUT);    // Initialize the LED_BUILTIN / pin beside USB port as an output   digitalWrite(LED_BUILTIN, HIGH);  // turn led off   delay(1000);   Serial.begin(115200);   WiFi.begin(ssid, password);   Serial.println("");   // Wait for connection   while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }   // Blink led when connected   int a = 0; while( a < 5 ) {  a++;  digitalWrite(LED_BUILTIN, LOW);  delay(100);  digitalWrite(LED_BUILTIN, HIGH);  delay(100);  }     Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("DHCP IP address: "); Serial.println(WiFi.localIP());   WiFi.config(ip, gateway, subnet); delay(50);   Serial.print("New fixed IP address: "); Serial.print(WiFi.localIP()); Serial.println(" ");     if (mdns.begin("esp8266", WiFi.localIP())) { Serial.println("MDNS responder started"); }     server.on("/",                [](){ server.send(200, "text/html", webPage); });   //   // A //   server.on("/theloo-on",    [](){ server.send(200, "text/html", webPage);       int a = 0; while( a < 15 ) {  a++;          // "start pulse" long low         Serial.println(" ");         digitalWrite(0,HIGH); delayMicroseconds(3288);digitalWrite(0, LOW); delayMicroseconds(1701);         char *word = "0111000000111111000011000000";           for(int i = 0; word[i]; i++) {             if (word[i] == '1') {               // "1" long high + short low               digitalWrite(0,HIGH); delayMicroseconds(867); digitalWrite(0, LOW); delayMicroseconds(209);  //Serial.print("1");                                 } else {                // "0" short high + long low               digitalWrite(0,HIGH); delayMicroseconds(209); digitalWrite(0, LOW); delayMicroseconds(867);  //Serial.print("0");                                       }                                         }             }}); // B // // C //   server.on("/theloo-off",    [](){ server.send(200, "text/html", webPage);       int a = 0; while( a < 15 ) {  a++;          // "start pulse" long low         Serial.println(" ");         digitalWrite(0,HIGH); delayMicroseconds(3288);digitalWrite(0, LOW); delayMicroseconds(1701);         char *word = "0111000000111100111100111100";           for(int i = 0; word[i]; i++) {             if (word[i] == '1') {               // "1" long high + short low               digitalWrite(0,HIGH); delayMicroseconds(867); digitalWrite(0, LOW); delayMicroseconds(209);  //Serial.print("1");                                 } else {                // "0" short high + long low               digitalWrite(0,HIGH); delayMicroseconds(209); digitalWrite(0, LOW); delayMicroseconds(867);  //Serial.print("0");                                       }                                         }             }}); // D //   server.begin();   Serial.println("HTTP server started"); } void loop(void){   server.handleClient(); }