Skip to content

Commit 3aa0dea

Browse files
committed
add LED control example
command out all echos
1 parent d5b0364 commit 3aa0dea

File tree

4 files changed

+159
-17
lines changed

4 files changed

+159
-17
lines changed

examples/WebSocketClient/WebSocketClient.ino

+7-7
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,28 @@ void webSocketEvent(WStype_t type, uint8_t * payload, size_t lenght) {
3030
case WStype_CONNECTED:
3131
{
3232
USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
33+
34+
// send message to server when Connected
35+
webSocket.sendTXT(num, "Connected");
3336
}
3437
break;
3538
case WStype_TEXT:
3639
USE_SERIAL.printf("[WSc] get text: %s\n", payload);
3740

38-
// send data to back to Server
39-
webSocket.sendTXT(payload, lenght);
41+
// send message to server
42+
// webSocket.sendTXT("message here");
4043
break;
4144
case WStype_BIN:
4245
USE_SERIAL.printf("[WSc] get binary lenght: %u\n", lenght);
4346
hexdump(payload, lenght);
4447

45-
// echo data back to Server
46-
webSocket.sendBIN(payload, lenght);
48+
// send data to server
49+
// webSocket.sendBIN(payload, lenght);
4750
break;
4851
}
4952

5053
}
5154

52-
5355
void setup() {
5456
// USE_SERIAL.begin(921600);
5557
USE_SERIAL.begin(115200);
@@ -79,8 +81,6 @@ void setup() {
7981

8082
}
8183

82-
83-
8484
void loop() {
8585
webSocket.loop();
8686
}

examples/WebSocketServer/WebSocketServer.ino

+8-5
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,26 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
2828
{
2929
IPAddress ip = webSocket.remoteIP(num);
3030
USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
31+
32+
// send message to client
33+
webSocket.sendTXT(num, "Connected");
3134
}
3235
break;
3336
case WStype_TEXT:
3437
USE_SERIAL.printf("[%u] get Text: %s\n", num, payload);
3538

36-
// echo data back to browser
37-
webSocket.sendTXT(num, payload, lenght);
39+
// send message to client
40+
// webSocket.sendTXT(num, "message here");
3841

3942
// send data to all connected clients
40-
webSocket.broadcastTXT(payload, lenght);
43+
// webSocket.broadcastTXT("message here");
4144
break;
4245
case WStype_BIN:
4346
USE_SERIAL.printf("[%u] get binary lenght: %u\n", num, lenght);
4447
hexdump(payload, lenght);
4548

46-
// echo data back to browser
47-
webSocket.sendBIN(num, payload, lenght);
49+
// send message to client
50+
// webSocket.sendBIN(num, payload, lenght);
4851
break;
4952
}
5053

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* WebSocketServer_LEDcontrol.ino
3+
*
4+
* Created on: 26.11.2015
5+
*
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <ESP8266WiFi.h>
11+
#include <ESP8266WiFiMulti.h>
12+
#include <WebSocketsServer.h>
13+
#include <ESP8266WebServer.h>
14+
#include <ESP8266mDNS.h>
15+
#include <Hash.h>
16+
17+
#define LED_RED 15
18+
#define LED_GREEN 12
19+
#define LED_BLUE 13
20+
21+
#define USE_SERIAL Serial
22+
23+
24+
ESP8266WiFiMulti WiFiMulti;
25+
26+
ESP8266WebServer server = ESP8266WebServer(80);
27+
WebSocketsServer webSocket = WebSocketsServer(81);
28+
29+
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
30+
31+
switch(type) {
32+
case WStype_DISCONNECTED:
33+
USE_SERIAL.printf("[%u] Disconnected!\n", num);
34+
break;
35+
case WStype_CONNECTED: {
36+
IPAddress ip = webSocket.remoteIP(num);
37+
USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
38+
39+
// send message to client
40+
webSocket.sendTXT(num, "Connected");
41+
}
42+
break;
43+
case WStype_TEXT:
44+
USE_SERIAL.printf("[%u] get Text: %s\n", num, payload);
45+
46+
if(payload[0] == '#') {
47+
// we get RGB data
48+
49+
// decode rgb data
50+
uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);
51+
52+
analogWrite(LED_RED, ((rgb >> 16) & 0xFF));
53+
analogWrite(LED_GREEN, ((rgb >> 8) & 0xFF));
54+
analogWrite(LED_BLUE, ((rgb >> 0) & 0xFF));
55+
}
56+
57+
break;
58+
}
59+
60+
}
61+
62+
void setup() {
63+
USE_SERIAL.begin(921600);
64+
//USE_SERIAL.begin(115200);
65+
66+
USE_SERIAL.setDebugOutput(true);
67+
68+
USE_SERIAL.println();
69+
USE_SERIAL.println();
70+
USE_SERIAL.println();
71+
72+
for(uint8_t t = 4; t > 0; t--) {
73+
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
74+
USE_SERIAL.flush();
75+
delay(1000);
76+
}
77+
78+
pinMode(LED_RED, OUTPUT);
79+
pinMode(LED_GREEN, OUTPUT);
80+
pinMode(LED_BLUE, OUTPUT);
81+
82+
digitalWrite(LED_RED, 1);
83+
digitalWrite(LED_GREEN, 1);
84+
digitalWrite(LED_BLUE, 1);
85+
86+
WiFiMulti.addAP("SSID", "passpasspass");
87+
88+
while(WiFiMulti.run() != WL_CONNECTED) {
89+
delay(100);
90+
}
91+
92+
// start webSocket server
93+
webSocket.begin();
94+
webSocket.onEvent(webSocketEvent);
95+
96+
if(MDNS.begin("esp8266")) {
97+
Serial.println("MDNS responder started");
98+
}
99+
100+
// handle index
101+
server.on("/", []() {
102+
// send index.html
103+
server.send(200, "text/html", "<html><head><script>var connection = new WebSocket('ws://'+location.hostname+':81/', ['arduino']);connection.onopen = function () { connection.send('Connect ' + new Date()); }; connection.onerror = function (error) { console.log('WebSocket Error ', error);};connection.onmessage = function (e) { console.log('Server: ', e.data);};function sendRGB() { var r = parseInt(document.getElementById('r').value).toString(16); var g = parseInt(document.getElementById('g').value).toString(16); var b = parseInt(document.getElementById('b').value).toString(16); if(r.length < 2) { r = '0' + r; } if(g.length < 2) { g = '0' + g; } if(b.length < 2) { b = '0' + b; } var rgb = '#'+r+g+b; console.log('RGB: ' + rgb); connection.send(rgb); }</script></head><body>LED Control:<br/><br/>R: <input id=\"r\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" onchange=\"sendRGB();\" /><br/>G: <input id=\"g\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" onchange=\"sendRGB();\" /><br/>B: <input id=\"b\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" onchange=\"sendRGB();\" /><br/></body></html>");
104+
});
105+
106+
server.begin();
107+
108+
// Add service to MDNS
109+
MDNS.addService("http", "tcp", 80);
110+
MDNS.addService("ws", "tcp", 81);
111+
112+
digitalWrite(LED_RED, 0);
113+
digitalWrite(LED_GREEN, 0);
114+
digitalWrite(LED_BLUE, 0);
115+
116+
}
117+
118+
void loop() {
119+
webSocket.loop();
120+
server.handleClient();
121+
}
122+

tests/webSocket.html

+22-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
<head>
33

44
<script>
5-
var connection = new WebSocket('ws://10.11.2.2:81/test', ['arduino']);
5+
var connection = new WebSocket('ws://10.11.2.1:81/', ['arduino']);
66

77
connection.onopen = function () {
88
connection.send('Message from Browser to ESP8266 yay its Working!! ' + new Date());
99
connection.send('ping');
10+
11+
setInterval(function() {
12+
connection.send('Time: ' + new Date());
13+
}, 20);
14+
1015
};
1116

1217
connection.onerror = function (error) {
@@ -17,14 +22,26 @@
1722
console.log('Server: ', e.data);
1823
};
1924

20-
setInterval(function() {
21-
connection.send('Time: ' + new Date());
22-
}, 1000);
25+
function sendRGB() {
26+
var r = parseInt(document.getElementById('r').value).toString(16);
27+
var g = parseInt(document.getElementById('g').value).toString(16);
28+
var b = parseInt(document.getElementById('b').value).toString(16);
29+
if(r.length < 2) { r = '0' + r; }
30+
if(g.length < 2) { g = '0' + g; }
31+
if(b.length < 2) { b = '0' + b; }
32+
var rgb = '#'+r+g+b;
33+
console.log('RGB: ' + rgb);
34+
connection.send(rgb);
35+
}
2336

2437
</script>
2538

2639
</head>
2740
<body>
28-
Test Websocket.
41+
LED Control:<br/>
42+
<br/>
43+
R: <input id="r" type="range" min="0" max="255" step="1" onchange="sendRGB();" /><br/>
44+
G: <input id="g" type="range" min="0" max="255" step="1" onchange="sendRGB();" /><br/>
45+
B: <input id="b" type="range" min="0" max="255" step="1" onchange="sendRGB();" /><br/>
2946
</body>
3047
</html>

0 commit comments

Comments
 (0)