-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPolargraphArduinoGIOT.ino
135 lines (114 loc) · 3.98 KB
/
PolargraphArduinoGIOT.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
/*
* UT R.A.S. Demobots IoT Polargraph
*/
#include <Arduino.h>
#include "src/iot/backoff.h"
#include "src/iot/esp8266_wifi.h"
#include "src/polargraph/polargraph.h"
#include "src/iot/json_parse.h"
#define UPDATE_MS 4000
#define CONFIG_TIMEOUT_MS (1000 * 8)
long next_update = 0;
String lastConfigTimestamp = "";
int last_conf = 0; //get most recent
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
setupWifi();
pinMode(LED_BUILTIN, OUTPUT);
setupPolargraph();
next_update = millis();
}
void loop() {
if (loopPolargraph()) { //draw a line between two points if available/ enabled
if (backoff() && (millis() > next_update)) {
//sendTelemetry();
updateStateConfig();
next_update = millis() + UPDATE_MS;
}
}
}
void updateStateConfig() {
String state = getState();
Serial.println(String("state: ") + state);
sendState(state);
String conf = getConfig(CONFIG_TIMEOUT_MS, last_conf);
Serial.println(String("config: " + conf));
//Serial.println(String("config: " + conf.substring(0,74) + " ..."));
updateConfig(conf);
}
String getState() {
/* { 'x_position': 0,
'y_position': 0,
'left_length': 0,
'right_length': 0,
'drawing': true,
'buffer_size': 0 }
*/
String status = String ("{'x_pos': " + String(getCurrentPos().x) + ",");
status += String("'y_pos': " + String(getCurrentPos().y) + ",");
status += String("'left_len': " + String(getLeftLength()) + ",");
status += String("'right_len': " + String(getRightLength()) + ",");
status += String("'d': " + String(getIsDrawing()) + ",");
status += String("'buffer_size': " + String(getBufferSize()) + ",");
status += String("'last_conf': " + String(last_conf) + "");
status += String("}");
return status;
}
//TODO only update if its a recent timestamp
void updateConfig(String conf) {
/* {'t':'0',
'bx':'{}',
'by':'{}',
'd':'true',
'cb':'false',}
*/
if (conf != "" && conf.substring(0, 5) != "ERROR") {
int index = conf.indexOf("{");
if (index == -1) { return; }
int new_conf = conf.substring(0, index).toInt(); //returns 0 if not an integer which should be ok because no config is 0
Serial.println("new conf: " + String(new_conf) + " | last conf: " + String(last_conf));
//Only update if its a new version
if ((new_conf != 0) && (last_conf != new_conf)) {
last_conf = new_conf;
String requestedTimestamp = getFromJson(conf, "t", "-1");
if (requestedTimestamp != "-1") {
lastConfigTimestamp = requestedTimestamp;
}
boolean clear_buffer = stringToBool(getFromJson(conf, "cb", "false"));
if (clear_buffer) {
clearBuffer();
setIsDrawing(false);
//resetPos();
}
String buffer_add_x = getFromJson(conf, "bx");
//delay(100); //this is for serial buffer, can remove if not printing
String buffer_add_y = getFromJson(conf, "by");
boolean draw = stringToBool(getFromJson(conf, "d", "false"));
setIsDrawing(draw);
// Add coordinates to buffer
if (buffer_add_x != "" && buffer_add_y != "" && buffer_add_x != "{}" && buffer_add_y != "{}") {
int index_x = 1;
int index_y = 1;
int num = 0;
Serial.println("Adding new coords to buffer");
while (buffer_add_x.charAt(index_x) != '}' && buffer_add_y.charAt(index_y) != '}') {
int next_index_x = buffer_add_x.indexOf(",", index_x);
int next_x = buffer_add_x.substring(index_x, next_index_x).toInt();
index_x = next_index_x + 1;
int next_index_y = buffer_add_y.indexOf(",", index_y);
int next_y = buffer_add_y.substring(index_y, next_index_y).toInt();
index_y = next_index_y + 1;
pos new_pos = {next_x, next_y};
addToBuffer(new_pos);
num++;
if (num % 5) { Serial.print("."); }
}
Serial.println("\nAdded new coords to buffer");
}
}
}
else {
Serial.println("Conf not updated, " + conf);
}
}