gilmaimon / ArduinoWebsockets

A library for writing modern websockets applications with Arduino (ESP8266 and ESP32)
GNU General Public License v3.0
482 stars 97 forks source link

ESP32-Server Not Working Properly receives one msg and gets disconnected #135

Open ashutosh7777 opened 2 years ago

ashutosh7777 commented 2 years ago

/**** image

ESP32-Server.ino For ESP32.

/* Minimal Esp32 Websockets Server

This sketch:

  1. Connects to a WiFi network
  2. Starts a websocket server on port 80
  3. Waits for connections
  4. Once a client connects, it wait for a message from the client
  5. Sends an "echo" message to the client
  6. closes the connection and goes back to step 3

    Hardware: For this sketch you only need an ESP32 board.

    Created 15/02/2019 By Gil Maimon https://github.com/gilmaimon/ArduinoWebsockets */

include

include

const char ssid = "only"; //Enter SSID const char password = "ashu1234"; //Enter Password

using namespace websockets;

WebsocketsServer server; void setup() { Serial.begin(115200); // Connect to wifi WiFi.begin(ssid, password);

// Wait some time to connect to wifi for(int i = 0; i < 15 && WiFi.status() != WL_CONNECTED; i++) { Serial.print("."); delay(1000); }

Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); //You can get IP address assigned to ESP

server.listen(80); Serial.print("Is server live? "); Serial.println(server.available()); }

void loop() { WebsocketsClient client = server.accept(); if(client.available()) { WebsocketsMessage msg = client.readBlocking();

// log
Serial.print("Got Message: ");
Serial.println(msg.data());

// return echo

// client.send("Echo: " + msg.data());

// close the connection

// client.close(); }

delay(1000); } 161898290-8091c2db-0d2f-423b-bfe1-b2fda9996ee7

any help will be great for us. thank you in advance

gilmaimon commented 2 years ago

When the client object goes out of scope, the connection is closed. If you want to keep receiving/sending messages, you should not let the object go out of scope. Instead keep interacting with it until you want it closed. You could check the wiki for more information and advanced examples for handling multiple clients and so on.

ashutosh7777 commented 2 years ago

ya i tried advance client also issue remains same

AsadDUET commented 1 year ago

same problem here. can't find any solution. @ashutosh7777 did you find any solution?

erickcrus commented 6 months ago

Same here. It seems that the board is restarting after receiving a message.

Edit: After a series of tests, I realized that in my case, the ESP32 was rebooting due to a lack of power. By adding a new power source to the relay circuit and using only the signal from the ESP32, it stopped rebooting and worked normally.