forked from krvarma/Plotly_SparkCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotly_spark.cpp
339 lines (316 loc) · 10.5 KB
/
plotly_spark.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
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
#include "plotly_spark.h"
plotly::plotly(char *username, char *api_key, char* stream_tokens[], char *filename, int nTraces)
{
log_level = 2; // 0 = Debugging, 1 = Informational, 2 = Status, 3 = Errors, 4 = Quiet (// Serial Off)
dry_run = false;
username_ = username;
api_key_ = api_key;
stream_tokens_ = stream_tokens;
filename_ = filename;
nTraces_ = nTraces;
maxpoints = 30;
fibonacci_ = 1;
world_readable = true;
convertTimestamp = true;
timezone = "America/Montreal";
fileopt = "overwrite";
}
bool plotly::init(){
//
// Validate a stream with a REST post to plotly
//
if(dry_run && log_level < 3){
Serial.println(F("... This is a dry run, we are not connecting to plotly's servers..."));
}
else if(log_level < 3) {
Serial.println(F("... Attempting to connect to plotly's REST servers"));
}
while ( !client.connect("plot.ly", 80) ) {
if(log_level < 4){
Serial.println(F("... Couldn\'t connect to plotly's REST servers... trying again!"));
}
fibonacci_ += fibonacci_;
delay(min(fibonacci_, 60000));
}
fibonacci_ = 1;
if(log_level < 3){} Serial.println(F("... Connected to plotly's REST servers"));
if(log_level < 3){} Serial.println(F("... Sending HTTP Post to plotly"));
print_(F("POST /clientresp HTTP/1.1\r\n"));
print_(F("Host: 107.21.214.199\r\n"));
print_(F("User-Agent: Arduino/0.5.1\r\n"));
print_(F("Content-Length: "));
int contentLength = 126 + len_(username_) + len_(fileopt) + nTraces_*(87+len_(maxpoints)) + (nTraces_-1)*2 + len_(filename_);
if(world_readable){
contentLength += 4;
} else{
contentLength += 5;
}
print_(contentLength);
// contentLength =
// 44 // first part of querystring below
// + len_(username) // upper bound on username length
// + 5 // &key=
// + 10 // api_key length
// + 7 // &args=[...
// + nTraces*(87+len(maxpoints)) // len({\"y\": [], \"x\": [], \"type\": \"scatter\", \"stream\": {\"token\": \") + 10 + len(\", "maxpoints": )+len(maxpoints)+len(}})
// + (nTraces-1)*2 // ", " in between trace objects
// + 22 // ]&kwargs={\"fileopt\": \"
// + len_(fileopt)
// + 16 // \", \"filename\": \"
// + len_(filename)
// + 21 // ", "world_readable":
// + 4 if world_readable, 5 otherwise
// + 1 // closing }
//------
// 126 + len_(username) + len_(fileopt) + nTraces*(86+len(maxpoints)) + (nTraces-1)*2 + len_(filename)
//
// Terminate headers with new lines
print_(F("\r\n\r\n"));
// Start printing querystring body
print_(F("version=2.3&origin=plot&platform=arduino&un="));
print_(username_);
print_(F("&key="));
print_(api_key_);
print_(F("&args=["));
// print a trace for each token supplied
for(int i=0; i<nTraces_; i++){
print_(F("{\"y\": [], \"x\": [], \"type\": \"scatter\", \"stream\": {\"token\": \""));
print_(stream_tokens_[i]);
print_(F("\", \"maxpoints\": "));
print_(maxpoints);
print_(F("}}"));
if(nTraces_ > 1 && i != nTraces_-1){
print_(F(", "));
}
}
print_(F("]&kwargs={\"fileopt\": \""));
print_(fileopt);
print_(F("\", \"filename\": \""));
print_(filename_);
print_(F("\", \"world_readable\": "));
if(world_readable){
print_("true");
} else{
print_("false");
}
print_(F("}"));
// final newline to terminate the POST
print_(F("\r\n"));
//
// Wait for a response
// Parse the response for the "All Streams Go!" and proceed to streaming
// if we find it
//
char allStreamsGo[] = "All Streams Go!";
char error[] = "\"error\": \"";
int asgCnt = 0; // asg stands for All Streams Go
char url[] = "\"url\": \"http://107.21.214.199/~";
char fid[4];
int fidCnt = 0;
int urlCnt = 0;
int usernameCnt = 0;
int urlLower = 0;
int urlUpper = 0;
bool proceed = false;
bool fidMatched = false;
if(log_level < 2){
Serial.println(F("... Sent message, waiting for plotly's response..."));
}
if(!dry_run){
while(client.connected()){
if(client.available()){
char c = client.read();
if(log_level < 2) Serial.print(c);
//
// Attempt to read the "All streams go" msg if it exists
// by comparing characters as they roll in
//
if(asgCnt == len_(allStreamsGo) && !proceed){
proceed = true;
}
else if(allStreamsGo[asgCnt]==c){
asgCnt += 1;
} else if(asgCnt > 0){
// reset counter
asgCnt = 0;
}
//
// Extract the last bit of the URL from the response
// The url is in the form http://107.21.214.199/~USERNAME/FID
// We'll character-count up through char url[] and through username_, then start
// filling in characters into fid
//
if(log_level < 3){
if(url[urlCnt]==c && urlCnt < len_(url)){
urlCnt += 1;
} else if(urlCnt > 0 && urlCnt < len_(url)){
// Reset counter
urlCnt = 0;
}
if(urlCnt == len_(url) && fidCnt < 4 && !fidMatched){
// We've counted through the url, start counting through the username
if(usernameCnt < len_(username_)+2){
usernameCnt += 1;
} else {
// the url ends with "
if(c != '"'){
fid[fidCnt] = c;
fidCnt += 1;
} else if(fidCnt>0){
fidMatched = true;
}
}
}
}
}
}
client.stop();
}
if(!dry_run && !proceed && log_level < 4){
Serial.println(F("... Error initializing stream, aborting. Try again or get in touch with Chris at [email protected]"));
}
if(!dry_run && proceed && log_level < 3){
Serial.println(F("... A-ok from plotly, All Streams Go!"));
if(fidMatched){
Serial.print(F("... View your streaming plot here: https://plot.ly/~"));
Serial.print(username_);
Serial.print(F("/"));
for(int i=0; i<fidCnt; i++){
Serial.print(fid[i]);
}
Serial.println(F(""));
}
}
return proceed;
}
void plotly::openStream() {
//
// Start request to stream servers
//
if(log_level < 3){} Serial.println(F("... Connecting to plotly's streaming servers..."));
char server[] = "arduino.plot.ly";
int port = 80;
while ( !client.connect(server, port) ) {
if(log_level < 4) Serial.println(F("... Couldn\'t connect to servers... trying again!"));
fibonacci_ += fibonacci_;
delay(min(fibonacci_, 60000));
}
fibonacci_ = 1;
if(log_level < 3){} Serial.println(F("... Connected to plotly's streaming servers\n... Initializing stream"));
print_(F("POST / HTTP/1.1\r\n"));
print_(F("Host: arduino.plot.ly\r\n"));
print_(F("User-Agent: Python\r\n"));
print_(F("Transfer-Encoding: chunked\r\n"));
print_(F("Connection: close\r\n"));
if(convertTimestamp){
print_(F("plotly-convertTimestamp: \""));
print_(timezone);
print_(F("\"\r\n"));
}
print_(F("\r\n"));
if(log_level < 3){} Serial.println(F("... Done initializing, ready to stream!"));
}
void plotly::closeStream(){
print_(F("0\r\n\r\n"));
client.stop();
}
void plotly::reconnectStream(){
while(!client.connected()){
if(log_level<4) Serial.println(F("... Disconnected from streaming servers"));
closeStream();
openStream();
}
}
void plotly::jsonStart(int i){
// Print the length of the message in hex:
// 15 char for the json that wraps the data: {"x": , "y": }\n
// + 23 char for the token: , "token": "abcdefghij"
// = 38
if(log_level<2) Serial.print(i+44, HEX);
if(!dry_run) client.print(i+44, HEX);
print_("\r\n{\"x\": ");
}
void plotly::jsonMiddle(){
print_(", \"y\": ");
}
void plotly::jsonEnd(char *token){
print_(", \"streamtoken\": \"");
print_(token);
print_("\"}\n\r\n");
}
int plotly::len_(int i){
// int range: -32,768 to 32,767
if(i > 9999) return 5;
else if(i > 999) return 4;
else if(i > 99) return 3;
else if(i > 9) return 2;
else if(i > -1) return 1;
else if(i > -10) return 2;
else if(i > -100) return 3;
else if(i > -1000) return 4;
else if(i > -10000) return 5;
else return 6;
}
int plotly::len_(unsigned long i){
// max length of unsigned long: 4294967295
if(i > 999999999) return 10;
else if(i > 99999999) return 9;
else if(i > 9999999) return 8;
else if(i > 999999) return 7;
else if(i > 99999) return 6;
else if(i > 9999) return 5;
else if(i > 999) return 4;
else if(i > 99) return 3;
else if(i > 9) return 2;
else return 1;
}
int plotly::len_(char *i){
return strlen(i);
}
void plotly::plot(unsigned long x, int y, char *token){
reconnectStream();
jsonStart(len_(x)+len_(y));
print_(x);
jsonMiddle();
print_(y);
jsonEnd(token);
}
void plotly::plot(unsigned long x, float y, char *token){
reconnectStream();
char s_[15];
dtostrf(y,2,3,s_);
jsonStart(len_(x)+len_(s_)-1);
print_(x);
jsonMiddle();
print_(y);
jsonEnd(token);
}
void plotly::print_(int d){
if(log_level < 2) Serial.print(d);
if(!dry_run) client.print(d);
}
void plotly::print_(unsigned long d){
if(log_level < 2) Serial.print(d);
if(!dry_run) client.print(d);
}
void plotly::print_(float d){
if(log_level < 2) Serial.print(d);
if(!dry_run) client.print(d);
}
void plotly::print_(char *d){
if(log_level < 2) Serial.print(d);
if(!dry_run) client.print(d);
}
/*
void plotly::print_(const __FlashStringHelper* d){
if(log_level < 2) Serial.print(d);
if(!dry_run) client.print(d);
}
*/
//convert double to ascii
char *plotly::dtostrf (double val, signed char width, unsigned char prec, char *sout) {
char fmt[20];
sprintf(fmt, "%%%d.%df", width, prec);
sprintf(sout, fmt, val);
return sout;
}