Skip to content

Commit

Permalink
updated documentation + example & turned off debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
fr33r4ng3r committed Oct 21, 2012
1 parent 00ecb04 commit 83b2a24
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# ArduinoWebsocketClient, an Arduino client for connecting and messaging with Websockets
Blog: [World Domination Using Arduinos And Websockets](http://kevinrohling.wordpress.com/2011/09/14/world-domination-using-arduinos-and-websockets)
Blog: [Smart Phone Powered Home Automation using Websockets and Amazon EC2](http://www.incamoon.com)

Websockets currently provide a simple and lightweight way to send and receive messages from web browsers. This project was developed to extend this capability to embedded devices (Arduinos). It is my hope that allowing devices to easily send information about themselves as well as respond to messages received from hosted services will result in some interesting applications.

## Caveats

This library doesn't support every inch of the Websocket spec, most notably the use of a Sec-Websocket-Key. Also, because Arduino doesn't support SSL, this library also doesn't support the use of Websockets over https. If you're interested in learning more about the Websocket spec I recommend checking out the [Wikipedia Page](http://en.wikipedia.org/wiki/WebSocket). Now that I've got that out of the way, I've been able to successfully use this to connect to several hosted Websocket services, including: [echo.websocket.org](http://websocket.org/echo.html) and [pusherapp.com](http://pusherapp.com).

UPDATE: [RFC 6544](http://tools.ietf.org/html/rfc6455) support is improving. Sec-Websocket-Key + Sec-Websocket-Protocol are now supported. Also, the client will re-establish lost connections.

## Installation instructions

Once you've cloned this repo locally, copy the ArduinoWebsocketClient directory into your Arduino Sketchbook directory under Libraries then restart the Arduino IDE so that it notices the new library. Now, under File\Examples you should see ArduinoWebsocketClient. To use the library in your app, select Sketch\Import Library\ArduinoWebsocketClient.
Expand All @@ -22,16 +25,17 @@ void setup() {
Serial.begin(9600);
Ethernet.begin(mac);
client.connect(server);
client.setDataArrivedDelegate(dataArrived);
client.onMessage(onMessage);
client.send("Hello World!");
}
void loop() {
client.monitor();
}
void dataArrived(WebSocketClient client, String data) {
Serial.println("Data Arrived: " + data);
void onMessage(WebSocketClient client, char* message) {
Serial.print("Received: ");
Serial.println(message);
}
```

Expand Down
17 changes: 14 additions & 3 deletions WebSocketClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ prog_char clientHandshakeLine4[] PROGMEM = "Host: ";
prog_char clientHandshakeLine5[] PROGMEM = "Sec-WebSocket-Origin: ArduinoWebSocketClient";
prog_char clientHandshakeLine6[] PROGMEM = "Sec-WebSocket-Version: 13";
prog_char clientHandshakeLine7[] PROGMEM = "Sec-WebSocket-Key: ";
prog_char clientHandshakeLine8[] PROGMEM = "Sec-WebSocket-Protocol: ";
prog_char serverHandshake[] PROGMEM = "HTTP/1.1 101";

PROGMEM const char *WebSocketClientStringTable[] =
Expand All @@ -50,6 +51,7 @@ PROGMEM const char *WebSocketClientStringTable[] =
clientHandshakeLine5,
clientHandshakeLine6,
clientHandshakeLine7,
clientHandshakeLine8,
serverHandshake
};

Expand All @@ -73,10 +75,15 @@ void WebSocketClient::reconnect() {
result = readHandshake();
}
if(!result) {

#ifdef DEBUG
debug(F("Connection Failed!"));
#endif

if(_onError != NULL) {
_onError(*this, "Connection Failed!");
}
_client.stop();
}
}

Expand Down Expand Up @@ -135,7 +142,7 @@ void WebSocketClient::monitor () {
len += nextByte();
} else if (len == 127) {
len = nextByte();
for(int i = 0; i < 7; i++) { // NOTE: This may not be correct. RFC 6455 defines network byte order. (section 5.2)
for(int i = 0; i < 7; i++) { // NOTE: This may not be correct. RFC 6455 defines network byte order(??). (section 5.2)
len <<= 8;
len += nextByte();
}
Expand Down Expand Up @@ -334,7 +341,11 @@ void WebSocketClient::sendHandshake(char* hostname, char* path, char* protocol)

generateHash(buffer);
_client.println(buffer);


getStringTableItem(buffer, 8);
_client.print(buffer);
_client.println(protocol);

_client.println();
}

Expand All @@ -343,7 +354,7 @@ bool WebSocketClient::readHandshake() {
char line[128];
int maxAttempts = 300, attempts = 0;
char response[12];
getStringTableItem(response, 8);
getStringTableItem(response, 9);

while(_client.available() == 0 && attempts < maxAttempts)
{
Expand Down
4 changes: 2 additions & 2 deletions WebSocketClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
#ifndef WEBSOCKETCLIENT_H
#define WEBSOCKETCLIENT_H_

#define TRACE // uncomment to support TRACE level debugging of wire protocol
#define DEBUG // turn on debugging
//#define TRACE // uncomment to support TRACE level debugging of wire protocol
//#define DEBUG // turn on debugging

#define RETRY_TIMEOUT 3000

Expand Down
27 changes: 19 additions & 8 deletions examples/EchoExample/EchoExample.ino
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
#include "Arduino.h"
#include <Ethernet.h>
#include <SPI.h>
#include <SoftwareSerial.h>
#include <WebSocketClient.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "echo.websocket.org";
WebSocketClient client;
SoftwareSerial debug = SoftwareSerial(7,6);

void setup() {
Serial.begin(9600);
Ethernet.begin(mac);
client.connect(server);
client.setDataArrivedDelegate(dataArrived);
client.send("Hello World!");
debug.begin(9600);
Ethernet.begin(mac);
client.setDebug(&debug);
client.connect(server);
client.onMessage(onMessage);
client.onError(onError);
debug.println("Connected!");
client.send("Hello World!");
}

void loop() {
client.monitor();
client.monitor();
}

void dataArrived(WebSocketClient client, String data) {
Serial.println("Data Arrived: " + data);
void onMessage(WebSocketClient client, char* message) {
debug.print("Received: ");
debug.println(message);
}

void onError(WebSocketClient client, char* message) {
debug.print("ERROR: ");
debug.println(message);
}

0 comments on commit 83b2a24

Please sign in to comment.