I am using WebSockets by Markus Sattler. I know it is not an issue with the server, as I have been able to connect to the server with ArduinoWebSockets by Gil Maimon, among other ways. I'm connected to the wifi and attempting to access a local server being hosted from my laptop on the same wifi. I made sure to allow incoming connections on the port (9000), so it's not a firewall issue either. I also tried multiple different versions of the library as I saw some threads saying it worked on certain versions. It seems to work fine, but gets caught on webSocket.begin(serverAddress, serverPort);. I would really appreciate any help, I have been stuck on this issue for ages.
#include <SPI.h>
#include <WiFiNINA.h>
#include <Arduino.h>
#include <WebSocketsClient.h>
#define SSID "---"
#define PASSWORD "---"
#define ECG_PIN A0
const char* serverAddress = "---";
const uint16_t serverPort = 9000;
FirebaseData fbdo;
WiFiClient wifi;
WebSocketsClient webSocket;
void setup() {
Serial.begin(115200);
WiFi.begin(SSID, PASSWORD);
Serial.print("Connecting to Wi-Fi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected to WiFi: ");
Serial.println(SSID);
// Initialize WebSocket
webSocket.begin(serverAddress, serverPort);
}
void loop() {
if (webSocket.isConnected()) {
Serial.print("connected");
//int ecgValue = analogRead(ECG_PIN); // Read ECG signal
//String ecgString = String(ecgValue); // Create a String object
//webSocket.sendTXT(ecgString); // Send ECG value as a string
//delay(5); // Small delay to control sampling rate
} else {
Serial.println("WebSocket not connected, attempting to reconnect...");
webSocket.begin(serverAddress, serverPort);
delay(300);
}
}
import asyncio
import websockets
async def receive_ecg(websocket):
async for message in websocket:
print(f"Received ECG Value: {message}")
async def main():
server = await websockets.serve(receive_ecg, "0.0.0.0", 9000)
print("WebSocket server started on ws://0.0.0.0:9000...")
await server.wait_closed()
asyncio.run(main())
I had initially been trying to send ecg data which is being taken from an AD8232 that is wired to the Arduino, but I have altered the code to focus on just getting it to connect.