-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsocketIO.ino
86 lines (77 loc) · 2.59 KB
/
socketIO.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
void socketIO_Connected(const char * payload, size_t length) {
Serial.println("Socket.IO Connected!");
blinkOnConnect();
}
void socketIO_sendMac(const char * payload, size_t length) {
Serial.println("GOT MAC REQUEST");
const size_t capacity = JSON_OBJECT_SIZE(1) + 50;
DynamicJsonDocument doc(capacity);
doc["macAddress"] = myID;
String bodyReq;
serializeJson(doc, bodyReq);
Serial.println(bodyReq);
socketIO.emit("mac", bodyReq.c_str());
}
void socketIO_event(const char * payload, size_t length) {
Serial.print("got message: ");
Serial.println(payload);
}
void socketIO_msg(const char * payload, size_t length) {
Serial.println("got msg");
const size_t capacity = 2 * JSON_OBJECT_SIZE(2) + 60;
DynamicJsonDocument incomingDoc(capacity);
deserializeJson(incomingDoc, payload);
const char* recMacAddress = incomingDoc["macAddress"];
const char* data_project = incomingDoc["data"]["project"];
Serial.print("I got a message from ");
Serial.println(recMacAddress);
Serial.print("Which is of type ");
Serial.println(data_project);
if (String(data_project) == "lighttouch") {
long data_hue = incomingDoc["data"]["hue"];
Serial.print("Light touch! Hue: ");
Serial.println(data_hue);
// TODO - Run light touch
hue[REMOTELED] = (uint8_t)data_hue;
ledChanged[REMOTELED] = true;
//added to enable reset of fading mid fade
isFadingRGB[REMOTELED] = false;
fadeRGB(REMOTELED);
startLongFade(REMOTELED);
}
else if (String(data_project) == "test") {
blinkDevice();
}
}
void socketIO_sendButtonPress() {
Serial.println("button send");
const size_t capacity = 2 * JSON_OBJECT_SIZE(2);
DynamicJsonDocument doc(capacity);
doc["macAddress"] = getRemoteMacAddress(1);
JsonObject data = doc.createNestedObject("data");
data["project"] = "test";
String sender;
serializeJson(doc, sender);
socketIO.emit("msg", sender.c_str());
}
void socketIO_sendColour() {
Serial.println("colour send");
const size_t capacity = 3 * JSON_OBJECT_SIZE(2);
DynamicJsonDocument doc(capacity);
doc["macAddress"] = getRemoteMacAddress(1);
JsonObject data = doc.createNestedObject("data");
data["project"] = "lighttouch";
data["hue"] = String((int)getUserHue());
String sender;
serializeJson(doc, sender);
socketIO.emit("msg", sender.c_str());
}
void setupSocketIOEvents() {
// Setup 'on' listen events
socketIO.on("connect", socketIO_Connected);
socketIO.on("event", socketIO_event);
socketIO.on("send mac", socketIO_sendMac);
socketIO.on("msg", socketIO_msg);
socketIO.begin(host, port, path);
Serial.println("attached socketio listeners");
}