Ameba-AIoT / ameba-arduino-pro2

AmebaPro2 Arduino third-party package SDK
https://www.amebaiot.com/en/ameba-arduino-summary/
MIT License
50 stars 23 forks source link

[WiFiServer/WiFiClient] Connection doesn’t persist when client object goes out of scope #257

Closed redelacruz closed 1 month ago

redelacruz commented 3 months ago

Boards

AMB82-Mini

External Hardware

Other than included JXF37 camera, none.

Hardware Configuration

None

Version

latest main (checkout manually)

IDE Name

VSCode w/ Arduino extension

Operating System

Windows 11

Auto Flash Mode

Disable

Erase All Flash Memory (16MB)

Disable

Standard Lib

Arduino_STD_PRINTF

Upload Speed

2000000

Description

Copying bug report in AmebaIOT forum to make reference issue for PR.


According to Arduino’s docs for server.available():

The connection persists when the returned client object goes out of scope; you can close it by calling client.stop().

However, we observed that the current behavior of WiFiServer and WiFiClient as it has been implemented into the Arduino SDK for the AMB82-Mini seems to contradict this.

Sketch

#include <WiFi.h>

char ssid[] = "";    // your network SSID (name)
char pass[] = "";        // your network password
int status = WL_IDLE_STATUS;
WiFiServer server(80);
WiFiClient client;

void setup()
{
    Serial.begin(115200);
    while (status != WL_CONNECTED) {
        status = WiFi.begin(ssid, pass);
        delay(5000);
    }

    server.begin();
}

void loop()
{
    client = server.available();
    if (client) {
        Serial.println("new client connected");
        String currentLine = "";
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();
                Serial.write(c);
                if (c == '\n') {
                    if (currentLine.length() == 0) {
                        //do something
                    } else {
                        currentLine = "";
                    }
                } else if (c != '\r') {
                    currentLine += c;
                }
            }
        }
        client.stop();
        Serial.println("client disconnected");
    } else {
        Serial.println("waiting for client connection");
        delay(1000);
    }
}

Error/Debug Message

[INFO] A client connected to this server :
[PORT]: 51193
[IP]:192.168.2.36

new client connected
client disconnected

[INFO] A client connected to this server :
[PORT]: 51192
[IP]:192.168.2.36

new client connected
client disconnected

[INFO] A client connected to this server :
[PORT]: 51194
[IP]:192.168.2.36

new client connected
client disconnected

// ...continues indefinitely

Reproduce remarks

No response

I have checked online documentation, FAQ, GitHub Wiki and existing/closed issues.

github-actions[bot] commented 3 months ago

Hello, hope this message finds you well. Congrats to your first Issue! We will review it as soon as possiable. Feel free to have a look at https://www.amebaiot.com/en/ameba-arduino-summary/ for more information

github-actions[bot] commented 3 months ago

This issue is stale because it has been open for 14 days with no activity.