Latest dog door code and operation problems

OK, this code never was perfect and when I opened it from MIT app on phone it sometimes would not run and even with lots of suggestions it was never fully fixed. Many times it would time out and say address could not be reached. But now I got new internet and the device from A T & T changed the starting IP pool in the router from 192.168.1.2 to 192.168.2.2 so I changed the IP in the MIT and on the ESP8266. The phone will only partly work now, it gets no feedback on state of leds. Very much way over my head now that I am very old. Help really appreciated.


```cpp





#include <ESP8266WiFi.h>

const char* ssid = "ASUS";
const char* password = "*******";

// Set your Static IP address
IPAddress local_IP(192, 168, 2, 239);
// Set your Gateway IP address
IPAddress gateway(192, 168, 2, 2);

IPAddress subnet(255, 255, 255, 0);

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);

  // Configures static IP address
  if (!WiFi.config(local_IP, gateway, subnet)) {
    Serial.println("STA Failed to configure");
  }

  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");
            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:

            // 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");
}

First, run this code to make sure your gateway IP is correct?

#include <ESP8266WiFi.h>

const char* ssid = "ASUS";
const char* password = "*******";

void setup() {
  Serial.begin(115200);
  Serial.println(" ");
  Serial.print("Connecting ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" ");
  Serial.print("GW_IP address: ");
  Serial.println(WiFi.gatewayIP());
}
void loop() {
  // put your main code here, to run repeatedly:

}

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