Code for dog door smartphone control

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]

Did you update the app in the meantime? Your code pretend to support HTTP/1.1 but doesn't provide some headers mandatory in that protocol version. So if the framework got pickier about protocol details that might explain the problems you see.

Additionally you set a static IP. How did you ensure your DHCP server doesn't give that address to another device. Such a double use of an IP might explain the symptoms you described.

No, the app has not changed. As for the address I cannot reply since I don't know. But the app specifically says that address and if used regularly it is fine but left alone for an hour or more it takes too much time to connect.

edit: I set the address to manual in router, and it will not connect at all.

If you set it to manual you have to set a fixed address for every device on the network (your PC too).

I would decrease the size of your DHCP range (if your router allows that) and give the Arduino an address outside that range. That way you can connect your PC without modification but have a reserved address for your Arduino.

OK, so notice I said it would not work set to manual. So I did not leave it set that way. What does happen is that when I run the app on the phone, it says 'error 1101: unable to get a response with the specified URL:http://192.168.1.239'
When I am way out of range of my router and I open the app it does nothing, no error messages.
So my router allows assigning DCHP around automatic ones so I think that means I can reserve addresses for specific URLs. I will try that.

Please explain what you mean about HTTP/1.1?
Thanks, Tom

The only header you supply is "Content-Type" but HTTP/1.1 requires at least a "Connection" header, otherwise the client expects a persistent connection which you don't support.

Sorry, I have no clue what this means.

Replace:

by

            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/plain");
            client.println("Content-Length: 0");
            client.println("Connection: close");
            client.println();   // The HTTP header ends with another blank line:

Upload the sketch and try again.

OK thanks. Now that is something I can read, research and begin to understand how it works.

this had unexpected results and did not read the state of the leds but after re compiling and re-uploading it now works. I will test and report back in a week or so.
Thanks, Tom

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.