From abb1fc28a640fda35f319bc88de54815718bc1b1 Mon Sep 17 00:00:00 2001 From: Derek P Date: Fri, 10 Feb 2012 11:06:45 -0800 Subject: [PATCH 01/10] initial sketches of the WiFlyClient integration. --- WebSocketClient.cpp | 39 ++++++++++++++++++++------------------- WebSocketClient.h | 23 +++++++++++++++++------ WiFlyClient.cpp | 14 ++++++++++++++ WiFlyClient.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 25 deletions(-) create mode 100644 WiFlyClient.cpp create mode 100644 WiFlyClient.h diff --git a/WebSocketClient.cpp b/WebSocketClient.cpp index 335329b..36bff28 100644 --- a/WebSocketClient.cpp +++ b/WebSocketClient.cpp @@ -2,17 +2,17 @@ WebsocketClient, a websocket client for Arduino Copyright 2011 Kevin Rohling http://kevinrohling.com - + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -36,7 +36,7 @@ prog_char clientHandshakeLine5[] PROGMEM = "Origin: ArduinoWebSocketClient"; prog_char serverHandshake[] PROGMEM = "HTTP/1.1 101"; PROGMEM const char *WebSocketClientStringTable[] = -{ +{ stringVar, clientHandshakeLine1, clientHandshakeLine2, @@ -52,6 +52,7 @@ String WebSocketClient::getStringTableItem(int index) { return String(buffer); } + bool WebSocketClient::connect(char hostname[], char path[], int port) { bool result = false; @@ -59,7 +60,7 @@ bool WebSocketClient::connect(char hostname[], char path[], int port) { sendHandshake(hostname, path); result = readHandshake(); } - + return result; } @@ -74,7 +75,7 @@ void WebSocketClient::disconnect() { void WebSocketClient::monitor () { char character; - + if (_client.available() > 0 && (character = _client.read()) == 0) { String data = ""; bool endReached = false; @@ -86,7 +87,7 @@ void WebSocketClient::monitor () { data += character; } } - + if (_dataArrivedDelegate != NULL) { _dataArrivedDelegate(*this, data); } @@ -105,10 +106,10 @@ void WebSocketClient::sendHandshake(char hostname[], char path[]) { String line3 = getStringTableItem(3); String line4 = getStringTableItem(4); String line5 = getStringTableItem(5); - + line1.replace(stringVar, path); line4.replace(stringVar, hostname); - + _client.println(line1); _client.println(line2); _client.println(line3); @@ -122,37 +123,37 @@ bool WebSocketClient::readHandshake() { char character; String handshake = "", line; int maxAttempts = 300, attempts = 0; - - while(_client.available() == 0 && attempts < maxAttempts) - { - delay(100); + + while(_client.available() == 0 && attempts < maxAttempts) + { + delay(100); attempts++; } - + while((line = readLine()) != "") { handshake += line + '\n'; } - + String response = getStringTableItem(6); result = handshake.indexOf(response) != -1; - + if(!result) { _client.stop(); } - + return result; } String WebSocketClient::readLine() { String line = ""; char character; - + while(_client.available() > 0 && (character = _client.read()) != '\n') { if (character != '\r' && character != -1) { line += character; } } - + return line; } diff --git a/WebSocketClient.h b/WebSocketClient.h index ab4ff77..fa499f6 100644 --- a/WebSocketClient.h +++ b/WebSocketClient.h @@ -2,17 +2,17 @@ WebsocketClient, a websocket client for Arduino Copyright 2011 Kevin Rohling http://kevinrohling.com - + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -25,14 +25,21 @@ #ifndef WEBSOCKETCLIENT_H #define WEBSOCKETCLIENT_H_ +//Uncomment this to use WIFLY Client +#define WIFLY true + #include #include #include + + +#ifdef WIFLY +#include "WiFlyClient.h" +#else #include -#include "Arduino.h" +#endif -//Uncomment this to use WIFLY Client -#define WIFLY true +#include "Arduino.h" class WebSocketClient { public: @@ -46,7 +53,11 @@ class WebSocketClient { private: String getStringTableItem(int index); void sendHandshake(char hostname[], char path[]); +#ifdef WIFLY + WiFlyClient _client; +else EthernetClient _client; +#endif DataArrivedDelegate _dataArrivedDelegate; bool readHandshake(); String readLine(); diff --git a/WiFlyClient.cpp b/WiFlyClient.cpp new file mode 100644 index 0000000..2ae9a78 --- /dev/null +++ b/WiFlyClient.cpp @@ -0,0 +1,14 @@ +#include "Arduino.h" +#include "WiFlyClient.h" +#include + + +WiFlyClient::WiFlyClient(WiFlySerial *WiFly) { + _WiFly = WiFly; +} + +int WiFlyClient::connect(char hostname[], char path[] = "/", int port = 80) { + _hostname = hostname; + _port = port; + _path = path; +} \ No newline at end of file diff --git a/WiFlyClient.h b/WiFlyClient.h new file mode 100644 index 0000000..57c8795 --- /dev/null +++ b/WiFlyClient.h @@ -0,0 +1,44 @@ +#ifndef WIFLYCLIENT_H +#define WIFLYCLIENT_H + +#include "Arduino.h" + +#include +#include +#include + +class WiFlyClient { + +public: + + // constructor expects an initialized, and fully + // connected instance of the WiFly driver. + // Let the user worry about authentication and pin mapping. + WiFlyClient(WiFlySerial *WiFly); + + int connect(char hostname[], char path[] = "/", int port = 80); + + void print(const char *str); + void print(const String *str); + + void println(const String *str); + + int read(); + int available(); + + bool connected(); + void stop(); + + operator bool(); + +private: + + WiFlySerial _WiFly; + + int _port; + char _hostname[]; + char _path[]; + +}; + +#endif \ No newline at end of file From 5da6ec8639f1eea5780a9e3b4740a36779c56a77 Mon Sep 17 00:00:00 2001 From: Derek P Date: Fri, 10 Feb 2012 11:36:06 -0800 Subject: [PATCH 02/10] simplified the API a bit, implemented what I believe to be the necessary API mapping. untested. --- WiFlyClient.cpp | 33 ++++++++++++++++++++++++++++++--- WiFlyClient.h | 6 +----- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/WiFlyClient.cpp b/WiFlyClient.cpp index 2ae9a78..2f2fa61 100644 --- a/WiFlyClient.cpp +++ b/WiFlyClient.cpp @@ -2,13 +2,40 @@ #include "WiFlyClient.h" #include - WiFlyClient::WiFlyClient(WiFlySerial *WiFly) { _WiFly = WiFly; } -int WiFlyClient::connect(char hostname[], char path[] = "/", int port = 80) { +bool WiFlyClient::connect(char hostname[], int port = 80) { _hostname = hostname; _port = port; - _path = path; + return _WiFly.openConnection(hostname, port); +} + +void print(const char *str) { + _WiFly.uart.print(str); +} + +void print(const String *str) { + _WiFly.uart.print(str); +} + +void println(const String *str) { + _WiFly.uart.println(str); +} + +int read() { + return _WiFly.uart.read(); +} + +int available() { + return _WiFly.uart.available(); +} + +bool connected() { + return _WiFly.isConnected(); +} + +void stop() { + _WiFly.closeConnection(); } \ No newline at end of file diff --git a/WiFlyClient.h b/WiFlyClient.h index 57c8795..f0201c4 100644 --- a/WiFlyClient.h +++ b/WiFlyClient.h @@ -16,7 +16,7 @@ class WiFlyClient { // Let the user worry about authentication and pin mapping. WiFlyClient(WiFlySerial *WiFly); - int connect(char hostname[], char path[] = "/", int port = 80); + bool connect(char hostname[], char path[] = "/", int port = 80); void print(const char *str); void print(const String *str); @@ -29,15 +29,11 @@ class WiFlyClient { bool connected(); void stop(); - operator bool(); - private: WiFlySerial _WiFly; - int _port; char _hostname[]; - char _path[]; }; From 0b990a9971930ee15be50286a4d50db9400bf2f2 Mon Sep 17 00:00:00 2001 From: Derek P Date: Fri, 10 Feb 2012 11:38:06 -0800 Subject: [PATCH 03/10] don't need path as part of the connect signature. --- WiFlyClient.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WiFlyClient.h b/WiFlyClient.h index f0201c4..f9a0f0c 100644 --- a/WiFlyClient.h +++ b/WiFlyClient.h @@ -16,7 +16,7 @@ class WiFlyClient { // Let the user worry about authentication and pin mapping. WiFlyClient(WiFlySerial *WiFly); - bool connect(char hostname[], char path[] = "/", int port = 80); + bool connect(char hostname[], int port = 80); void print(const char *str); void print(const String *str); From 97911750a0d0e884c2c6ca56e94140687a81e11d Mon Sep 17 00:00:00 2001 From: Derek P Date: Fri, 10 Feb 2012 11:54:09 -0800 Subject: [PATCH 04/10] not sure that we need that .h --- WiFlyClient.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/WiFlyClient.cpp b/WiFlyClient.cpp index 2f2fa61..308f9c2 100644 --- a/WiFlyClient.cpp +++ b/WiFlyClient.cpp @@ -1,4 +1,3 @@ -#include "Arduino.h" #include "WiFlyClient.h" #include From ee15648d51d5bac25a5f05862daae730f6b408fb Mon Sep 17 00:00:00 2001 From: Derek P Date: Fri, 10 Feb 2012 16:20:51 -0800 Subject: [PATCH 05/10] observed that _client.println() is used within the client, I assume this sends a \n\n, so I've adjusted the WiFlyClient class to support that notion. --- WiFlyClient.cpp | 4 ++++ WiFlyClient.h | 1 + 2 files changed, 5 insertions(+) diff --git a/WiFlyClient.cpp b/WiFlyClient.cpp index 308f9c2..5d5f469 100644 --- a/WiFlyClient.cpp +++ b/WiFlyClient.cpp @@ -23,6 +23,10 @@ void println(const String *str) { _WiFly.uart.println(str); } +void println() { + _WiFly.uart.println("\n"); +} + int read() { return _WiFly.uart.read(); } diff --git a/WiFlyClient.h b/WiFlyClient.h index f9a0f0c..2f4beb8 100644 --- a/WiFlyClient.h +++ b/WiFlyClient.h @@ -22,6 +22,7 @@ class WiFlyClient { void print(const String *str); void println(const String *str); + void println(); int read(); int available(); From ed9c7c07f9758758c083173a73873d2f2abdea33 Mon Sep 17 00:00:00 2001 From: Derek P Date: Fri, 10 Feb 2012 17:00:58 -0800 Subject: [PATCH 06/10] standardized the spacing of the project. Soft tabs, 4 spaces. --- WebSocketClient.cpp | 2 +- WebSocketClient.h | 22 ++++++++++++++-------- WiFlyClient.cpp | 26 +++++++++++++------------- WiFlyClient.h | 38 +++++++++++++++++++------------------- 4 files changed, 47 insertions(+), 41 deletions(-) diff --git a/WebSocketClient.cpp b/WebSocketClient.cpp index 36bff28..0f5eeb5 100644 --- a/WebSocketClient.cpp +++ b/WebSocketClient.cpp @@ -47,7 +47,7 @@ PROGMEM const char *WebSocketClientStringTable[] = }; String WebSocketClient::getStringTableItem(int index) { - char buffer[35]; +char buffer[35]; strcpy_P(buffer, (char*)pgm_read_word(&(WebSocketClientStringTable[index]))); return String(buffer); } diff --git a/WebSocketClient.h b/WebSocketClient.h index fa499f6..7ecd8f2 100644 --- a/WebSocketClient.h +++ b/WebSocketClient.h @@ -42,22 +42,28 @@ #include "Arduino.h" class WebSocketClient { - public: - typedef void (*DataArrivedDelegate)(WebSocketClient client, String data); - bool connect(char hostname[], char path[] = "/", int port = 80); + + public: + + typedef void (*DataArrivedDelegate)(WebSocketClient client, String data); + bool connect(char hostname[], char path[] = "/", int port = 80); bool connected(); void disconnect(); - void monitor(); - void setDataArrivedDelegate(DataArrivedDelegate dataArrivedDelegate); - void send(String data); - private: + void monitor(); + void setDataArrivedDelegate(DataArrivedDelegate dataArrivedDelegate); + void send(String data); + + private: + String getStringTableItem(int index); void sendHandshake(char hostname[], char path[]); + #ifdef WIFLY WiFlyClient _client; -else +#else EthernetClient _client; #endif + DataArrivedDelegate _dataArrivedDelegate; bool readHandshake(); String readLine(); diff --git a/WiFlyClient.cpp b/WiFlyClient.cpp index 5d5f469..0423b10 100644 --- a/WiFlyClient.cpp +++ b/WiFlyClient.cpp @@ -2,43 +2,43 @@ #include WiFlyClient::WiFlyClient(WiFlySerial *WiFly) { - _WiFly = WiFly; + _WiFly = WiFly; } -bool WiFlyClient::connect(char hostname[], int port = 80) { - _hostname = hostname; - _port = port; - return _WiFly.openConnection(hostname, port); +bool WiFlyClient::connect(const char hostname[], int port = 80) { + _hostname = hostname; + _port = port; + return _WiFly.openConnection(hostname, port); } void print(const char *str) { - _WiFly.uart.print(str); + _WiFly.uart.print(str); } void print(const String *str) { - _WiFly.uart.print(str); + _WiFly.uart.print(str); } void println(const String *str) { - _WiFly.uart.println(str); + _WiFly.uart.println(str); } void println() { - _WiFly.uart.println("\n"); + _WiFly.uart.println("\n"); } int read() { - return _WiFly.uart.read(); + return _WiFly.uart.read(); } int available() { - return _WiFly.uart.available(); + return _WiFly.uart.available(); } bool connected() { - return _WiFly.isConnected(); + return _WiFly.isConnected(); } void stop() { - _WiFly.closeConnection(); + _WiFly.closeConnection(); } \ No newline at end of file diff --git a/WiFlyClient.h b/WiFlyClient.h index 2f4beb8..2e18c62 100644 --- a/WiFlyClient.h +++ b/WiFlyClient.h @@ -1,5 +1,5 @@ #ifndef WIFLYCLIENT_H -#define WIFLYCLIENT_H +#define WIFLYCLIENT_H_ #include "Arduino.h" @@ -9,32 +9,32 @@ class WiFlyClient { -public: + public: - // constructor expects an initialized, and fully - // connected instance of the WiFly driver. - // Let the user worry about authentication and pin mapping. - WiFlyClient(WiFlySerial *WiFly); + // constructor expects an initialized, and fully + // connected instance of the WiFly driver. + // Let the user worry about authentication and pin mapping. + WiFlyClient(WiFlySerial *WiFly); - bool connect(char hostname[], int port = 80); + bool connect(const char hostname[], int port = 80); - void print(const char *str); - void print(const String *str); + void print(const char *str); + void print(const String *str); - void println(const String *str); - void println(); + void println(const String *str); + void println(); - int read(); - int available(); + int read(); + int available(); - bool connected(); - void stop(); + bool connected(); + void stop(); -private: + private: - WiFlySerial _WiFly; - int _port; - char _hostname[]; + WiFlySerial _WiFly; + int _port; + char _hostname[]; }; From a9e865511feede107001324d1d613d56743f45ee Mon Sep 17 00:00:00 2001 From: Derek P Date: Fri, 10 Feb 2012 18:11:01 -0800 Subject: [PATCH 07/10] first pass at compiling the code, it compiles, not fully tested. --- WebSocketClient.cpp | 8 ++++++++ WebSocketClient.h | 13 ++++++++++--- WiFlyClient.cpp | 30 ++++++++++++++---------------- WiFlyClient.h | 12 +++++------- 4 files changed, 37 insertions(+), 26 deletions(-) diff --git a/WebSocketClient.cpp b/WebSocketClient.cpp index 0f5eeb5..ede5bb0 100644 --- a/WebSocketClient.cpp +++ b/WebSocketClient.cpp @@ -26,6 +26,7 @@ #include #include #include +#include "WiFlyClient.h" prog_char stringVar[] PROGMEM = "{0}"; prog_char clientHandshakeLine1[] PROGMEM = "GET {0} HTTP/1.1"; @@ -46,6 +47,13 @@ PROGMEM const char *WebSocketClientStringTable[] = serverHandshake }; +#ifdef WIFLY +WebSocketClient::WebSocketClient(WiFlySerial &WiFly) : _client(WiFly) { + +} +#endif + + String WebSocketClient::getStringTableItem(int index) { char buffer[35]; strcpy_P(buffer, (char*)pgm_read_word(&(WebSocketClientStringTable[index]))); diff --git a/WebSocketClient.h b/WebSocketClient.h index 7ecd8f2..06368c0 100644 --- a/WebSocketClient.h +++ b/WebSocketClient.h @@ -35,6 +35,7 @@ #ifdef WIFLY #include "WiFlyClient.h" +#include #else #include #endif @@ -45,6 +46,12 @@ class WebSocketClient { public: +#ifdef WIFLY + WebSocketClient(WiFlySerial &WiFly); +#else + WebSocketClient(); +#endif + typedef void (*DataArrivedDelegate)(WebSocketClient client, String data); bool connect(char hostname[], char path[] = "/", int port = 80); bool connected(); @@ -58,10 +65,10 @@ class WebSocketClient { String getStringTableItem(int index); void sendHandshake(char hostname[], char path[]); -#ifdef WIFLY - WiFlyClient _client; -#else +#ifndef WIFLY EthernetClient _client; +#else + WiFlyClient _client; #endif DataArrivedDelegate _dataArrivedDelegate; diff --git a/WiFlyClient.cpp b/WiFlyClient.cpp index 0423b10..04448f9 100644 --- a/WiFlyClient.cpp +++ b/WiFlyClient.cpp @@ -1,44 +1,42 @@ #include "WiFlyClient.h" #include -WiFlyClient::WiFlyClient(WiFlySerial *WiFly) { - _WiFly = WiFly; +WiFlyClient::WiFlyClient(WiFlySerial &WiFly) : _WiFly(WiFly) { + } -bool WiFlyClient::connect(const char hostname[], int port = 80) { - _hostname = hostname; - _port = port; +bool WiFlyClient::connect(const char hostname[], int port) { return _WiFly.openConnection(hostname, port); } -void print(const char *str) { - _WiFly.uart.print(str); +void WiFlyClient::print(const char &s) { + _WiFly.uart.print(s); } -void print(const String *str) { - _WiFly.uart.print(str); +void WiFlyClient::print(const String &s) { + _WiFly.uart.print(s); } -void println(const String *str) { - _WiFly.uart.println(str); +void WiFlyClient::println(const String &s) { + _WiFly.uart.println(s); } -void println() { +void WiFlyClient::println() { _WiFly.uart.println("\n"); } -int read() { +int WiFlyClient::read() { return _WiFly.uart.read(); } -int available() { +int WiFlyClient::available() { return _WiFly.uart.available(); } -bool connected() { +bool WiFlyClient::connected() { return _WiFly.isConnected(); } -void stop() { +void WiFlyClient::stop() { _WiFly.closeConnection(); } \ No newline at end of file diff --git a/WiFlyClient.h b/WiFlyClient.h index 2e18c62..5873bac 100644 --- a/WiFlyClient.h +++ b/WiFlyClient.h @@ -1,5 +1,5 @@ #ifndef WIFLYCLIENT_H -#define WIFLYCLIENT_H_ +#define WIFLYCLIENT_H #include "Arduino.h" @@ -14,14 +14,14 @@ class WiFlyClient { // constructor expects an initialized, and fully // connected instance of the WiFly driver. // Let the user worry about authentication and pin mapping. - WiFlyClient(WiFlySerial *WiFly); + WiFlyClient(WiFlySerial &WiFly); bool connect(const char hostname[], int port = 80); - void print(const char *str); - void print(const String *str); + void print(const char &s); + void print(const String &s); - void println(const String *str); + void println(const String &s); void println(); int read(); @@ -33,8 +33,6 @@ class WiFlyClient { private: WiFlySerial _WiFly; - int _port; - char _hostname[]; }; From f70e39851e6c0dacbbe31aaa65c579357ca56a16 Mon Sep 17 00:00:00 2001 From: Derek P Date: Mon, 13 Feb 2012 12:57:49 -0800 Subject: [PATCH 08/10] slight simplifcation of types, adding in DebugPrinting into our classes. --- WebSocketClient.cpp | 42 +++++++++++++++++++++++++++++++++++++----- WebSocketClient.h | 16 ++++++++++++++-- WiFlyClient.cpp | 31 +++++++++++++++++++++++++++++-- WiFlyClient.h | 13 ++++++++++++- 4 files changed, 92 insertions(+), 10 deletions(-) diff --git a/WebSocketClient.cpp b/WebSocketClient.cpp index ede5bb0..9531aa0 100644 --- a/WebSocketClient.cpp +++ b/WebSocketClient.cpp @@ -49,19 +49,18 @@ PROGMEM const char *WebSocketClientStringTable[] = #ifdef WIFLY WebSocketClient::WebSocketClient(WiFlySerial &WiFly) : _client(WiFly) { - } #endif String WebSocketClient::getStringTableItem(int index) { -char buffer[35]; + char buffer[35]; strcpy_P(buffer, (char*)pgm_read_word(&(WebSocketClientStringTable[index]))); return String(buffer); } -bool WebSocketClient::connect(char hostname[], char path[], int port) { +bool WebSocketClient::connect(const char *hostname, const char *path, int port) { bool result = false; if (_client.connect(hostname, port)) { @@ -103,11 +102,11 @@ void WebSocketClient::monitor () { } void WebSocketClient::setDataArrivedDelegate(DataArrivedDelegate dataArrivedDelegate) { - _dataArrivedDelegate = dataArrivedDelegate; + _dataArrivedDelegate = dataArrivedDelegate; } -void WebSocketClient::sendHandshake(char hostname[], char path[]) { +void WebSocketClient::sendHandshake(const char *hostname, const char *path) { String stringVar = getStringTableItem(0); String line1 = getStringTableItem(1); String line2 = getStringTableItem(2); @@ -171,3 +170,36 @@ void WebSocketClient::send (String data) { _client.print((char)255); } +// implementation thanks to Tom Waldock and his +// his WiFlySerial project. + +// setDebugChannel +// Conduit for debug output +// must not be a NewSoftSerial instance as incoming interrupts conflicts with outgoing data. +void WebSocketClient::setDebugChannel(Print* pChannel) { + pDebugChannel = pChannel; +#ifdef WIFLY + _client.setDebugChannel(pChannel); +#endif +} +void WebSocketClient::clearDebugChannel() { + pDebugChannel = NULL; +#ifdef WIFLY + _client.clearDebugChannel(); +#endif +} + +void WebSocketClient::DebugPrint(const char* pMessage) { + if ( pDebugChannel ) + pDebugChannel->println(pMessage); +} +void WebSocketClient::DebugPrint(const int iNumber) { + if ( pDebugChannel ) + pDebugChannel->println(iNumber); +} +void WebSocketClient::DebugPrint(const char ch) { + if ( pDebugChannel ) + pDebugChannel->print(ch); +} + + diff --git a/WebSocketClient.h b/WebSocketClient.h index 06368c0..2e08b47 100644 --- a/WebSocketClient.h +++ b/WebSocketClient.h @@ -53,17 +53,29 @@ class WebSocketClient { #endif typedef void (*DataArrivedDelegate)(WebSocketClient client, String data); - bool connect(char hostname[], char path[] = "/", int port = 80); + bool connect(const char *hostname, const char *path = "/", int port = 80); bool connected(); void disconnect(); void monitor(); void setDataArrivedDelegate(DataArrivedDelegate dataArrivedDelegate); void send(String data); + // debug utilities - use Serial : not NewSoftSerial as it will affect incoming stream. + // should change these to use stream << + void setDebugChannel( Print* pDebug); + Print* getDebugChannel() { return pDebugChannel; }; + void clearDebugChannel(); + void DebugPrint( const char* pMessage); + void DebugPrint( const int iNumber); + void DebugPrint( const char ch); + + private: String getStringTableItem(int index); - void sendHandshake(char hostname[], char path[]); + void sendHandshake(const char *hostname, const char *path); + Print* pDebugChannel; + #ifndef WIFLY EthernetClient _client; diff --git a/WiFlyClient.cpp b/WiFlyClient.cpp index 04448f9..dac0bab 100644 --- a/WiFlyClient.cpp +++ b/WiFlyClient.cpp @@ -5,7 +5,7 @@ WiFlyClient::WiFlyClient(WiFlySerial &WiFly) : _WiFly(WiFly) { } -bool WiFlyClient::connect(const char hostname[], int port) { +bool WiFlyClient::connect(const char *hostname, int port) { return _WiFly.openConnection(hostname, port); } @@ -39,4 +39,31 @@ bool WiFlyClient::connected() { void WiFlyClient::stop() { _WiFly.closeConnection(); -} \ No newline at end of file +} + +// implementation thanks to Tom Waldock and his +// his WiFlySerial project. + +// setDebugChannel +// Conduit for debug output +// must not be a NewSoftSerial instance as incoming interrupts conflicts with outgoing data. +void WiFlyClient::setDebugChannel(Print* pChannel) { + pDebugChannel = pChannel; +} +void WiFlyClient::clearDebugChannel() { + pDebugChannel = NULL; +} + +void WiFlyClient::DebugPrint(const char* pMessage) { + if ( pDebugChannel ) + pDebugChannel->println(pMessage); +} +void WiFlyClient::DebugPrint(const int iNumber) { + if ( pDebugChannel ) + pDebugChannel->println(iNumber); +} +void WiFlyClient::DebugPrint(const char ch) { + if ( pDebugChannel ) + pDebugChannel->print(ch); +} + diff --git a/WiFlyClient.h b/WiFlyClient.h index 5873bac..1586da0 100644 --- a/WiFlyClient.h +++ b/WiFlyClient.h @@ -16,7 +16,7 @@ class WiFlyClient { // Let the user worry about authentication and pin mapping. WiFlyClient(WiFlySerial &WiFly); - bool connect(const char hostname[], int port = 80); + bool connect(const char *hostname, int port = 80); void print(const char &s); void print(const String &s); @@ -30,9 +30,20 @@ class WiFlyClient { bool connected(); void stop(); + // debug utilities - use Serial : not NewSoftSerial as it will affect incoming stream. + // should change these to use stream << + void setDebugChannel( Print* pDebug); + Print* getDebugChannel() { return pDebugChannel; }; + void clearDebugChannel(); + void DebugPrint( const char* pMessage); + void DebugPrint( const int iNumber); + void DebugPrint( const char ch); + private: WiFlySerial _WiFly; + Print* pDebugChannel; + }; From 0e110a4a43eb1c2500a1d5348bf605f8d340a8cf Mon Sep 17 00:00:00 2001 From: Kevin Rohling Date: Wed, 15 Feb 2012 17:33:11 -0800 Subject: [PATCH 09/10] Added support for WiFly constructors to WebSocketClient. Created an updated EchoExample to use WiFly --- WebSocketClient.cpp | 6 ++++++ WebSocketClient.h | 2 ++ WiFlyClient.cpp | 24 ++++++++++++++++++++++ WiFlyClient.h | 6 +++++- examples/EchoExample/WiFly_EchoExample.ino | 24 ++++++++++++++++++++++ 5 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 examples/EchoExample/WiFly_EchoExample.ino diff --git a/WebSocketClient.cpp b/WebSocketClient.cpp index 9531aa0..bd4f1f7 100644 --- a/WebSocketClient.cpp +++ b/WebSocketClient.cpp @@ -50,6 +50,12 @@ PROGMEM const char *WebSocketClientStringTable[] = #ifdef WIFLY WebSocketClient::WebSocketClient(WiFlySerial &WiFly) : _client(WiFly) { } + +WebSocketClient::WebSocketClient(const char *ssid, const char *password) : _client(ssid, password) { +} + +WebSocketClient::WebSocketClient(int rxPin, int txPin, const char *ssid, const char *password) : _client(rxPin, txPin, ssid, password) { +} #endif diff --git a/WebSocketClient.h b/WebSocketClient.h index 2e08b47..73bda26 100644 --- a/WebSocketClient.h +++ b/WebSocketClient.h @@ -48,6 +48,8 @@ class WebSocketClient { #ifdef WIFLY WebSocketClient(WiFlySerial &WiFly); + WebSocketClient(const char *ssid, const char *password); + WebSocketClient(int rxPin, int txPin, const char *ssid, const char *password); #else WebSocketClient(); #endif diff --git a/WiFlyClient.cpp b/WiFlyClient.cpp index dac0bab..3485e12 100644 --- a/WiFlyClient.cpp +++ b/WiFlyClient.cpp @@ -1,10 +1,34 @@ #include "WiFlyClient.h" #include +#define ARD_DEFAULT_RX_PIN 2 +#define ARD_DEFAULT_TX_PIN 3 + +WiFlyClient::WiFlyClient(int rxPin, int txPin, const char *ssid, const char *password) : _WiFly(rxPin, txPin) { + initializeWiFly(ssid, password); +} + +WiFlyClient::WiFlyClient(const char *ssid, const char *password) : _WiFly(ARD_DEFAULT_RX_PIN, ARD_DEFAULT_TX_PIN) { + initializeWiFly(ssid, password); +} + WiFlyClient::WiFlyClient(WiFlySerial &WiFly) : _WiFly(WiFly) { } + +void WiFlyClient::initializeWiFly(const char *ssid, const char *password) { + _WiFly.begin(); + if (!_WiFly.isConnected()) { + _WiFly.leave(); // restart my link + _WiFly.SendCommand("set com remote 0", "AOK"); + _WiFly.SendCommand("set opt jointmr 5000", "AOK"); + _WiFly.setSSID(ssid); + _WiFly.setPassphrase(password); + _WiFly.join(); + } +} + bool WiFlyClient::connect(const char *hostname, int port) { return _WiFly.openConnection(hostname, port); } diff --git a/WiFlyClient.h b/WiFlyClient.h index 1586da0..9b260f9 100644 --- a/WiFlyClient.h +++ b/WiFlyClient.h @@ -15,6 +15,10 @@ class WiFlyClient { // connected instance of the WiFly driver. // Let the user worry about authentication and pin mapping. WiFlyClient(WiFlySerial &WiFly); + + WiFlyClient(const char *ssid, const char *password); + + WiFlyClient(int rxPin, int txPin, const char *ssid, const char *password); bool connect(const char *hostname, int port = 80); @@ -43,7 +47,7 @@ class WiFlyClient { WiFlySerial _WiFly; Print* pDebugChannel; - + void initializeWiFly(const char *ssid, const char *password); }; diff --git a/examples/EchoExample/WiFly_EchoExample.ino b/examples/EchoExample/WiFly_EchoExample.ino new file mode 100644 index 0000000..dc28cc9 --- /dev/null +++ b/examples/EchoExample/WiFly_EchoExample.ino @@ -0,0 +1,24 @@ +#include "Arduino.h" +#include +#include +#include +#include + +byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +char server[] = "echo.websocket.org"; +WebSocketClient client("ssid", "password"); + +void setup() { + Serial.begin(9600); + client.connect(server); + client.setDataArrivedDelegate(dataArrived); + client.send("Hello World!"); +} + +void loop() { + client.monitor(); +} + +void dataArrived(WebSocketClient client, String data) { + Serial.println("Data Arrived: " + data); +} From 6c27182ffe487308264b8d0d1de04fcad154e22e Mon Sep 17 00:00:00 2001 From: Derek P Date: Fri, 17 Feb 2012 11:03:05 -0800 Subject: [PATCH 10/10] arduino 1.0 wants to make this into a folder. --- .../EchoExample/{ => WiFly_EchoExample}/WiFly_EchoExample.ino | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) rename examples/EchoExample/{ => WiFly_EchoExample}/WiFly_EchoExample.ino (79%) diff --git a/examples/EchoExample/WiFly_EchoExample.ino b/examples/EchoExample/WiFly_EchoExample/WiFly_EchoExample.ino similarity index 79% rename from examples/EchoExample/WiFly_EchoExample.ino rename to examples/EchoExample/WiFly_EchoExample/WiFly_EchoExample.ino index dc28cc9..5768026 100644 --- a/examples/EchoExample/WiFly_EchoExample.ino +++ b/examples/EchoExample/WiFly_EchoExample/WiFly_EchoExample.ino @@ -6,13 +6,15 @@ byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; char server[] = "echo.websocket.org"; -WebSocketClient client("ssid", "password"); +WebSocketClient client(2, 3, "2WIRE598", "8888986907"); void setup() { Serial.begin(9600); + client.setDebugChannel( (Print*) &Serial); client.connect(server); client.setDataArrivedDelegate(dataArrived); client.send("Hello World!"); + Serial.println("here"); } void loop() {