Hi, this code, or one very like it worked for a few years but now when my smartphone app sends a request there is no response and it times out. If I try it enough over many minutes it finally works and if used every 15 minutes or so it remains OK. The app was written with MIT app inventor. Here is the code, any suggestions? I may not know enough to spot problems!
[code]
#include <ESP8266WiFi.h>
const char* ssid = "ASUS" ;
const char* password = "********" ;
IPAddress subnet(255, 255, 255, 0); // Subnet Mask
IPAddress gateway(192, 168, 1, 1); // Default Gateway
IPAddress local_IP(192, 168, 1, 239); // Static IP Address for ESP8266
int insideSw = 4; // this is the 'inside' switch
int outsideSw = 5; // this is the 'outside' switch
int ledInside = 12; //led to indicate if door is available
int ledOutside = 13; //led to indicate if door is available
WiFiServer server (80);
void setup () {
Serial.begin (115200);
if (WiFi.config(local_IP, gateway, subnet)) {
Serial.println("Static IP Configured");
}
else {
Serial.println("Static IP Configuration Failed");
}
WiFi.begin(ssid, password);
Serial.print("Connecting...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi Connected! IP address:");
Serial.println(WiFi.localIP());
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
pinMode (4, OUTPUT);
pinMode (5, OUTPUT);
pinMode(12, INPUT);
pinMode(13, INPUT);
digitalWrite (insideSw, HIGH);
digitalWrite (outsideSw, HIGH);
digitalWrite (ledInside, LOW);
digitalWrite (ledOutside, LOW);
// Connect to the Wi-Fi network
Serial.println ();
Serial.println ();
Serial.print( "Connecting to" );
Serial.println(ssid);
WiFi.hostname("Name");
WiFi.begin(ssid, password);
while (WiFi.status () != WL_CONNECTED) {
delay (500);
Serial.print ( "." );
}
Serial.println ( "" );
Serial.println ( "WiFi connected" );
// Start of the Web Server
server.begin ();
Serial.println ( "Web server started." );
// This gets the IP address
Serial.print ( "This is the IP to connect:" );
Serial.print ( "http: //" );
Serial.print (WiFi.localIP ());
Serial.println ( "/" );
}
void loop() {
// Check if a client has been connected
WiFiClient client = server.available ();
if (client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response
if (currentLine.length() == 0) { // Check to see if the client request was just the IP address if it was just refresh
client.println("HTTP/1.1 200 OK"); // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
client.println("Content-Type:text/html"); // and a content-type so the client knows what's coming, then a blank line
client.println(""); // The HTTP response ends with another blank line:
// read the input pin.
int insideState = digitalRead(ledInside); // get the state of the led
int outsideState = digitalRead(ledOutside); // get the state of the led
// print out the state of the led.
client.print("inside");
client.println(insideState); // send to client
client.println(); // print blank line
delay(1); // delay in between reads for stability
client.print("outside");
client.println(outsideState); // send to client
client.println();
delay(1); // delay in between reads for stability
break; // break out of the while loop:
}
else {
currentLine = ""; // if you got a newline, then clear currentLine:
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
// Check to see if the client request was "/I" or "/O"
if (currentLine.endsWith("/I")) {
digitalWrite(insideSw, LOW); // /I triggers the inside
delay(250);
digitalWrite(insideSw, HIGH);
}
if (currentLine.endsWith("/O")) {
digitalWrite(outsideSw, LOW); // /O triggers the outside
delay(250);
digitalWrite(outsideSw, HIGH);
}
}
}
client.stop(); // close the connection:
Serial.println("client disconnected");
}
[/code]