Skip to content

Connect Websocket client to Socket.IO server #136

Open
@TannerGilbert

Description

@TannerGilbert

I'm currently trying to connect my Arduino Portenta H7 to a Socket.IO server, but I'm unable to establish a connection.

Arduino code:

#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoHttpClient.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(10, 0, 0, 100);

EthernetClient ethernet;

char serverAddress[] = "10.0.0.14/socket.io/?EIO=4&transport=websocket";  // server address
int port = 8080;

WebSocketClient client = WebSocketClient(ethernet, serverAddress, port);
int count = 0;

void setup() {

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Ethernet WebServer Example");

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }
}

void loop() {
  Serial.println("starting WebSocket client");
  client.begin();

  while (client.connected()) {
    Serial.print("Sending hello ");
    Serial.println(count);

    // send a hello #
    client.beginMessage(TYPE_TEXT);
    client.print("hello ");
    client.print(count);
    client.endMessage();

    // increment count for next message
    count++;

    // check if a message is available to be received
    int messageSize = client.parseMessage();

    if (messageSize > 0) {
      Serial.println("Received a message:");
      Serial.println(client.readString());
    }

    // wait 5 seconds
    delay(5000);
  }

  Serial.println("disconnected");
}

Socket.IO Example Server:

import socketio
from aiohttp import web

sio = socketio.AsyncServer()
app = web.Application()
sio.attach(app)


async def index(request):
    with open("index.html") as f:
        return web.Response(text=f.read(), content_type="text/html")


@sio.on("*")
def catch_all(event, sid, data):
    print("Socket ID: ", sid)
    print(data)


app.router.add_get("/", index)

if __name__ == "__main__":
    web.run_app(app)

Versions:

  • python-socketio: 5x
  • Engine.IO: 4
  • Socket.IO 5

I also tried using arduinoWebSockets, but I couldn't get it to work with the Arduino Portenta H7. Any help is greatly appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: imperfectionPerceived defect in any part of project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions