Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

ESP32 spuriously thinks it has a client
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

ESP32 spuriously thinks it has a client

by RustyShackleford on Tue Mar 31, 2020 6:15 pm

Hopefully there is enough information (just my "loop"), without my posting all my code (you really don't want to see it). If there's some critical info I can add, let me know. Basically, my ESP32 (HUZZAH32) is a server. It controls some peripherals, turning them on and off etc, based on time-of-day. I also process HTML commands ("GET" requests) to control how all this works. The user acts as a client by sending GETs. Right now I am doing this simply with requests from a browser: the URL of the ESP32 followed by the text of the GET.

So each time thru the loop, I check to see if the loop counter is a multiple of 64K, simply so I don't waste time, and I don't need to react that quickly (that 0xffff test passes about once per second). When that happens, I check to see if the time is 700 (minutes past midnite) and do some housekeeping tasks (update sunset time, update the RTC from SNTP). I also turn the system on or off as prescribed by the various commands, using the routine 'update_offon_state'.

Finally, I check to see if there is a client, and if so I read the GET request, and process it (for example, changing the on-time and off-time for the system).

Code: Select all | TOGGLE FULL SIZE
void loop() {

  int i;
  client = server.available();   // listen for incoming clients

  // do timekeeping tasks on infrequent passes through 'loop'
  if ((loops++ & 0xffff) == 0) {
   
    update_local_time ();
    Serial.println("timekeep");
 
    // once a day, uptdate RTC from the web and get new sunset time
    if (l_now>=700 && l_prv<700) {
      update_rtc_time ();
      update_sunset_time ();
      update_offon_time ();
    }

    update_offon_state ();

    l_prv = l_now;
  }

  if (client) {                             // if you get a client,

    // get the client request
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // String to hold incoming data from client
    int newlines=0;
    while (client.connected() && newlines<2) {     // loop while client 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
          newlines++;
        } else if (c != '\r') {  // if you got anything else but a carriage return,
          currentLine += c;      // add it to the end of the currentLine
          newlines = 0;
        }
      }
    }

    // send HTTP response
    client.println("HTTP/1.1 200 OK");
    client.println("Content-type:text/html");
    client.println();
    client.print (currentLine.substring(0,3).c_str());
    client.println();
   
    // Process the client request
    if (currentLine.startsWith ("GET /")) {
      process_get (currentLine.charAt (5), currentLine.substring(6));
    }

    // close the connection:
    client.stop();
    Serial.println(" Client Disconnected.");
   }
}


The problem: The system takes a couple of minutes to react to reaching the time-of-day at which to turn on or off (by calling 'update_offon_state'. Why would that be, since that routine gets executed once per second or so (when loops&0xffff=0) ? So I look at the serial monitor and see:

    timekeep
    timekeep
    timekeep
    ...
    timekeep
    New Client.

In other words, 'timekeep' is being printed once per second or so (each time loops&0xffff=0) and this happens of order 20 times or so. And then suddenly I see "New Client". Somehow the system thinks there's an incoming client, and it hangs there awhile waiting for the request. Thus it's not cycling back thru the loop, to process the 'update_offon_state' and turn the system on or off within a second or so of the scheduled time. However, it doesn't hang forever. Typically, after a minute or two, the 'update_offon_state' does execute properly.

That's because, if I wait of order a minute, apparently it times out and I see "Client Disconnected", but it's often immediately followed by "New Client". But after this happens a time or two, the "Client Disconnected" is not followed by a "New Client" and I start seeing "timekeep" again, as expected.

So apparently it thinks it's getting a new client, even though I've issued no requests from my browser. Any idea what's going on ? More info needed ?

RustyShackleford
 
Posts: 31
Joined: Fri Jan 05, 2018 3:17 pm

Re: ESP32 spuriously thinks it has a client

by RustyShackleford on Wed Apr 01, 2020 2:11 pm

Something I can do to make my question easier to answer ?

RustyShackleford
 
Posts: 31
Joined: Fri Jan 05, 2018 3:17 pm

Re: ESP32 spuriously thinks it has a client

by RustyShackleford on Thu Apr 09, 2020 7:47 pm

In case anybody cares ...

Apparently the problem (my sketch thinking there was a new client) was due to some behavior of my browser that I don't fully understand. I wrote a perlscript which uses LWP Simple to send the GET requests to the Arduino, and everything works as expected.

RustyShackleford
 
Posts: 31
Joined: Fri Jan 05, 2018 3:17 pm

Please be positive and constructive with your questions and comments.