I am trying to create a websocket connection between my Arduino Mega board(client) and a python script (server) running on my windows 10 PC. I am using a W5100 Ethernet shield and connecting directly to my PC. Hopefully it is feasible to do this with static IP addresses and without the need of a router.
I am using the WebSocket library GitHub - krohling/ArduinoWebsocketClient: Websocket client for Arduino
Notice that library is a little outdated so I needed to replace the WebsocketClient.cpp file with the one I found here.
Here is the Python Code I am running on my PC:
import asyncio
import websockets
async def echo(websocket, path):
print('connection made!!!')
async for message in websocket:
print(message)
await websocket.send("Sending Some Data")
asyncio.get_event_loop().run_until_complete(
websockets.serve(echo, '192.168.168.101', 9339)) #this is the ip address of my ethernet adapter
asyncio.get_event_loop().run_forever()
This is the code I am running on my arduino:
#include "Arduino.h"
#include <Ethernet.h>
#include <SPI.h>
#include <WebSocketClient.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char host[] = "192.168.168.101:9339";
IPAddress server(192,168,168,101);
uint16_t port = 9339;
IPAddress myIP(10, 1, 10, 239);
WebSocketClient client;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac);
Serial.println(Ethernet.localIP());
client.connect(host, '/', port);
client.setDataArrivedDelegate(dataArrived);
client.send("Hello World!");
}
void loop() {
client.monitor();
}
void dataArrived(WebSocketClient client, String data) {
Serial.println("Data Arrived: " + data);
}
I have tested my ethernet shield by replacing the server hostname with "echo.websocket.org", connecting it directly to my PC and bridging my PC's ethernet adapter and WiFi and that worked. Changing server to the IP address of echo.websocket.org and using that to connect worked too, but changing the IP to that of my python server fails to connect.
I also tried turning off Windows Firewall.
Any help or advice would be really appreciated.
Thank you in advance.