-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.cpp
154 lines (107 loc) · 3.25 KB
/
ping.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "ping.h"
void PingService::setupMessaging(const char* pubkey, const char* subkey) {
PubNub.begin(pubkey, subkey);
}
void PingService::setEnabled(bool isEnabled) {
this->isEnabled = isEnabled;
}
// Checks for incoming PING requests from other devices
// params:
// pixels - NeoPixel strip on which the ping will be executed
void PingService::checkForPings(Adafruit_NeoPixel& pixels) {
if(!isEnabled)
return;
if (millis() - lastPingCheckMillis > PING_CHECK_INTERVAL) {
debugLogln("Checking for pings...");
PubSubClient* sclient = PubNub.subscribe(CHANNEL, 1);
yield();
debugLog("sclient: ");
debugLogln((unsigned long)sclient);
if (sclient != 0) {
debugLogln("sclient not 0");
char buffer[BUFFER_SIZE];
size_t bufferSize = 0;
while (sclient->wait_for_data()) {
buffer[bufferSize++] = sclient->read();
if (bufferSize >= BUFFER_SIZE - 1) {
break;
}
}
yield();
debugLog("bufferSize after read: ");
debugLogln(bufferSize);
//Cut down first char: "["
//Last 3 chars ( "]]\0" ) aren't needed for us
memmove(buffer, buffer + 1, bufferSize - 3);
//Cut down last 4 chars: "}]]\0"
buffer[bufferSize - 3] = '\0';
debugLog("buffer: ");
debugLogln(buffer);
yield();
//Only care about JSON with at least 1 key-value pair
// strlen("{\"\":}") == 5
if (bufferSize > 5) {
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(buffer);
yield();
debugLog("json: ");
root.printTo(Serial);
debugLogln();
yield();
if (root["id"] != ESP.getChipId()) {
debugLogln("Ping received!");
executePing(pixels);
}
}
sclient->stop();
}
lastPingCheckMillis = millis();
debugLogln("End checking for pings!");
}
}
// Make PING request to other devices
void PingService::pingOthers() {
if (!isEnabled)
return;
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
yield();
root["id"] = ESP.getChipId();
String out;
root.printTo(out);
debugLog("json: ");
debugLogln(out);
yield();
PubNub_BASE_CLIENT *client = PubNub.publish(CHANNEL, out.c_str(), 3);
yield();
if (client != 0) {
client->stop();
}
debugLogln("Ping others!");
}
// Make lighting effect for PING request
// params:
// pixels - NeoPixel strip in which the effect will be executed
void PingService::executePing(Adafruit_NeoPixel& pixels) {
// Only needed for first LED, because only static modes
// won't update the NeoPixel strip instantly
uint32_t pastColor = pixels.getPixelColor(0);
for (uint8_t block = 0; block < 3; ++block) {
for (uint8_t light = 0; light < 2; ++light) {
for (uint8_t i = 0; i < pixels.numPixels(); ++i) {
pixels.setPixelColor(i, 255, 0, 0);
}
pixels.show();
delay(135);
pixels.clear();
pixels.show();
delay(135);
}
delay(500);
}
// Set pixels to saved color from before the ping
for (uint8_t i = 0; i < pixels.numPixels(); ++i) {
pixels.setPixelColor(i, pastColor);
}
pixels.show();
}