Websockets2_generic lib

I am using this lib for esp32 client ..i want to send esp32 onboard led states to server..
client.onMessage([&](WebsocketsMessage message)
{
if
(strcmp(message.data(), "flashlight") == 0)
{
fl_state = fl_state ? 0 : 1;
Serial.printf("Toggling fashlight to %u\n", fl_state);
digitalWrite(fl, fl_state);
}else{
Serial.print("Got Message: ");
Serial.println(message.data());
});

this is my code ...but something errors.

As my crystal ball is currently out of order I cannot help you as I cannot see what error you get. And if you want help from us, post complete code because as experience shows, the error in the majority of cases in that part of the code people are hiding from us.

If you post code use code tags (the </> button in the editor)!

#include "String.h"
#include "defines.h"
#include <WebSockets2_Generic.h>
#include <WiFi.h>
const int fl=2;
int fl_state=0;

using namespace websockets2_generic;

WebsocketsClient client;

void onEventsCallback(WebsocketsEvent event, String data)
{
(void) data;

if (event == WebsocketsEvent::ConnectionOpened)
{
Serial.println("Connnection Opened");
}
else if (event == WebsocketsEvent::ConnectionClosed)
{
Serial.println("Connnection Closed");
}
else if (event == WebsocketsEvent::GotPing)
{
Serial.println("Got a Ping!");
}
else if (event == WebsocketsEvent::GotPong)
{
Serial.println("Got a Pong!");
}
}

void setup()
{}
pinMode(fl, OUTPUT);
digitalWrite(fl, LOW);
Serial.begin(115200);

while (!Serial && millis() < 5000);

Serial.print("\nStart ESP32-Client on ");
Serial.println(ARDUINO_BOARD);
Serial.println(WEBSOCKETS2_GENERIC_VERSION);

WiFi.begin(ssid, password);
for (int i = 0; i < 10 && WiFi.status() != WL_CONNECTED; i++)
{
Serial.print(".");
delay(1000);
}
if (WiFi.status() != WL_CONNECTED)
{
Serial.println("No Wifi!");
return;
}

Serial.println("Connected to Wifi, Connecting to WebSockets Server @");
Serial.println(websockets_server_host);

client.onMessage([&](WebsocketsMessage message)
{
if (strcmp((char *)message.data(), "flashlight") == 0)
{
fl_state = fl_state ? 0 : 1;
Serial.printf("Toggling fashlight to %u\n", fl_state);
digitalWrite(fl, fl_state);
}else{
Serial.print("Got Message: ");
Serial.println(message.data());
}
});
client.onEvent(onEventsCallback);
bool connected = client.connect(websockets_server_host, websockets_server_port, "/");

if (connected)
{
Serial.println("Connected!");
String WS_msg = "{"msg":"vijay"}";
// String WS_msg = "{message:10,code:20}";
client.send(WS_msg);
}

void loop()
{

if (client.available())
{
client.poll();
}

}

Edit your code and insert code tags (the </> button in the editor)!

thanks for reply..problem is solved before.
....

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.