From 1c56734360e6d464aa0e21910f9ead0ad954ded9 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Tue, 28 Jun 2016 16:54:40 -0400 Subject: [PATCH] Add new SimpleWebSocket example --- examples/SimpleWebSocket/SimpleWebSocket.ino | 80 +++++++++++++++++++ .../{DweetGet => SimpleWebSocket}/config.h | 1 + 2 files changed, 81 insertions(+) create mode 100644 examples/SimpleWebSocket/SimpleWebSocket.ino rename examples/{DweetGet => SimpleWebSocket}/config.h (99%) diff --git a/examples/SimpleWebSocket/SimpleWebSocket.ino b/examples/SimpleWebSocket/SimpleWebSocket.ino new file mode 100644 index 0000000..ccd97b7 --- /dev/null +++ b/examples/SimpleWebSocket/SimpleWebSocket.ino @@ -0,0 +1,80 @@ +/* + Simple WebSocket client for ArduinoHttpClient library + Connects to the WebSocket server, and sends a hello + message every 5 seconds + + note: WiFi SSID and password are stored in config.h file. + If it is not present, add a new tab, call it "config.h" + and add the following variables: + char ssid[] = "ssid"; // your network SSID (name) + char pass[] = "password"; // your network password + + created 28 Jun 2016 + by Sandeep Mistry + + this example is in the public domain +*/ +#include +#include +#include "config.h" + +char serverAddress[] = "echo.websocket.org"; // server address +int port = 80; + +WiFiClient wifi; +WebSocketClient client = WebSocketClient(wifi, serverAddress, port); +int status = WL_IDLE_STATUS; +int count = 0; + +void setup() { + Serial.begin(9600); + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to Network named: "); + Serial.println(ssid); // print the network name (SSID); + + // Connect to WPA/WPA2 network: + status = WiFi.begin(ssid, pass); + } + + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); +} + +void loop() { + Serial.println("starting WebSocket client"); + client.begin(); + + while (client.connected()) { + Serial.print("Sending hello "); + Serial.println(count); + + // send a hello # + client.beginMessage(TYPE_TEXT); + client.print("hello "); + client.print(count); + client.endMessage(); + + // increment count for next message + count++; + + // check if a message is available to be received + int messageSize = client.parseMessage(); + + if (messageSize > 0) { + Serial.println("Received a message:"); + Serial.println(client.readString()); + } + + // wait 5 seconds + delay(5000); + } + + Serial.println("disconnected"); +} + diff --git a/examples/DweetGet/config.h b/examples/SimpleWebSocket/config.h similarity index 99% rename from examples/DweetGet/config.h rename to examples/SimpleWebSocket/config.h index c263766..7765359 100644 --- a/examples/DweetGet/config.h +++ b/examples/SimpleWebSocket/config.h @@ -1,2 +1,3 @@ char ssid[] = "ssid"; // your network SSID (name) char pass[] = "password"; // your network password +