8000 add WebSocketServerAllFunctionsDemo (WIP) · blackhack/arduinoWebSockets@5636b02 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5636b02

Browse files
committed
add WebSocketServerAllFunctionsDemo (WIP)
1 parent 4b33575 commit 5636b02

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* WebSocketServerAllFunctionsDemo.ino
3+
*
4+
* Created on: 10.05.2018
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(80);
27+
WebSocketsServer webSocket = WebSocketsServer(81);
28+
29+
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
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+
USE_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\" oninput=\"sendRGB();\" /><br/>G: <input id=\"g\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/>B: <input id=\"b\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"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+
unsigned long last_10sec = 0;
119+
unsigned int counter = 0;
120+
121+
void loop() {
122+
unsigned long t = millis();
123+
webSocket.loop();
124+
server.handleClient();
125+
126+
if((t - last_10sec) > 10 * 1000) {
127+
counter++;
128+
bool ping = (counter % 2);
129+
int i = webSocket.connectedClients(ping);
130+
USE_SERIAL.printf("%d Connected websocket clients ping: %d", i, ping);
131+
last_10sec = millis();
132+
}
133+
}

0 commit comments

Comments
 (0)
0