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 <ESP8266WiFi.h>
//#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <RCSwitch.h>
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 += "<html><head><meta name='viewport' content='width=device-width'><title></title><style>a{color:blue}.content1{width: 20px; float:right;}.content2{width: 20px; float:left;}</style></head><body><center>
";
webPage += "<b><font size=+2 color='#CC33FF'>Building (far end)</font></b>
";
//
webPage += "<b><font size=+1 color='#CC33FF'>Sockets 3</font></b>
";
webPage += "<a href='s3-all-on' target='x2'>All On</a> <a href='s3-all-off' target='x2'>All Off</a>
";
webPage += "<iframe name=x2 style='display:none'></iframe>";
webPage += "
</center></body></html>";
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();
}