Skip to content

Commit 24eb13c

Browse files
committed
add WebSocketClientAVR example
1 parent 83ac64b commit 24eb13c

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* WebSocketClientAVR.ino
3+
*
4+
* Created on: 10.12.2015
5+
*
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <SPI.h>
11+
#include <Ethernet.h>
12+
13+
#include <WebSocketsClient.h>
14+
15+
16+
17+
// Enter a MAC address for your controller below.
18+
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
19+
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
20+
21+
// Set the static IP address to use if the DHCP fails to assign
22+
IPAddress ip(192, 168, 0, 177);
23+
24+
WebSocketsClient webSocket;
25+
26+
27+
28+
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
29+
30+
31+
switch(type) {
32+
case WStype_DISCONNECTED:
33+
Serial.println("[WSc] Disconnected!\n");
34+
break;
35+
case WStype_CONNECTED:
36+
{
37+
Serial.print("[WSc] Connected to url: ");
38+
Serial.println((char *)payload);
39+
// send message to server when Connected
40+
webSocket.sendTXT("Connected");
41+
}
42+
break;
43+
case WStype_TEXT:
44+
Serial.print("[WSc] get text: ");
45+
Serial.println((char *)payload);
46+
// send message to server
47+
// webSocket.sendTXT("message here");
48+
break;
49+
case WStype_BIN:
50+
Serial.print("[WSc] get binary length: ");
51+
Serial.println(length);
52+
// hexdump(payload, length);
53+
54+
// send data to server
55+
// webSocket.sendBIN(payload, length);
56+
break;
57+
}
58+
59+
}
60+
61+
void setup()
62+
{
63+
// Open serial communications and wait for port to open:
64+
Serial.begin(115200);
65+
while (!Serial) {}
66+
67+
// start the Ethernet connection:
68+
if (Ethernet.begin(mac) == 0) {
69+
Serial.println("Failed to configure Ethernet using DHCP");
70+
// no point in carrying on, so do nothing forevermore:
71+
// try to congifure using IP address instead of DHCP:
72+
Ethernet.begin(mac, ip);
73+
}
74+
75+
webSocket.begin("192.168.0.123", 8011);
76+
webSocket.onEvent(webSocketEvent);
77+
78+
}
79+
80+
81+
void loop()
82+
{
83+
webSocket.loop();
84+
}

0 commit comments

Comments
 (0)