forked from marcoschwartz/aREST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aREST.h
1164 lines (890 loc) · 27.5 KB
/
aREST.h
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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
aREST Library for Arduino
See the README file for more details.
Written in 2014 by Marco Schwartz under a GPL license.
Version 1.9.10
Changelog:
Version 1.9.10: Added support for floats & Strings for Uno (without the CC3000 chip)
Version 1.9.8: Added support for ESP8266 chip
Version 1.9.7: Added support for Arduino 1.6.2
Version 1.9.6: Added support for float variables for Arduino Mega
Version 1.9.5: Added compatibility with Arduino IDE 1.5.8
Version 1.9.4: Bug fixes & added support for configuring analog pints as digital outputs
Version 1.9.3: Added description of available variables for the /id and / routes
Version 1.9.2: Added compatibility with the Arduino WiFi library
Version 1.9.1: Added compatibility with CORS
Version 1.9: New speedup of the library (answers 2x faster in HTTP compared to version 1.8)
Version 1.8: Speedup of the library (answers 2.5x faster with the CC3000 WiFi chip)
Version 1.7.5: Reduced memory footprint of the library
Version 1.7.4: Added a function to read all analog & digital inputs at once
Version 1.7.3: Added LIGHTWEIGHT mode to only send limited data back
Version 1.7.2: Added possibility to assign a status pin connected to a LED
Version 1.7.1: Added possibility to change number of exposed variables & functions
Version 1.7: Added compatibility with the Arduino Due & Teensy 3.x
Version 1.6: Added compatibility with the Arduino Yun
Version 1.5: Size reduction, and added compatibility with Adafruit BLE
Version 1.4: Added authentification with API key
Version 1.3: Added support for the Ethernet shield
Version 1.2: Added support of Serial communications
Version 1.1: Added variables & functions support
Version 1.0: First working version of the library
*/
#ifndef aRest_h
#define aRest_h
// Include Arduino header
#include "Arduino.h"
// Using ESP8266 ?
#if defined(ESP8266)
#include "stdlib_noniso.h"
#endif
// Which board?
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(CORE_WILDFIRE) || defined(ESP8266)
#define NUMBER_ANALOG_PINS 16
#define NUMBER_DIGITAL_PINS 54
#define OUTPUT_BUFFER_SIZE 2000
#elif defined(__AVR_ATmega328P__) && !defined(ADAFRUIT_CC3000_H)
#define NUMBER_ANALOG_PINS 6
#define NUMBER_DIGITAL_PINS 14
#define OUTPUT_BUFFER_SIZE 350
#elif defined(ADAFRUIT_CC3000_H)
#define NUMBER_ANALOG_PINS 6
#define NUMBER_DIGITAL_PINS 14
#define OUTPUT_BUFFER_SIZE 275
#else
#define NUMBER_ANALOG_PINS 6
#define NUMBER_DIGITAL_PINS 14
#define OUTPUT_BUFFER_SIZE 350
#endif
// Size of name & ID
#define NAME_SIZE 20
#define ID_SIZE 10
// Debug mode
#ifndef DEBUG_MODE
#define DEBUG_MODE 0
#endif
// Use light answer mode
#ifndef LIGHTWEIGHT
#define LIGHTWEIGHT 0
#endif
// Default number of max. exposed variables
#ifndef NUMBER_VARIABLES
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(CORE_WILDFIRE) || defined(ESP8266) || !defined(ADAFRUIT_CC3000_H)
#define NUMBER_VARIABLES 10
#else
#define NUMBER_VARIABLES 5
#endif
#endif
// Default number of max. exposed functions
#ifndef NUMBER_FUNCTIONS
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(CORE_WILDFIRE) || defined(ESP8266)
#define NUMBER_FUNCTIONS 10
#else
#define NUMBER_FUNCTIONS 5
#endif
#endif
class aREST {
public:
aREST() {
command = 'u';
pin_selected = false;
status_led_pin = 255;
state = 'u';
}
// Set status LED
void set_status_led(uint8_t pin){
// Set variables
status_led_pin = pin;
// Set pin as output
pinMode(status_led_pin,OUTPUT);
}
// Glow status LED
void glow_led() {
if(status_led_pin != 255){
unsigned long i = millis();
int j = i % 4096;
if (j > 2048) { j = 4096 - j;}
analogWrite(status_led_pin,j/8);
}
}
// Send HTTP headers for Ethernet & WiFi
void send_http_headers(){
addToBuffer(F("HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: POST, GET, PUT, OPTIONS\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n"));
}
// Reset variables after a request
void reset_status() {
answer = "";
command = 'u';
pin_selected = false;
state = 'u';
arguments = "";
index = 0;
//memset(&buffer[0], 0, sizeof(buffer));
}
// Handle request with the Adafruit CC3000 WiFi library
#ifdef ADAFRUIT_CC3000_H
void handle(Adafruit_CC3000_ClientRef& client) {
if (client.available()) {
// Handle request
handle_proto(client,true,0);
// Answer
sendBuffer(client,32,20);
client.stop();
// Reset variables for the next command
reset_status();
}
}
// Handle request with the Arduino Yun
#elif defined(_YUN_CLIENT_H_)
void handle(YunClient& client) {
if (client.available()) {
// Handle request
handle_proto(client,false,0);
// Answer
sendBuffer(client,25,10);
client.stop();
// Reset variables for the next command
reset_status();
}
}
// Handle request with the Adafruit BLE board
#elif defined(_ADAFRUIT_BLE_UART_H_)
void handle(Adafruit_BLE_UART& serial) {
if (serial.available()) {
// Handle request
handle_proto(serial,false,0);
// Answer
sendBuffer(serial,100,1);
// Reset variables for the next command
reset_status();
}
}
// Handle request for the Arduino Ethernet shield
#elif defined(ethernet_h)
void handle(EthernetClient& client){
if (client.available()) {
// Handle request
handle_proto(client,true,0);
// Answer
sendBuffer(client,50,0);
client.stop();
// Reset variables for the next command
reset_status();
}
}
// Handle request for the ESP8266 chip
#elif defined(ESP8266)
void handle(WiFiClient& client){
if (client.available()) {
if (DEBUG_MODE) {Serial.println("Request received");}
// Handle request
handle_proto(client,true,0);
// Answer
sendBuffer(client,0,0);
client.stop();
// Reset variables for the next command
reset_status();
}
}
// Handle request for the Arduino WiFi shield
#elif defined(WiFi_h)
void handle(WiFiClient& client){
if (client.available()) {
if (DEBUG_MODE) {Serial.println("Request received");}
// Handle request
handle_proto(client,true,0);
// Answer
sendBuffer(client,50,1);
client.stop();
// Reset variables for the next command
reset_status();
}
}
#elif defined(CORE_TEENSY)
// Handle request on the Serial port
void handle(usb_serial_class& serial){
if (serial.available()) {
// Handle request
handle_proto(serial,false,1);
// Answer
sendBuffer(serial,25,1);
// Reset variables for the next command
reset_status();
}
}
#elif defined(__AVR_ATmega32U4__)
// Handle request on the Serial port
void handle(Serial_& serial){
if (serial.available()) {
// Handle request
handle_proto(serial,false,1);
// Answer
sendBuffer(serial,25,1);
// Reset variables for the next command
reset_status();
}
}
#else
// Handle request on the Serial port
void handle(HardwareSerial& serial){
if (serial.available()) {
// Handle request
handle_proto(serial,false,1);
// Answer
sendBuffer(serial,25,1);
// Reset variables for the next command
reset_status();
}
}
#endif
void handle(char * string) {
// Process String
handle_proto(string);
// Reset variables for the next command
reset_status();
}
void handle_proto(char * string) {
// Check if there is data available to read
for (int i = 0; i < strlen(string); i++){
char c = string[i];
answer = answer + c;
// Process data
process(c);
}
// Send command
send_command(false);
}
template <typename T>
void handle_proto(T& serial, bool headers, uint8_t read_delay)
{
// Check if there is data available to read
while (serial.available()) {
// Get the server answer
char c = serial.read();
delay(read_delay);
answer = answer + c;
//if (DEBUG_MODE) {Serial.print(c);}
// Process data
process(c);
}
// Send command
send_command(headers);
}
void process(char c){
// Check if we are receveing useful data and process it
if ((c == '/' || c == '\r') && state == 'u') {
if (DEBUG_MODE) {Serial.println(answer);}
// If the command is mode, and the pin is already selected
if (command == 'm' && pin_selected && state == 'u') {
// Get state
state = answer[0];
}
// If a digital command has been received, process the data accordingly
if (command == 'd' && pin_selected && state == 'u') {
// If it's a read command, read from the pin and send data back
if (answer[0] == 'r') {state = 'r';}
// If not, get value we want to apply to the pin
else {value = answer.toInt(); state = 'w';}
}
// If analog command has been selected, process the data accordingly
if (command == 'a' && pin_selected && state == 'u') {
// If it's a read, read from the correct pin
if (answer[0] == 'r') {state = 'r';}
// Else, write analog value
else {value = answer.toInt(); state = 'w';}
}
// If the command is already selected, get the pin
if (command != 'u' && pin_selected == false) {
// Get pin
if (answer[0] == 'A') {
pin = 14 + answer[1] - '0';
}
else {
pin = answer.toInt();
}
if (DEBUG_MODE) {
Serial.print("Selected pin: ");
Serial.println(pin);
}
pin_selected = true;
// Nothing more ?
if ((answer[1] != '/' && answer[2] != '/')
|| (answer[1] == ' ' && answer[2] == '/')
|| (answer[2] == ' ' && answer[3] == '/')) {
// Nothing more & digital ?
if (command == 'd') {
// Read all digital ?
if (answer[0] == 'a') {state = 'a';}
// Save state & end there
else {state = 'r';}
}
// Nothing more & analog ?
if (command == 'a') {
// Read all analog ?
if (answer[0] == 'a') {state = 'a';}
// Save state & end there
else {state = 'r';}
}
}
}
// Digital command received ?
if (answer.startsWith("digital")) {command = 'd';}
// Mode command received ?
if (answer.startsWith("mode")) {command = 'm';}
// Analog command received ?
if (answer.startsWith("analog")) {command = 'a';}
// Variable or function request received ?
if (command == 'u') {
// Check if variable name is in int array
for (uint8_t i = 0; i < variables_index; i++){
if(answer.startsWith(int_variables_names[i])) {
// End here
pin_selected = true;
state = 'x';
// Set state
command = 'v';
value = i;
}
}
// Check if variable name is in float array (Mega & ESP8266 only)
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(ESP8266) || defined(CORE_WILDFIRE) || !defined(ADAFRUIT_CC3000_H)
for (uint8_t i = 0; i < float_variables_index; i++){
if(answer.startsWith(float_variables_names[i])) {
// End here
pin_selected = true;
state = 'x';
// Set state
command = 'l';
value = i;
}
}
#endif
// Check if variable name is in float array (Mega & ESP8266 only)
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(ESP8266) || defined(CORE_WILDFIRE) || !defined(ADAFRUIT_CC3000_H)
for (uint8_t i = 0; i < string_variables_index; i++){
if(answer.startsWith(string_variables_names[i])) {
// End here
pin_selected = true;
state = 'x';
// Set state
command = 's';
value = i;
}
}
#endif
// Check if function name is in array
for (uint8_t i = 0; i < functions_index; i++){
if(answer.startsWith(functions_names[i])) {
// End here
pin_selected = true;
state = 'x';
// Set state
command = 'f';
value = i;
// Get command
arguments = "";
uint8_t header_length = strlen(functions_names[i]);
if (answer.substring(header_length, header_length + 1) == "?") {
uint8_t footer_start = answer.length();
if (answer.endsWith(" HTTP/"))
footer_start -= 6; // length of " HTTP/"
arguments = answer.substring(header_length + 8, footer_start);
}
}
}
// If the command is "id", return device id, name and status
if ( (answer[0] == 'i' && answer[1] == 'd') ){
// Set state
command = 'i';
// End here
pin_selected = true;
state = 'x';
}
if (answer[0] == ' '){
// Set state
command = 'r';
// End here
pin_selected = true;
state = 'x';
}
// Check the type of HTTP request
// if (answer.startsWith("GET")) {method = "GET";}
// if (answer.startsWith("POST")) {method = "POST";}
// if (answer.startsWith("PUT")) {method = "PUT";}
// if (answer.startsWith("DELETE")) {method = "DELETE";}
// if (DEBUG_MODE && method != "") {
// Serial.print("Selected method: ");
// Serial.println(method);
// }
}
answer = "";
}
}
bool send_command(bool headers) {
if (DEBUG_MODE) {
Serial.println(F("Sending command"));
Serial.print(F("Command: "));
Serial.println(command);
Serial.print(F("State: "));
Serial.println(state);
Serial.print(F("State of buffer at the start: "));
Serial.println(buffer);
}
// Start of message
if (headers && command != 'r') {send_http_headers();}
// Mode selected
if (command == 'm'){
// Send feedback to client
if (!LIGHTWEIGHT){
addToBuffer(F("{\"message\": \"Pin D"));
addToBuffer(pin);
}
// Input
if (state == 'i'){
// Set pin to Input
pinMode(pin,INPUT);
// Send feedback to client
if (!LIGHTWEIGHT){addToBuffer(F(" set to input\", "));}
}
// Output
if (state == 'o'){
// Set to Output
pinMode(pin,OUTPUT);
// Send feedback to client
if (!LIGHTWEIGHT){addToBuffer(F(" set to output\", "));}
}
}
// Digital selected
if (command == 'd') {
if (state == 'r'){
// Read from pin
value = digitalRead(pin);
// Send answer
if (LIGHTWEIGHT){addToBuffer(value);}
else {
addToBuffer(F("{\"return_value\": "));
addToBuffer(value);
addToBuffer(F(", "));
}
}
#if !defined(__AVR_ATmega32U4__) || !defined(ADAFRUIT_CC3000_H)
if (state == 'a') {
if (!LIGHTWEIGHT) {addToBuffer(F("{"));}
for (uint8_t i = 0; i < NUMBER_DIGITAL_PINS; i++) {
// Read analog value
value = digitalRead(i);
// Send feedback to client
if (LIGHTWEIGHT){
addToBuffer(value);
addToBuffer(F(","));
}
else {
addToBuffer(F("\"D"));
addToBuffer(i);
addToBuffer(F("\": "));
addToBuffer(value);
addToBuffer(F(", "));
}
}
}
#endif
if (state == 'w') {
// Apply on the pin
digitalWrite(pin,value);
// Send feedback to client
if (!LIGHTWEIGHT){
addToBuffer(F("{\"message\": \"Pin D"));
addToBuffer(pin);
addToBuffer(F(" set to "));
addToBuffer(value);
addToBuffer(F("\", "));
}
}
}
// Analog selected
if (command == 'a') {
if (state == 'r'){
// Read analog value
value = analogRead(pin);
// Send feedback to client
if (LIGHTWEIGHT){addToBuffer(value);}
else {
addToBuffer(F("{\"return_value\": "));
addToBuffer(value);
addToBuffer(F(", "));
}
}
#if !defined(__AVR_ATmega32U4__)
if (state == 'a') {
if (!LIGHTWEIGHT) {addToBuffer(F("{"));}
for (uint8_t i = 0; i < NUMBER_ANALOG_PINS; i++) {
// Read analog value
value = analogRead(i);
// Send feedback to client
if (LIGHTWEIGHT){
addToBuffer(value);
addToBuffer(F(","));
}
else {
addToBuffer(F("\"A"));
addToBuffer(i);
addToBuffer(F("\": "));
addToBuffer(value);
addToBuffer(F(", "));
}
}
}
#endif
if (state == 'w') {
// Write output value
analogWrite(pin,value);
// Send feedback to client
addToBuffer(F("{\"message\": \"Pin D"));
addToBuffer(pin);
addToBuffer(F(" set to "));
addToBuffer(value);
addToBuffer(F("\", "));
}
}
// Variable selected
if (command == 'v') {
// Send feedback to client
if (LIGHTWEIGHT){addToBuffer(*int_variables[value]);}
else {
addToBuffer(F("{\""));
addToBuffer(int_variables_names[value]);
addToBuffer(F("\": "));
addToBuffer(*int_variables[value]);
addToBuffer(F(", "));
}
}
// Float ariable selected (Mega only)
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(ESP8266) || defined(CORE_WILDFIRE) || !defined(ADAFRUIT_CC3000_H)
if (command == 'l') {
// Send feedback to client
if (LIGHTWEIGHT){addToBuffer(*float_variables[value]);}
else {
addToBuffer(F("{\""));
addToBuffer(float_variables_names[value]);
addToBuffer(F("\": "));
addToBuffer(*float_variables[value]);
addToBuffer(F(", "));
}
}
#endif
// String variable selected (Mega & ESP8266 only)
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(ESP8266) || defined(CORE_WILDFIRE) || !defined(ADAFRUIT_CC3000_H)
if (command == 's') {
// Send feedback to client
if (LIGHTWEIGHT){addToBuffer(*string_variables[value]);}
else {
addToBuffer(F("{\""));
addToBuffer(string_variables_names[value]);
addToBuffer(F("\": \""));
addToBuffer(*string_variables[value]);
addToBuffer(F("\", "));
}
}
#endif
// Function selected
if (command == 'f') {
// Execute function
uint8_t result = functions[value](arguments);
// Send feedback to client
if (!LIGHTWEIGHT) {
addToBuffer(F("{\"return_value\": "));
addToBuffer(result);
addToBuffer(F(", "));
//addToBuffer(F(", \"message\": \""));
//addToBuffer(functions_names[value]);
//addToBuffer(F(" executed\", "));
}
}
if (command == 'r') {
root_answer();
}
if (command == 'i') {
if (LIGHTWEIGHT) {addToBuffer(id);}
else {
addToBuffer(F("{"));
}
}
// End of message
if (LIGHTWEIGHT){
addToBuffer(F("\r\n"));
}
else {
if (command != 'r') {
addToBuffer(F("\"id\": \""));
addToBuffer(id);
addToBuffer(F("\", \"name\": \""));
addToBuffer(name);
addToBuffer(F("\", \"connected\": true}\r\n"));
}
}
if (DEBUG_MODE) {
Serial.print(F("State of buffer at the end: "));
Serial.println(buffer);
}
// End here
return true;
}
virtual void root_answer() {
#if defined(ADAFRUIT_CC3000_H) || defined(ESP8266) || defined(ethernet_h) || defined(WiFi_h)
addToBuffer(F("HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: POST, GET, PUT, OPTIONS\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n"));
#endif
if (LIGHTWEIGHT) {addToBuffer(id);}
else {
// Start
addToBuffer(F("{\"variables\": {"));
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(ESP8266) || defined(CORE_WILDFIRE) || !defined(ADAFRUIT_CC3000_H)
// Int variables
if (variables_index == 0 && string_variables_index == 0 && float_variables_index == 0){
addToBuffer(F(" }, "));
}
else {
if (variables_index > 0){
for (uint8_t i = 0; i < variables_index; i++){
addToBuffer(F("\""));
addToBuffer(int_variables_names[i]);
addToBuffer(F("\": "));
addToBuffer(*int_variables[i]);
addToBuffer(F(", "));
}
}
if (string_variables_index > 0){
for (uint8_t i = 0; i < string_variables_index; i++){
addToBuffer(F("\""));
addToBuffer(string_variables_names[i]);
addToBuffer(F("\": \""));
addToBuffer(*string_variables[i]);
addToBuffer(F("\", "));
}
}
if (float_variables_index > 0){
for (uint8_t i = 0; i < float_variables_index; i++){
addToBuffer(F("\""));
addToBuffer(float_variables_names[i]);
addToBuffer(F("\": "));
addToBuffer(*float_variables[i]);
addToBuffer(F(", "));
}
}
removeLastBufferChar();
removeLastBufferChar();
addToBuffer(F("}, "));
}
#else
// Int variables
if (variables_index > 0){
for (uint8_t i = 0; i < variables_index-1; i++){
addToBuffer(F("\""));
addToBuffer(int_variables_names[i]);
addToBuffer(F("\": "));
addToBuffer(*int_variables[i]);
addToBuffer(F(", "));
}
// End
addToBuffer(F("\""));
addToBuffer(int_variables_names[variables_index-1]);
addToBuffer(F("\": "));
addToBuffer(*int_variables[variables_index-1]);
addToBuffer(F("}, "));
}
else {
addToBuffer(F(" }, "));
}
#endif
}
// End
addToBuffer(F("\"id\": \""));
addToBuffer(id);
addToBuffer(F("\", \"name\": \""));
addToBuffer(name);
addToBuffer(F("\", \"connected\": true}\r\n"));
}
void variable(char * variable_name, int *variable){
int_variables[variables_index] = variable;
int_variables_names[variables_index] = variable_name;
variables_index++;
}
// Float variables (Mega & ESP only, or without CC3000)
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(ESP8266) || defined(CORE_WILDFIRE) || !defined(ADAFRUIT_CC3000_H)
void variable(char * variable_name, float *variable){
float_variables[float_variables_index] = variable;
float_variables_names[float_variables_index] = variable_name;
float_variables_index++;
}
#endif
// String variables (Mega & ESP only, or without CC3000)
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(ESP8266) || defined(CORE_WILDFIRE) || !defined(ADAFRUIT_CC3000_H)
void variable(char * variable_name, String *variable){
string_variables[string_variables_index] = variable;
string_variables_names[string_variables_index] = variable_name;
string_variables_index++;
}
#endif
void function(char * function_name, int (*f)(String)){
functions_names[functions_index] = function_name;
functions[functions_index] = f;
functions_index++;
}
// Set device ID
void set_id(char *device_id){
strcpy(id,device_id);
}
// Set device name
void set_name(char *device_name){
strcpy(name, device_name);
}
// Set device name
void set_name(String device_name){
device_name.toCharArray(name, NAME_SIZE);
}
// Set device ID
void set_id(String device_id){
device_id.toCharArray(id, NAME_SIZE);
}
// Remove last char from buffer
void removeLastBufferChar() {
index = index - 1;
}
// Add to output buffer
void addToBuffer(char * toAdd){
if (DEBUG_MODE) {
Serial.print(F("Added to buffer: "));
Serial.println(toAdd);
}
for (int i = 0; i < strlen(toAdd); i++){
buffer[index+i] = toAdd[i];
}
index = index + strlen(toAdd);
}
// Add to output buffer
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(ESP8266) || defined(CORE_WILDFIRE) || !defined(ADAFRUIT_CC3000_H)
void addToBuffer(String toAdd){
if (DEBUG_MODE) {