This repository was archived by the owner on May 17, 2019. It is now read-only.
forked from brandenhall/Arduino-Websocket
-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathWebSocketClient_Demo.ino
92 lines (69 loc) · 1.77 KB
/
WebSocketClient_Demo.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <ESP8266WiFi.h>
#include <WebSocketClient.h>
const char* ssid = "SSID HERE";
const char* password = "PASSWORD HERE";
char path[] = "/";
char host[] = "echo.websocket.org";
WebSocketClient webSocketClient;
// Use WiFiClient class to create TCP connections
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(5000);
// Connect to the websocket server
if (client.connect("echo.websocket.org", 80)) {
Serial.println("Connected");
} else {
Serial.println("Connection failed.");
while(1) {
// Hang on failure
}
}
// Handshake with the server
webSocketClient.path = path;
webSocketClient.host = host;
if (webSocketClient.handshake(client)) {
Serial.println("Handshake successful");
} else {
Serial.println("Handshake failed.");
while(1) {
// Hang on failure
}
}
}
void loop() {
String data;
if (client.connected()) {
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);
}