-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhub.ino
99 lines (88 loc) · 2.66 KB
/
hub.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <ESP8266WiFi.h>
#include <WebSocketsServer.h>
#include "Secrets.hpp"
WebSocketsServer webSocketData = WebSocketsServer(81);
WebSocketsServer webSocketCommand = WebSocketsServer(82);
void webSocketDataEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
Serial.printf("[%u] WebSocket disconnected", num);
Serial.println();
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocketData.remoteIP(num);
Serial.printf("[%u] WebSocket connected from %d.%d.%d.%d", num, ip[0], ip[1], ip[2], ip[3]);
Serial.println();
}
break;
case WStype_TEXT:
{
IPAddress ip = webSocketData.remoteIP(num);
Serial.printf("[%u] Received data from %d.%d.%d.%d: %s", num, ip[0], ip[1], ip[2], ip[3], payload);
Serial.println();
webSocketData.sendTXT(1, payload, length);
}
break;
}
}
void webSocketCommandEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
Serial.printf("[%u] WebSocket disconnected", num);
Serial.println();
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocketCommand.remoteIP(num);
Serial.printf("[%u] WebSocket connected from %d.%d.%d.%d", num, ip[0], ip[1], ip[2], ip[3]);
Serial.println();
}
break;
case WStype_TEXT:
{
IPAddress ip = webSocketCommand.remoteIP(num);
Serial.printf("[%u] Received command from %d.%d.%d.%d: %s", num, ip[0], ip[1], ip[2], ip[3], payload);
Serial.println();
webSocketCommand.broadcastTXT(payload);
}
break;
}
}
void setup() {
Serial.begin(115200);
setupAP();
setupWiFi();
webSocketData.begin();
webSocketCommand.begin();
webSocketData.onEvent(webSocketDataEvent);
webSocketCommand.onEvent(webSocketCommandEvent);
}
void loop() {
webSocketData.loop();
webSocketCommand.loop();
// Send random data to connected clients every 5 seconds
static unsigned long lastSendTime = 0;
if (millis() - lastSendTime > 5000) {
String command = "Random Data: " + String(random(0, 1024));
webSocketCommand.broadcastTXT(command);
lastSendTime = millis();
}
}
void setupWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setupAP() {
WiFi.softAP(apSSID, apPassword);
Serial.println("Access Point started");
Serial.print("IP Address: ");
Serial.println(WiFi.softAPIP());
}