0

I been trying to get my ESP8266 to connect to a websocket, but no luck so far. Things that are ruled out are:

  1. The ESP8266 is connected to WiFi and has access to Internet (checked using a HTTP request).
  2. Not a problem with the socket server, I spun up my own websocket server and it was not hit.
  3. Tried port 80 and 443.

My code is as follows, keeps printing

Not Connected!

:

#include <Arduino.h>
#include "WebSocketClient.h"
#include "ESP8266WiFi.h"
    
WebSocketClient ws(true);
    
void setup() {
  Serial.begin(115200);
  WiFi.begin("SSID", "PASSWORD");
    
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
}
    
void loop() {
  if (!ws.isConnected()) {
    ws.connect("echo.websocket.org", "/", 80);
    Serial.println("Not connected!");
  } else {
    ws.send("hello");
    
    String msg;
    if (ws.getMessage(msg)) {
      Serial.println(msg);
    }
  }
  delay(500);
}
0

1 Answer 1

2

WebSocketClient ws(true); tries to connect securely and uses WiFiClientSecure in stead of WiFiClient. This will probably require specifying a certificate or some such.

Try WebSocketClient ws(false); to see if that works (tries to force an insecure connection).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this solved it. I had been scratching my head whole day !
I ran into a new problem now, hope you don't mind helping me! I got a server running at link and it works fine, checked using link. But I am once again unable to access the server using the same code which worked for wss://echo.websocket.org.
No time atm, sorry, just a few tips: use port 80 if you can, use ws: for unsecure and wss: for secure, and maybe pose a new question here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.