Hi everyone,
I currently have a WebSocket Server constructed using the mWebSockets.h library on a ATmega328P Arduino Uno with a W5500 Ethernet Shield. This is designed to send a message to a client running in python every time the server is queried by the client. The code for the server is shown below:
#include <WebSocketServer.h>
using namespace net;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[]{0xA8, 0x61, 0x0A, 0xAE, 0x69, 0x13};
IPAddress ip(198, 162, 1, 177);
IPAddress gateway(0,0,0,0);
IPAddress DNSserver(0,0,0,0);
IPAddress subnet(255,255,255,0);
constexpr uint16_t port = 80;
WebSocketServer wss{port};
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
// 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,DNSserver,gateway,subnet);
// 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.");
}
// start the server
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
wss.onConnection([](WebSocket & ws) {
const auto protocol = ws.getProtocol();
if (protocol) {
Serial.print(F("Client protocol: "));
Serial.println(protocol);
}
ws.onMessage([](WebSocket & ws, const WebSocket::DataType dataType,
const char *message, uint16_t length) {
switch (dataType) {
case WebSocket::DataType::TEXT:
Serial.print(F("Received: "));
Serial.println(message);
break;
case WebSocket::DataType::BINARY:
Serial.println(F("Received binary data"));
break;
}
ws.send(dataType, message, length);
});
ws.onClose([](WebSocket &, const WebSocket::CloseCode, const char *,
uint16_t) {
Serial.println(F("Disconnected"));
});
Serial.print(F("New client: "));
Serial.println(ws.getRemoteIP());
const char message[] {"Hello from Arduino server!"};
ws.send(WebSocket::DataType::TEXT, message, strlen(message));
});
wss.begin();
Serial.println(Ethernet.localIP());
}
void loop() {
wss.listen();
}
The python client code is shown below:
#!/usr/bin/python3
import asyncio
import websockets
import socket
import time
import datetime
import struct
starttime = time.time() # start value for timed data acquisition
# setup socket 1
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s1.connect(("198.162.1.177",80))
s1.sendall(b'GET / HTTP/1.1 400')
if True:
print('Connected')
def acquire():
data = s1.recv(10000)
print(data)
while True:
acquire()
time.sleep(1.0 - ((time.time() - starttime) % 1.0)) # continue every (2.0 seconds)
s1.close()
Currently, it seems like a connection can be made between the two but a handshake cannot be established:
Traceback (most recent call last):
File "websocketbasictest.py", line 11, in <module>
asyncio.get_event_loop().run_until_complete(test()) # run until test() is finished
File "/usr/lib64/python3.6/asyncio/base_events.py", line 488, in run_until_complete
return future.result()
File "websocketbasictest.py", line 5, in test
async with websockets.connect( "ws://198.162.1.177:80/") as websocket:
File "/usr/lib64/python3.6/site-packages/websockets/legacy/client.py", line 604, in __aenter__
return await self
File "/usr/lib64/python3.6/site-packages/websockets/legacy/client.py", line 634, in __await_impl__
extra_headers=protocol.extra_headers,
File "/usr/lib64/python3.6/site-packages/websockets/legacy/client.py", line 397, in handshake
response_headers, available_subprotocols
File "/usr/lib64/python3.6/site-packages/websockets/legacy/client.py", line 302, in process_subprotocol
raise InvalidHandshake("no subprotocols supported")
websockets.exceptions.InvalidHandshake: no subprotocols supported
Given that I am very new to socket communication, I am unsure how to solve this issue as I thought both the websockets library in python and the mWebSockets.h library in Arduino use the RFC6455 protocol. How do I resolve this issue?