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