-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy path_66-slack-bot.ino
836 lines (748 loc) · 18.8 KB
/
_66-slack-bot.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
// 80MHZ NODEMCU V.1, use git version of esp8266/Arduino
// slackbot testing using https://github.com/urish/arduino-slack-bot
/**
Arduino Real-Time Slack Bot
Copyright (C) 2016, Uri Shaked.
Licensed under the MIT License
*/
/*
modified by chaeplin @ gmail.com
*/
#include <Arduino.h>
#include <TimeLib.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
// https://github.com/Links2004/arduinoWebSockets
#include <WebSocketsClient.h>
// https://github.com/bblanchon/ArduinoJson
#include <ArduinoJson.h>
// https://github.com/chaeplin/lgwhisen
#include <lgWhisen.h>
// https://github.com/markszabo/IRremoteESP8266
#include <IRremoteESP8266.h>
#include <ESP8266mDNS.h>
#include <ArduinoOTA.h>
#include <WiFiUdp.h>
#include <Average.h>
//for LED status
#include <Ticker.h>
Ticker ticker;
extern "C" {
#include "user_interface.h"
}
#include "/usr/local/src/ap_setting.h"
#include "/usr/local/src/slack_setting.h"
const char* api_fingerprint = "AB F0 5B A9 1A E0 AE 5F CE 32 2E 7C 66 67 49 EC DD 6D 6A 38";
//#define SLACK_BOT_TOKEN "put-your-slack-token-here"
//#define SLACK_CHANNEL "xxxxxx"
//#define SLACK_USER "xxxxxxx"
//#define SLACK_TEAM "xxxxxx"
//#define WIFI_SSID "wifi-name"
//#define WIFI_PASSWORD "wifi-password"
//#definr OTA_PASSWORD "ota-password"
//
IPAddress mqtt_server = MQTT_SERVER;
WiFiClient wifiClient;
WebSocketsClient webSocket;
WiFiUDP udp;
long nextCmdId = 1;
bool connected = false;
// AC
#define IR_RX_PIN 14
#define IR_TX_PIN 4
#define AC_CONF_TYPE 1
#define AC_CONF_HEATING 0
#define AC_CONF_ON_MIN 30
#define AC_CONF_OFF_MIN 20
volatile struct
{
uint8_t ac_mode;
uint8_t ac_temp;
uint8_t ac_flow;
bool haveData;
bool timermode;
bool timerfirst;
unsigned long intervalon; // ms
unsigned long intervaloff; // ms
unsigned long timerMillis;
unsigned long nextTimercheck;
} ir_data;
// mqtt
void ICACHE_RAM_ATTR callback(char* intopic, byte* inpayload, unsigned int length);
const char* hellotopic = "HELLO";
const char* willTopic = "clients/nodemcu";
const char* willMessage = "0";
const char* substopic = "esp8266/arduino/solar";
String clientName;
PubSubClient mqttclient(mqtt_server, 1883, callback, wifiClient);
// Timelib
unsigned int localPort = 12390;
const int timeZone = 9;
// temp received by mqtt
Average<float> ave(20);
// ir
IRrecv irrecv(IR_RX_PIN);
lgWhisen lgWhisen;
unsigned long lastPing = 0;
long lastReconnectAttempt = 0;
time_t prevDisplay = 0;
void tick()
{
//toggle state
int state = digitalRead(BUILTIN_LED); // get the current state of GPIO1 pin
digitalWrite(BUILTIN_LED, !state); // set pin to the opposite state
}
boolean reconnect() {
if (!mqttclient.connected())
{
if (mqttclient.connect((char*) clientName.c_str(), willTopic, 0, true, willMessage))
{
mqttclient.publish(willTopic, "1", true);
mqttclient.loop();
mqttclient.publish(hellotopic, "hello again 1 from nodemcu ");
mqttclient.loop();
mqttclient.subscribe(substopic);
mqttclient.loop();
}
}
return mqttclient.connected();
}
void parseMqttMsg(String receivedpayload, String receivedtopic)
{
char json[] = "{\"DS18B20\":28.00,\"FreeHeap\":32384,\"RSSI\":-74,\"millis\":2413775}";
receivedpayload.toCharArray(json, 200);
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
if (!root.success()) {
return;
}
if (root.containsKey("DS18B20"))
{
ave.push(root["DS18B20"]);
}
}
void callback(char* intopic, byte* inpayload, unsigned int length)
{
String receivedtopic = intopic;
String receivedpayload ;
for (int i = 0; i < length; i++) {
receivedpayload += (char)inpayload[i];
}
Serial.printf("[MQTT] payload: %s\n", receivedpayload.c_str());
if (length <= 200)
{
parseMqttMsg(receivedpayload, receivedtopic);
}
}
void chane_ac_temp_flow()
{
if (ir_data.ac_mode == 1)
{
lgWhisen.setTemp(ir_data.ac_temp);
lgWhisen.setFlow(ir_data.ac_flow);
irrecv.disableIRIn();
lgWhisen.activate();
delay(5);
irrecv.enableIRIn();
}
}
/**
Sends a ping message to Slack. Call this function immediately after establishing
the WebSocket connection, and then every 5 seconds to keep the connection alive.
*/
void sendPing()
{
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["type"] = "ping";
root["id"] = nextCmdId++;
String json;
root.printTo(json);
webSocket.sendTXT(json);
}
void sendHello()
{
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["type"] = "message";
root["id"] = nextCmdId++;
root["channel"] = SLACK_CHANNEL;
root["text"] = "nodemcu started";
String json;
root.printTo(json);
webSocket.sendTXT(json);
}
void sendCheck()
{
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["type"] = "message";
root["id"] = nextCmdId++;
root["channel"] = SLACK_CHANNEL;
String msg = "ac status : ";
if (ir_data.timermode)
{
int timeremain = ((ir_data.nextTimercheck - (millis() - ir_data.timerMillis)) / 1000 ) / 60;
if (ir_data.ac_mode == 0)
{
msg += ":timer_clock::black_square_for_stop:, next change in ";
msg += timeremain;
msg += " min";
}
else
{
msg += ":timer_clock::arrows_counterclockwise:, next change in ";
msg += timeremain;
msg += " min";
}
}
else
{
if (ir_data.ac_mode == 0)
{
msg += ":black_square_for_stop:";
}
else
{
msg += ":arrows_counterclockwise:";
}
}
msg += "\nac :thermometer: set : ";
msg += ir_data.ac_temp;
msg += "\nac flow set : ";
msg += ir_data.ac_flow;
msg += "\ncurr :thermometer: : ";
msg += ave.mean();
root["text"] = msg.c_str();
String json;
root.printTo(json);
webSocket.sendTXT(json);
}
void sendHelp()
{
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["type"] = "message";
root["id"] = nextCmdId++;
root["channel"] = SLACK_CHANNEL;
String msg = "on -> ac on\n";
msg += "off -> ac off\n";
msg += "ton -> timer start with ac on\n";
msg += "[18 ~ 30] -> temperature set\n";
if (AC_CONF_TYPE == 0)
{
msg += "[0 ~ 2] -> flow set(low/mid/high)\n";
}
else
{
msg += "[0 ~ 3] -> flow set(low/mid/high/change)\n";
}
msg += "check -> report ac status";
root["text"] = msg.c_str();
String json;
root.printTo(json);
webSocket.sendTXT(json);
}
void processSlackMessage(String receivedpayload)
{
char json[] = "{\"type\":\"message\",\"user\":\"XX0990XX\",\"text\":\"XX0990XX\",\"team\":\"XX0990XX\",\"user_team\":\"XX0990XX\",\"user_profile\":{\"avatar_hash\":\"g9c0531af70a\",\"image_72\":\"https://secure.gravatar.com/avatar/9c0531af70ad0dbed618e9f4c2198145.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0019-72.png\",\"first_name\":null,\"real_name\":\"\",\"name\":\"XX0990XX\"},\"channel\":\"XX0990XX\",\"ts\":\"1466954463.000275\"}";
receivedpayload.toCharArray(json, 1024);
StaticJsonBuffer<1024> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
if (!root.success()) {
Serial.println("root failed");
return;
}
if (root.containsKey("text"))
{
const char* text = root["text"].asString();
Serial.printf("[Processing] text: %s\n", text);
if (String(text) == "help")
{
sendHelp();
return;
}
else if (String(text) == "check")
{
Serial.println("->check");
sendCheck();
return;
}
else if (String(text) == "on")
{
Serial.println("->on");
ir_data.ac_mode = 1;
ir_data.timermode = false;
ir_data.haveData = true;
return;
}
else if (String(text) == "off")
{
Serial.println("->off");
ir_data.ac_mode = 0;
ir_data.timermode = false;
ir_data.haveData = true;
return;
}
else if (String(text) == "ton")
{
Serial.println("->ton");
ir_data.ac_mode = 0;
ir_data.timermode = true;
ir_data.timerfirst = false;
return;
}
else if (String(text) == "0")
{
Serial.println("->flow 0");
ir_data.ac_flow = 0;
chane_ac_temp_flow();
sendCheck();
return;
}
else
{
if (String(text).length() == 2)
{
uint8_t num = atoi(text);
if (num >= 18 && num <= 30)
{
Serial.print("->temp : ");
Serial.println(num);
ir_data.ac_temp = num;
chane_ac_temp_flow();
sendCheck();
return;
}
}
if (String(text).length() == 1)
{
uint8_t num = atoi(text);
if (num >= 1 && num <= 3)
{
if (AC_CONF_TYPE == 0 && num == 3)
{
return;
}
else
{
Serial.print("->flow : ");
Serial.println(num);
ir_data.ac_flow = num;
chane_ac_temp_flow();
sendCheck();
return;
}
}
}
}
}
}
/**
Called on each web socket event. Handles disconnection, and also
incoming messages from slack.
*/
void ICACHE_RAM_ATTR webSocketEvent(WStype_t type, uint8_t *payload, size_t len)
{
switch (type) {
case WStype_DISCONNECTED:
Serial.printf("[WebSocket] Disconnected :-( \n");
connected = false;
ticker.attach(0.3, tick);
break;
case WStype_CONNECTED:
Serial.printf("[WebSocket] Connected to: %s\n", payload);
sendPing();
webSocket.loop();
sendHello();
webSocket.loop();
ticker.attach(0.6, tick);
break;
case WStype_TEXT:
Serial.printf("[WebSocket] Message: %s\n", payload);
String receivedpayload;
for (int i = 0; i < len; i++)
{
receivedpayload += (char)payload[i];
}
if (receivedpayload.startsWith("{\"type\":\"message"))
{
if (receivedpayload.indexOf(SLACK_CHANNEL) != -1 &&
receivedpayload.indexOf(SLACK_USER) != -1 &&
receivedpayload.indexOf(SLACK_TEAM) != -1 )
{
if (len < 1024)
{
processSlackMessage(receivedpayload);
}
}
}
break;
}
}
/**
Establishes a bot connection to Slack:
1. Performs a REST call to get the WebSocket URL
2. Conencts the WebSocket
Returns true if the connection was established successfully.
*/
bool connectToSlack()
{ // Step 1: Find WebSocket address via RTM API (https://api.slack.com/methods/rtm.start)
HTTPClient http;
String uri_to_post = "/api/rtm.start?token=";
uri_to_post += SLACK_BOT_TOKEN;
http.begin("slack.com", 443, uri_to_post, api_fingerprint);
int httpCode = http.GET();
if (httpCode != HTTP_CODE_OK)
{
Serial.printf("HTTP GET failed with code %d\n", httpCode);
return false;
}
WiFiClient *client = http.getStreamPtr();
client->find("wss:\\/\\/");
String host = client->readStringUntil('\\');
String path = client->readStringUntil('"');
path.replace("\\/", "/");
// Step 2: Open WebSocket connection and register event handler
Serial.println("WebSocket Host=" + host + " Path=" + path);
webSocket.beginSSL(host, 443, path, "", "");
webSocket.onEvent(webSocketEvent);
return true;
}
void wifi_connect()
{
wifi_set_phy_mode(PHY_MODE_11N);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
WiFi.hostname("esp-slackbot");
int Attempt = 1;
Serial.println("[WIFI] connecting...");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(". ");
if ( Attempt % 30 == 0 ) {
Serial.println();
}
delay(100);
Attempt++;
if (Attempt == 300)
{
Serial.println();
Serial.println("[WIFI] -----> Could not connect to WIFI");
delay(200);
ESP.restart();
}
}
Serial.println();
Serial.println("[WIFI] ===> WiFi connected");
Serial.print("[WIFI] ------> IP address: ");
Serial.println(WiFi.localIP());
}
void ArduinoOTA_config()
{
//OTA
// Port defaults to 8266
ArduinoOTA.setPort(8266);
ArduinoOTA.setHostname("esp-slackbot");
ArduinoOTA.setPassword(OTA_PASSWORD);
ArduinoOTA.onStart([]()
{
//sendUdpSyslog("ArduinoOTA Start");
});
ArduinoOTA.onEnd([]()
{
//sendUdpSyslog("ArduinoOTA End");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total)
{
//syslogPayload = "Progress: ";
//syslogPayload += (progress / (total / 100));
//sendUdpSyslog(syslogPayload);
});
ArduinoOTA.onError([](ota_error_t error)
{
//ESP.restart();
if (error == OTA_AUTH_ERROR) abort();
else if (error == OTA_BEGIN_ERROR) abort();
else if (error == OTA_CONNECT_ERROR) abort();
else if (error == OTA_RECEIVE_ERROR) abort();
else if (error == OTA_END_ERROR) abort();
});
ArduinoOTA.begin();
}
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println("Starting....... ");
Serial.setDebugOutput(false);
pinMode(LED_BUILTIN, OUTPUT);
ticker.attach(0.5, tick);
// ac
ir_data.ac_mode = 0;
ir_data.ac_temp = 27;
ir_data.ac_flow = 1;
ir_data.haveData = false;
ir_data.timermode = false;
ir_data.timerfirst = false;
ir_data.intervalon = (AC_CONF_ON_MIN * 60 * 1000); // ms
ir_data.intervaloff = (AC_CONF_OFF_MIN * 60 * 1000); // min
ir_data.timerMillis = millis();;
ir_data.nextTimercheck = ir_data.intervalon;
lgWhisen.setActype(AC_CONF_TYPE);
lgWhisen.setHeating(AC_CONF_HEATING);
lgWhisen.setTemp(ir_data.ac_temp); // 18 ~ 30
lgWhisen.setFlow(ir_data.ac_flow); // 0 : low, 1 : mid, 2 : high, if setActype == 1, 3 : change
lgWhisen.setIrpin(IR_TX_PIN); // ir tx pin
irrecv.enableIRIn();
// wifi connect
wifi_connect();
ticker.attach(0.2, tick);
clientName = "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
clientName += "-";
clientName += String(micros() & 0xff, 16);
configTime(9 * 3600, 0, "pool.ntp.org", "time.nist.gov");
// ntp update
udp.begin(localPort);
if (timeStatus() == timeNotSet)
{
Serial.println("get ntp time");
setSyncProvider(getNtpTime);
delay(500);
}
// OTA
ArduinoOTA_config();
// mqtt connect
reconnect();
}
void irrecv_config()
{
// ac ir rx
decode_results results;
if (irrecv.decode(&results))
{
if (lgWhisen.decode(&results))
{
if (lgWhisen.get_ir_mode() != 0)
{
ir_data.ac_mode = 1;
ir_data.timermode = false;
}
else
{
ir_data.ac_mode = 0;
ir_data.timermode = false;
}
if (lgWhisen.get_ir_temperature() != 255)
{
ir_data.ac_temp = lgWhisen.get_ir_temperature();
}
if (lgWhisen.get_ir_flow() != 255)
{
ir_data.ac_flow = lgWhisen.get_ir_flow();
}
sendCheck();
}
irrecv.enableIRIn();
}
}
void webSocket_config()
{
// slack
webSocket.loop();
if (connected) {
// Send ping every 5 seconds, to keep the connection alive
if (millis() - lastPing > 5000)
{
sendPing();
webSocket.loop();
lastPing = millis();
}
}
else
{ // Try to connect / reconnect to slack
connected = connectToSlack();
if (!connected)
{
delay(500);
}
}
}
void irtxchange_config()
{
// ac change, ir tx
if (ir_data.haveData)
{
lgWhisen.setTemp(ir_data.ac_temp);
lgWhisen.setFlow(ir_data.ac_flow);
switch (ir_data.ac_mode)
{ // ac power down
case 0:
Serial.println("IR -----> AC Power Down");
irrecv.disableIRIn();
lgWhisen.power_down();
delay(5);
irrecv.enableIRIn();
break;
// ac on
case 1:
Serial.println("IR -----> AC Power On");
irrecv.disableIRIn();
lgWhisen.activate();
delay(5);
irrecv.enableIRIn();
break;
default:
break;
}
sendCheck();
ir_data.haveData = false;
}
}
void irtxtimer_config()
{
// ac timer
if (ir_data.timermode)
{
if (((millis() - ir_data.timerMillis) > ir_data.nextTimercheck) || !ir_data.timerfirst)
{
if (ir_data.ac_mode == 0)
{
ir_data.ac_mode = 1;
ir_data.nextTimercheck = ir_data.intervalon;
}
else
{
ir_data.ac_mode = 0;
ir_data.nextTimercheck = ir_data.intervaloff;
}
ir_data.haveData = true;
ir_data.timerfirst = true;
ir_data.timerMillis = millis();
}
}
}
/**
Sends a ping every 5 seconds, and handles reconnections
*/
void loop()
{
if (WiFi.status() == WL_CONNECTED)
{
if (now() != prevDisplay)
{
prevDisplay = now();
if (timeStatus() == timeSet)
{
digitalClockDisplay();
}
}
webSocket_config();
if (!mqttclient.connected())
{
unsigned long now = millis();
if (now - lastReconnectAttempt > 100)
{
lastReconnectAttempt = now;
if (reconnect())
{
lastReconnectAttempt = 0;
}
}
}
else
{
irrecv_config();
irtxchange_config();
irtxtimer_config();
mqttclient.loop();
}
ArduinoOTA.handle();
}
else
{
wifi_connect();
setSyncProvider(getNtpTime);
}
}
//----------------
String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i)
{
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}
/*-------- NTP code ----------*/
const int NTP_PACKET_SIZE = 48;
byte packetBuffer[NTP_PACKET_SIZE];
time_t getNtpTime()
{
while (udp.parsePacket() > 0) ;
sendNTPpacket(mqtt_server);
uint32_t beginWait = millis();
while (millis() - beginWait < 2500)
{
int size = udp.parsePacket();
if (size >= NTP_PACKET_SIZE)
{
udp.read(packetBuffer, NTP_PACKET_SIZE);
unsigned long secsSince1900;
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
return 0;
}
void sendNTPpacket(IPAddress & address)
{
memset(packetBuffer, 0, NTP_PACKET_SIZE);
packetBuffer[0] = 0b11100011;
packetBuffer[1] = 0;
packetBuffer[2] = 6;
packetBuffer[3] = 0xEC;
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
udp.beginPacket(address, 123);
udp.write(packetBuffer, NTP_PACKET_SIZE);
udp.endPacket();
}
void digitalClockDisplay()
{
Serial.print("[TIME] ");
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.print(" ave.mean(): ");
Serial.print(ave.mean());
Serial.print(" ave.stddev(): ");
Serial.println(ave.stddev());
}
void printDigits(int digits)
{
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
// end