Wednesday, 15 March 2017

setup fast websocket for arduino uno

I am trying to make my Arduino Uno and an Apache server to talk to each other; however, it seems like this is not working out. My code is as follows:

#include <SPI.h>
#include <Ethernet2.h>
#include <WebSocketClient.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the     shield
byte mac[] = {
0x90, 0xA2, 0xDA, 0x10, 0xC6, 0x50};
IPAddress ip(192,168,1, 204);
IPAddress gateway( 192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

char server[] = "192.168.1.222";
WebSocketClient webSocketClient;
EthernetClient client;

void setup() {

Ethernet.begin(mac, ip);
//client.connect(server, 80);
Serial.begin(9600);
Serial.print("mac is ");

for(int i = 0; i < 5; i++){
Serial.print(".");
Serial.print(mac[i], HEX);
}
Serial.print("\nIP address: ");
Serial.print(Ethernet.localIP());

// if you get a connection, report back via serial:
if (client.connect(server, 8080)) {
  Serial.println("\nConnected!! :)");/*
  //Make a HTTP request:
  client.println("GET /search?q=arduino HTTP/1.1");
  client.println("Host: www.google.com");
  client.println("Connection: close");
  client.println();*/
}
else {
  // kf you didn't get a connection to the server:
  Serial.println("\nConnection failed >B(");
}

 Serial.print("set path\n");
 webSocketClient.path = "/";
 Serial.print(webSocketClient.path);
 Serial.print("\n");
 webSocketClient.host = server;
 Serial.print(webSocketClient.host);
 delay(3000);
 webSocketClient.handshake(client);
 if (webSocketClient.handshake(client)){
  Serial.println("Handshake successful");
  }
  else{
  Serial.println("Handshake failed.");
 while(1) {;}
 }
 Serial.print(webSocketClient.handshake(client));
}
void loop(){
  String data;
if (client.connected()) {
Serial.print(webSocketClient.getData(data));
webSocketClient.getData(data);

if (data.length() > 0) {
  Serial.print("Received data: ");
  Serial.println(data);
}

// capture the value of analog 1, send it along
pinMode(1, INPUT);
data = String(analogRead(1));
webSocketClient.sendData(data);
} else {

Serial.println("Client disconnected.");
while (1) {
  // Hang on disconnect.
}
}

// wait to fully let the client disconnect
 delay(3000);
}

When I test the connection using wiresharkI noticed there is a request for upgrade and switching protocols, couple of packets later websocket connection closes. at switching protocols:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: OXsMTNS9MCxsGzrdQVtyN5QuKaI=

GET / HTTP/1.1

Upgrade: websocket

Connection: Upgrade

Host: 192.168.1.222
Sec-WebSocket-Key: FrxOqclyL3fRSbq+g1NBSg==
Sec-WebSocket-Protocol: 
Sec-WebSocket-Version: 13


GE....T / HTTP/1.1

U....

at websocket Connection Cloe [FIN]:

Payload
  Close
    Status Code: Protocol error (1002)

Any idea on where I'm screwing up its communication? thank you!

The websocket lib I am using is called Arduino-websockets-fast



via Jelo Rilo

No comments:

Post a Comment