WiFiServer disconnects when new code is added.

So here's my setup. I have an esp8266 micro-controller that is wired to an mq-2 (smoke/gas) sensor. I'm using an MIT app inventor app to monitor the reading from the sensor and to turn the built in led on and off. So far this setup works great, but when I attempt to add more lines of code my app won't connect to the server. For example when I add,
If( reading>value )
Turn led on
Then my app won't connect to the server. Why could this be?

By that one line of code you posted, you can certainly say that nothing is wrong.

Perhaps it's wrong in the code you didn't post!

can you post the complete code?

Sorry about that, but here is the full code. I got most of it from a youtube tutorial on how to communicate android device with wifi micro controller.

#include "ESP8266WiFi.h"

int status = WL_IDLE_STATUS;
int smokeA0 = A0;
int threshold = 600; //treshold for smoke sensor

WiFiServer server(3300); //Declre server, port 80 used for internet
void setup() {
  Serial.begin(115200);
  //Initialize digital pin
  pinMode(LED_BUILTIN, OUTPUT );
  //Setting LED TO OFF (INVERTED LED)
  digitalWrite(LED_BUILTIN, HIGH);
  //Initializing analog pin
  pinMode(smokeA0,INPUT);

  //Wait for serial to begin
  while(!Serial);
  //Skip a line in serial after reset
  Serial.println();

  //Connect to internet
  Serial.println("Connecting to ssid");
  WiFi.begin("ssid","pass");
  //Wait for internet connection to establish
  delay(5000);
  status = WiFi.status();
  if( status != WL_CONNECTED)
  {
    Serial.println("Couldn't get a wifi connection");
    while(true);
  }
  else Serial.println( "Connected to ssid Successfully" );

  //Printing network info;
  Serial.print( "Printing ip address: " );
  Serial.println( WiFi.localIP());
  //Initialize server
  
  Serial.println("Initiating server");
  server.begin();
}

void loop() {
  
  WiFiClient client =server.available(); //Listen for incoming clients
  if(client)
  {
    String currentLine = ""; //Make string to hold incoming data from the client
    while(client.connected() ) //Loop while the client is connected
    {
      if( client.available() )  //If there's bytes to read from the client
      {
        char c = client.read(); //Read a byte
        if( c == '\n' )
        {
          if(currentLine.length() == 0 )
          {
            //Send sensor reading to server
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();
            client.print("Value at A0 is: ");
            client.print(analogRead(A0));
            client.print("
");
            client.println();
            break;
          }
          else {
            currentLine = "";
          }
        }
        else if( c != '\r' ) {
          currentLine += c;
        }

        //LED inverted turn on
        if( currentLine.endsWith( "GET /H" )) {
          digitalWrite(LED_BUILTIN,LOW);
        }
        //Turn LED off
        if( currentLine.endsWith( "GET /L" )) {
          digitalWrite(LED_BUILTIN, HIGH );
        }
      }
    }
  }
}

Here is a file if it makes it easier to read. Also if I add the code inside loop() or inside while(client.connected()) i get the same result and can’t connect.

sketch_jan20a.ino (2.28 KB)

which lines are the problem?
if you comment out the lines does it then work?

horace:
which lines are the problem?
if you comment out the lines does it then work?

The provided code is the functioning code. When I add
If( read > threshold )
Turn LED on

Anywhere in the loop code I can't get my server to connect to the phone. I've tried at the bigenning of the loop function, at the end, and inside the while(connected).

is read client.read() or analogueRead()?
post a sample of none workig code?

while(client.connected() )

This line will cause your code forever stuck in the loop if you do not properly close the connection (explicitly call client.stop() on the other side). For example, it will always returns TRUE even if the connection has been lost.
To be sure, you have to implement a protocol yourselves to check if the connection is still alive.

horace:
is read client.read() or analogueRead()?
post a sample of none workig code?

#include "ESP8266WiFi.h"
#include "Gsender.h"

int status = WL_IDLE_STATUS;
int smokeA0 = A0;
int threshold = 600; //treshold for smoke sensor

WiFiServer server(80); //Declre server, port 80 used for internet
void setup() {
  Serial.begin(115200);
  //Initialize digital pin
  pinMode(LED_BUILTIN, OUTPUT );
  //Setting LED TO OFF (INVERTED LED)
  digitalWrite(LED_BUILTIN, HIGH);
  //Initializing analog pin
  pinMode(smokeA0,INPUT);

  //Wait for serial to begin
  while(!Serial);
  //Skip a line in serial after reset
  Serial.println();

  //Connect to internet
  Serial.println("Connecting to ssid");
  WiFi.begin("ssid","pass");
  //Wait for internet connection to establish
  delay(5000);
  status = WiFi.status();
  if( status != WL_CONNECTED)
  {
    Serial.println("Couldn't get a wifi connection");
    while(true);
  }
  else Serial.println( "Connected to ssid Successfully" );

  //Printing network info;
  Serial.print( "Printing ip address: " );
  Serial.println( WiFi.localIP());
  //Initialize server
  
  Serial.println("Initiating server");
  server.begin();
}

void loop() {
  
  WiFiClient client =server.available(); //Listen for incoming clients
  if(client)
  {
    String currentLine = ""; //Make string to hold incoming data from the client
    while(client.connected() ) //Loop while the client is connected
    {
      if( client.available() )  //If there's bytes to read from the client
      {
        char c = client.read(); //Read a byte
        if( c == '\n' )
        {
          if(currentLine.length() == 0 )
          {
            //Send sensor reading to server
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();
            client.print("Value at A0 is: ");
            client.print(analogRead(A0));
            client.print("
");
            client.println();
            break;
          }
          else {
            currentLine = "";
          }
        }
        else if( c != '\r' ) {
          currentLine += c;
        }

        //LED inverted turn on
        if( currentLine.endsWith( "GET /H" )) {
          digitalWrite(LED_BUILTIN,LOW);
        }
        //Turn LED off
        if( currentLine.endsWith( "GET /L" )) {
          digitalWrite(LED_BUILTIN, HIGH );
        }
        //Client disconnected
        if( currentLine.endsWith( "GET /D" )) {
          client.stop();
        }
      }
      
      if( analogRead(A0) > threshold )
      {
        //Turn LED on
        digitalWrite(LED_BUILTIN, LOW);
        client.print("READING HIGH: ");
        client.print(analogRead(A0));
        break;
      } 
    }
  }
}

If the read is for analogRead. And when I add the code at the bottom it wont connect to the server.

arduino_new:

while(client.connected() )

This line will cause your code forever stuck in the loop if you do not properly close the connection (explicitly call client.stop() on the other side). For example, it will always returns TRUE even if the connection has been lost.
To be sure, you have to implement a protocol yourselves to check if the connection is still alive.

I did fix what you suggested but I don't think that is the problem since the client wont connect in the first place.

I'm guessing it has to do with the implementation. Its my first time doing something like this.

the code at end uses break; to exit the loop

    while(client.connected() ) //Loop while the client is connected
{
  ...

I think you need look at the structure of the code again