Skip to content

Library hangs on PICOW when WebSocketClient is allocated dynamically #893

Open
@riccardobl

Description

@riccardobl

Hello, i have an issue in using this library on a raspberry pico w, where i need to instantiate WebSocketClient dynamically.
Please check these two snippets below, the only difference being that Code B creates a new instance of WebSocketsClient using new.

Code A

 
#define USE_SERIAL Serial1

#include <Arduino.h>
#include <WebSocketsClient.h>
#include <Hash.h>
#include <SPI.h>
#include <WiFi.h>

IPAddress dns(8, 8, 8, 8); 
WebSocketsClient webSocket;

char ssid[] = "xx";      
char pass[] = "xx"; 

int status = WL_IDLE_STATUS;

void webSocketEvent(WStype_t type, uint8_t *payload, size_t length) {
    switch (type) {
    case WStype_DISCONNECTED:
        Serial.printf("[WSc] Disconnected!\n");
        break;
    case WStype_CONNECTED: {
        Serial.printf("[WSc] Connected to url: %s\n", payload);

        webSocket.sendTXT("Connected");
    } break;
    case WStype_TEXT:
        Serial.printf("[WSc] get text: %s\n", payload);
        break;
    case WStype_BIN:
        Serial.printf("[WSc] get binary length: %u\n", length);
        break;
    }
}

void setup() {
    Serial.begin(115200);
    while (!Serial) {
        Serial.println("Serial not ready");
    }

    if (WiFi.status() == WL_NO_SHIELD) {        
        while (true){
            Serial.println("WiFi shield not present");
        }
    }

    while (status != WL_CONNECTED) {
        Serial.print("Attempting to connect to SSID: ");
        Serial.println(ssid);
        status = WiFi.begin(ssid, pass);
        delay(10000);
    }

    WiFi.setDNS(dns);
    Serial.print("Dns configured.");
    
    webSocket.beginSSL("echo.websocket.org", 443);
    webSocket.onEvent(webSocketEvent);
}

  

void loop() {
    webSocket.loop();
}

Code B

 
#define USE_SERIAL Serial1

#include <Arduino.h>
#include <WebSocketsClient.h>
#include <Hash.h>
#include <SPI.h>
#include <WiFi.h>

IPAddress dns(8, 8, 8, 8); 
WebSocketsClient *webSocket;

char ssid[] = "xx";      
char pass[] = "xx"; 

int status = WL_IDLE_STATUS;

void webSocketEvent(WStype_t type, uint8_t *payload, size_t length) {
    switch (type) {
    case WStype_DISCONNECTED:
        Serial.printf("[WSc] Disconnected!\n");
        break;
    case WStype_CONNECTED: {
        Serial.printf("[WSc] Connected to url: %s\n", payload);

        webSocket->sendTXT("Connected");
    } break;
    case WStype_TEXT:
        Serial.printf("[WSc] get text: %s\n", payload);
        break;
    case WStype_BIN:
        Serial.printf("[WSc] get binary length: %u\n", length);
        break;
    }
}

void setup() {
    Serial.begin(115200);
    while (!Serial) {
        Serial.println("Serial not ready");
    }

    if (WiFi.status() == WL_NO_SHIELD) {        
        while (true){
            Serial.println("WiFi shield not present");
        }
    }

    while (status != WL_CONNECTED) {
        Serial.print("Attempting to connect to SSID: ");
        Serial.println(ssid);
        status = WiFi.begin(ssid, pass);
        delay(10000);
    }

    WiFi.setDNS(dns);
    Serial.print("Dns configured.");
    webSocket=new WebSocketsClient();
    webSocket->beginSSL("echo.websocket.org", 443);
    webSocket->onEvent(webSocketEvent);
}

  

void loop() {
    webSocket->loop();
}

The first one works fine, it connects and it get some text from the webserver.
The second one hangs the board stopping the execution and requiring an hard reset.
From my tests it seems it enters but never exit the webSocket->loop().

This is my platformio.ini


[env:PICOWSecureWebSocket]
monitor_speed = 115200
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
board = rpipicow
framework = arduino
board_build.core = earlephilhower

build_flags = 
    -DDEBUG_RP2040_WIRE 
    -DDEBUG_RP2040_SPI 
    -DDEBUG_RP2040_CORE
    -DPIO_FRAMEWORK_ARDUINO_ENABLE_RTTI
    -fexceptions
build_unflags = 
    -fno-exceptions
    -Wsign-compare
build_src_filter = 
    +<../tests/PICOWSecureWebSocket/>
lib_deps = 
    ${env.lib_deps}
    WebSockets@^2.4.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions