Esp8266 not connecting to local node ws server

In the past ive connected an esp32 to a local node ws server, recently bought an esp8266 and tried running my orginal server and arduino code. The esp8266 simply wont connect to the server.

What I know:
Esp connects to wifi
Node server connects to simple ws client no problem
All devices conected to same network
Ip address correctly copied to arduino code

Ive not made any edits to the standard ArduinoWebsockets client example.
The node server code is also below.

Been two days and getting nowhere :frowning:

Ive looked all over the web for solutions
Is there a way to debug the esp8266 ws connection, force a connection?
Im running a mac m1, Im now wondering if its a security issue. Firewall is off tho.

/*
	Esp8266 Websockets Client

	This sketch:
        1. Connects to a WiFi network
        2. Connects to a Websockets server
        3. Sends the websockets server a message ("Hello Server")
        4. Prints all incoming messages while the connection is open

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

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

*/

#include <ArduinoWebsockets.h>
#include <ESP8266WiFi.h>
const char* ssid     = "SKYF57NT";
const char* password = "hcM3yHAqAN5a";
const char* websockets_server_host = "192.168.0.35"; //Enter server adress
const uint16_t websockets_server_port = 8080; // Enter server port

using namespace websockets;

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

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

    // Check if connected to wifi
    if(WiFi.status() != WL_CONNECTED) {
        Serial.println("No Wifi!");
        return;
    }

    Serial.println("Connected to Wifi, Connecting to server.");
    // try to connect to Websockets server
    bool connected = client.connect(websockets_server_host, websockets_server_port, "/");
    if(connected) {
        Serial.println("Connecetd!");
        client.send("Hello Server");
    } else {
        Serial.println("Not Connected!");
    }
    
    // run callback when messages are received
    client.onMessage([&](WebsocketsMessage message) {
        Serial.print("Got Message: ");
        Serial.println(message.data());
    });
}

void loop() {
    // let the websockets client check for incoming messages
    if(client.available()) {
        client.poll();
    }
    delay(500);
}

Here is my node server

"use strict";

const serverPort = 8080,
    http = require("http"),
    express = require("express"),
    app = express(),
    server = http.createServer(app),
    WebSocket = require("ws"),
    websocketServer = new WebSocket.Server({ server });

// Console Log the address that we need to put into our ESP later on
require("dns").lookup(require("os").hostname(), function (err, add, fam) {
  console.log("Your webSocket IP Address: " + add + ":" + serverPort);
});

//when a websocket connection is established
websocketServer.on('connection', (webSocketClient) => {
    //send feedback to the incoming connection
    webSocketClient.send('{ "connection" : "ok"}');
    
    //when a message is received
    webSocketClient.on('message', (message) => {
    // Now we have the message we parse it to change it from a string, into an object
    const msgObj = JSON.parse(message);
    console.log(msgObj);
        //for each websocket client
        websocketServer
        .clients
        .forEach( client => {
            //send the client the current message
            client.send(`{ "message" : ${message} }`);
        });
    });
});

//start the web server
server.listen(serverPort, () => {
    console.log(`Websocket server started on port ` + serverPort);
});

so you mean that from this code

you get the info that you are connected wifi but cab't connect to the websockets_server_host ?

Did you try

Ive tested my node server and that communicates with a test client using the example from mischianti.

Yes correct, im not getting a connection at this point.

 bool connected = client.connect(websockets_server_host, websockets_server_port, "/");
    if(connected) {
        Serial.println("Connecetd!");
        client.send("Hello Server");
    } else {
        Serial.println("Not Connected!");
    }

so instead of using ArduinoWebsockets the code uses this library ... may be a bug in the first one...

1 Like

Problem solved, I turned off my router firewall :face_with_peeking_eye:
Sky routers in particular seem to be restrictive, first time ive had the router block.
Thanks

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