Skip to content

Commit 83b2a24

Browse files
committed
updated documentation + example & turned off debugging
1 parent 00ecb04 commit 83b2a24

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

Diff for: README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# ArduinoWebsocketClient, an Arduino client for connecting and messaging with Websockets
22
Blog: [World Domination Using Arduinos And Websockets](http://kevinrohling.wordpress.com/2011/09/14/world-domination-using-arduinos-and-websockets)
3+
Blog: [Smart Phone Powered Home Automation using Websockets and Amazon EC2](http://www.incamoon.com)
34

45
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.
56

67
## Caveats
78

89
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).
910

11+
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.
12+
1013
## Installation instructions
1114

1215
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.
@@ -22,16 +25,17 @@ void setup() {
2225
Serial.begin(9600);
2326
Ethernet.begin(mac);
2427
client.connect(server);
25-
client.setDataArrivedDelegate(dataArrived);
28+
client.onMessage(onMessage);
2629
client.send("Hello World!");
2730
}
2831
2932
void loop() {
3033
client.monitor();
3134
}
3235
33-
void dataArrived(WebSocketClient client, String data) {
34-
Serial.println("Data Arrived: " + data);
36+
void onMessage(WebSocketClient client, char* message) {
37+
Serial.print("Received: ");
38+
Serial.println(message);
3539
}
3640
```
3741

Diff for: WebSocketClient.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ prog_char clientHandshakeLine4[] PROGMEM = "Host: ";
3838
prog_char clientHandshakeLine5[] PROGMEM = "Sec-WebSocket-Origin: ArduinoWebSocketClient";
3939
prog_char clientHandshakeLine6[] PROGMEM = "Sec-WebSocket-Version: 13";
4040
prog_char clientHandshakeLine7[] PROGMEM = "Sec-WebSocket-Key: ";
41+
prog_char clientHandshakeLine8[] PROGMEM = "Sec-WebSocket-Protocol: ";
4142
prog_char serverHandshake[] PROGMEM = "HTTP/1.1 101";
4243

4344
PROGMEM const char *WebSocketClientStringTable[] =
@@ -50,6 +51,7 @@ PROGMEM const char *WebSocketClientStringTable[] =
5051
clientHandshakeLine5,
5152
clientHandshakeLine6,
5253
clientHandshakeLine7,
54+
clientHandshakeLine8,
5355
serverHandshake
5456
};
5557

@@ -73,10 +75,15 @@ void WebSocketClient::reconnect() {
7375
result = readHandshake();
7476
}
7577
if(!result) {
78+
79+
#ifdef DEBUG
7680
debug(F("Connection Failed!"));
81+
#endif
82+
7783
if(_onError != NULL) {
7884
_onError(*this, "Connection Failed!");
7985
}
86+
_client.stop();
8087
}
8188
}
8289

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

335342
generateHash(buffer);
336343
_client.println(buffer);
337-
344+
345+
getStringTableItem(buffer, 8);
346+
_client.print(buffer);
347+
_client.println(protocol);
348+
338349
_client.println();
339350
}
340351

@@ -343,7 +354,7 @@ bool WebSocketClient::readHandshake() {
343354
char line[128];
344355
int maxAttempts = 300, attempts = 0;
345356
char response[12];
346-
getStringTableItem(response, 8);
357+
getStringTableItem(response, 9);
347358

348359
while(_client.available() == 0 && attempts < maxAttempts)
349360
{

Diff for: WebSocketClient.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
#ifndef WEBSOCKETCLIENT_H
2828
#define WEBSOCKETCLIENT_H_
2929

30-
#define TRACE // uncomment to support TRACE level debugging of wire protocol
31-
#define DEBUG // turn on debugging
30+
//#define TRACE // uncomment to support TRACE level debugging of wire protocol
31+
//#define DEBUG // turn on debugging
3232

3333
#define RETRY_TIMEOUT 3000
3434

Diff for: examples/EchoExample/EchoExample.ino

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
#include "Arduino.h"
22
#include <Ethernet.h>
33
#include <SPI.h>
4+
#include <SoftwareSerial.h>
45
#include <WebSocketClient.h>
56

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

1012
void setup() {
11-
Serial.begin(9600);
12-
Ethernet.begin(mac);
13-
client.connect(server);
14-
client.setDataArrivedDelegate(dataArrived);
15-
client.send("Hello World!");
13+
debug.begin(9600);
14+
Ethernet.begin(mac);
15+
client.setDebug(&debug);
16+
client.connect(server);
17+
client.onMessage(onMessage);
18+
client.onError(onError);
19+
debug.println("Connected!");
20+
client.send("Hello World!");
1621
}
1722

1823
void loop() {
19-
client.monitor();
24+
client.monitor();
2025
}
2126

22-
void dataArrived(WebSocketClient client, String data) {
23-
Serial.println("Data Arrived: " + data);
27+
void onMessage(WebSocketClient client, char* message) {
28+
debug.print("Received: ");
29+
debug.println(message);
2430
}
31+
32+
void onError(WebSocketClient client, char* message) {
33+
debug.print("ERROR: ");
34+
debug.println(message);
35+
}

0 commit comments

Comments
 (0)