Double Client connect on wifi recieve

Hey everyone,

I am new to working with wifi and arduino, and have some questions about the code that is down below. This code is a mashup between the example code and the ESP32 library examples (ESP32 Useful Wi-Fi Library Functions (Arduino IDE) | Random Nerd Tutorials)

The project goal is that I can send a string of data to the microcontroller, which in turn controls an LED strip.

I am using a MakePico as microcontroller (mc).

My main question is:

  • Every time I send 1 data point to the mc, I always get 2x Client Connect - Client Disconnect. Why does it receive it twice while I only send it once?
  • And most importantly: how do I only get it to register my sent data once?

Sub questions:

  • Why do I need to instantiate a new client every loop? I only use 1 device to connect to the mc
#include <WiFi.h>

// WIFI network
const char* ssid = "networkName";
const char* password = "networkPassword";

uint16_t port = 80;

// Reconnect to WIFI
unsigned long previousMillis = 0;
unsigned long interval = 30000;

// DEBUGGING TOGGLE
bool show_debug = true;
String dbug_pfx = "[DEBUG] - ";

NetworkServer server(port);

void setup() {
  Serial.begin(115200);

  while (!Serial) {
    ;  //wait for serial port to connect
  }

  initWiFi();
  server.begin();
}

void loop() {
  checkConnection();

  NetworkClient client = server.accept();

  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) {
            // // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // // break out of the while loop:
            break;
          } else {  // if you got a newline, then clear currentLine:
            //Serial.println(currentLine);
            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
        }
      }
    }
    client.stop();
    Serial.println("Client Disconnected.");
    Serial.println();
  }
}

void initWiFi() {
  //WIFI_STA = Station: the ESP connects to an access point (router)
  //WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi ..");

  //Check for connection
  while (WiFi.status() != WL_CONNECTED) {
    // Serial.println(WiFi.status());
    Serial.print('.');
    delay(1000);
  }
  Serial.println();
  Serial.print(ssid);
  Serial.print(" | ");
  Serial.print(WiFi.localIP());
  Serial.print(" | ");
  Serial.print("RSSI: ");
  Serial.println(WiFi.RSSI());
}

void checkConnection() {
  unsigned long currentMillis = millis();
  // if WiFi is down, try reconnecting
  if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >= interval)) {
    Serial.print(millis());
    Serial.println("Reconnecting to WiFi...");
    WiFi.disconnect();
    WiFi.reconnect();
    previousMillis = currentMillis;
  }
}

Start with a working example, rather than making a mashup. I recommend to study several different examples before choosing one as a starting point.

Why do I need to instantiate a new client every loop?

You don't.

I started with the SimpleWiFiServer example, which has the same issue and is still the basis of the code. Only the connection and reconnection codes have been edited or added.

Again, take a look at some other examples.

The answer to your question of why two is answered by your question of instantiation.