1

I'm new to working with WebSockets and arduino. I'm trying to connect to a WebSocket server that I run on a separate computer from my ESP-01s. So far I'm able to connect to my wifi and get a local IP. I also tested connection between my Arduino and the server using a GET request.

My problem is that when I try to implement a WebSocketClient connection It just doesn't work. I'm using an Arduino UNO and a ESP-01S.

Here's my code:

#include <ESP8266WiFi.h>
#include <WebSocketsClient.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "my ssid";
const char* password = "my pass";
const char* webSocketServer = "my ws ip";
const int webSocketPort = 3000;
const char* targetIP = "my ws ip";  
const int targetPort = 3000;
 
WiFiClient client;
WebSocketsClient webSocket = WebSocketsClient();

void setup() {
  Serial.begin(57600);
  Serial.println("Initializing...");

  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Connect to WebSocket server
  Serial.println("Attempting to connect to WebSocket server...");
  webSocket.begin("echo.websocket.org", 80, "/");

  webSocket.onEvent(webSocketEvent);
 
  makeGETRequest();
  Serial.println("Setup complete");
}

void loop() {

  webSocket.loop();
}

void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
  switch(type) {
    case WStype_DISCONNECTED:
      Serial.println("Disconnected from WebSocket");
      break;
    case WStype_CONNECTED:
      Serial.println("Connected to WebSocket");
      break;
    case WStype_TEXT:
      Serial.printf("Received payload: %s\n", payload);
      break;
  }
}



void printIPAddress() {
  // Get and print the local IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void makeGETRequest() {
  // Create a WiFiClient object to establish a connection
  WiFiClient client;

  // Create an HTTPClient object
  HTTPClient http;

  // Construct the complete URL with the target IP and port
  String url = "http://" + String(targetIP) + ":" + String(targetPort);

  Serial.print("Making GET request to: ");
  Serial.println(url);

  // Begin the HTTP request
  if (http.begin(client, url)) {
    // Send the GET request
    int httpCode = http.GET();

    // Check the HTTP response code
    if (httpCode > 0) {
      Serial.print("HTTP response code: ");
      Serial.println(httpCode);

      // Print the response payload
      String payload = http.getString();
      Serial.println("Response payload:");
      Serial.println(payload);
    } else {
      Serial.println("Failed to get HTTP response");
    }

    // Close the connection
    http.end();
  } else {
    Serial.println("Failed to begin HTTP connection");
  }
}

I've tried to connect to both my own WebSocket server (which I've sucessfuly connected to from other PCs) and public servers such as "echo.websocket.org" but I havent been able to establish a connection (I get the message "Disconnected from WebSocket").

I am expecting the message "Connected to WebSocket".

What am I doing wrong? I'd really appreciate any help.

0

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.