|
1 /* |
|
2 * WebSocketServer.ino |
|
3 * |
|
4 * Created on: 22.05.2015 |
|
5 * |
|
6 */ |
|
7 |
|
8 #include <Arduino.h> |
|
9 |
|
10 #include <ESP8266WiFi.h> |
|
11 #include <ESP8266WiFiMulti.h> |
|
12 #include <WebSocketsServer.h> |
|
13 #include <Hash.h> |
|
14 |
|
15 ESP8266WiFiMulti WiFiMulti; |
|
16 |
|
17 WebSocketsServer webSocket = WebSocketsServer(81); |
|
18 |
|
19 #define USE_SERIAL Serial1 |
|
20 |
|
21 void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) { |
|
22 |
|
23 switch(type) { |
|
24 case WStype_DISCONNECTED: |
|
25 USE_SERIAL.printf("[%u] Disconnected!\n", num); |
|
26 break; |
|
27 case WStype_CONNECTED: |
|
28 { |
|
29 IPAddress ip = webSocket.remoteIP(num); |
|
30 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"); |
|
34 } |
|
35 break; |
|
36 case WStype_TEXT: |
|
37 USE_SERIAL.printf("[%u] get Text: %s\n", num, payload); |
|
38 |
|
39 // send message to client |
|
40 // webSocket.sendTXT(num, "message here"); |
|
41 |
|
42 // send data to all connected clients |
|
43 // webSocket.broadcastTXT("message here"); |
|
44 break; |
|
45 case WStype_BIN: |
|
46 USE_SERIAL.printf("[%u] get binary lenght: %u\n", num, lenght); |
|
47 hexdump(payload, lenght); |
|
48 |
|
49 // send message to client |
|
50 // webSocket.sendBIN(num, payload, lenght); |
|
51 break; |
|
52 } |
|
53 |
|
54 } |
|
55 |
|
56 void setup() { |
|
57 // USE_SERIAL.begin(921600); |
|
58 USE_SERIAL.begin(115200); |
|
59 |
|
60 //Serial.setDebugOutput(true); |
|
61 USE_SERIAL.setDebugOutput(true); |
|
62 |
|
63 USE_SERIAL.println(); |
|
64 USE_SERIAL.println(); |
|
65 USE_SERIAL.println(); |
|
66 |
|
67 for(uint8_t t = 4; t > 0; t--) { |
|
68 USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t); |
|
69 USE_SERIAL.flush(); |
|
70 delay(1000); |
|
71 } |
|
72 |
|
73 WiFiMulti.addAP("SSID", "passpasspass"); |
|
74 |
|
75 while(WiFiMulti.run() != WL_CONNECTED) { |
|
76 delay(100); |
|
77 } |
|
78 |
|
79 webSocket.begin(); |
|
80 webSocket.onEvent(webSocketEvent); |
|
81 } |
|
82 |
|
83 void loop() { |
|
84 webSocket.loop(); |
|
85 } |
|
86 |