void plotly::plot(String x, float y){ jsonStart(len_(x)+len_(y)); print_(x); jsonMiddle(); print_(y); jsonEnd(); }
void plotly::plot(char *x, int y){ jsonStart(len_(x)+len_(y)); print_(x); jsonMiddle(); print_(y); jsonEnd(); }
void plotly::plot(unsigned long x, int y){ jsonStart(len_(x)+len_(y)); print_(x); jsonMiddle(); print_(y); jsonEnd(); }
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]; my_dtostrf(y,2,3,s_); jsonStart(len_(x)+len_(s_)-1); print_(x); jsonMiddle(); print_(y); jsonEnd(token); }
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: plot.ly\r\n")); print_(F("User-Agent: SparkCore/0.0.1\r\n")); print_(F("Content-Type: application/x-www-form-urlencoded\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!"; int asgCnt = 0; // asg stands for All Streams Go char url[] = "\"url\": \"http://plot.ly/~"; char fid[4]; int fidCnt = 0; int urlCnt = 0; int usernameCnt = 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() && (!proceed || client.available())) { 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::begin(unsigned long maxpoints){ /* * 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...")); } #define WEBSITE "plot.ly" uint32_t ip = 0; // Try looking up the website's IP address while (ip == 0) { if (! cc3000.getHostByName(WEBSITE, &ip)) { if(LOG_LEVEL < 4) Serial.println(F("... Couldn't resolve Plotly's IP address! Get in touch with [email protected]")); } delay(500); } client = cc3000.connectTCP(ip, 80); while ( !client.connected() ) { if(LOG_LEVEL < 4){ Serial.println(F("... Couldn\'t connect to plotly's REST servers... trying again!")); } delay(1000); } } 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 = 202 + len_(username_) + len_(maxpoints) + len_(filename_); print_(contentLength); /* contentLength = * 44 // first part of querystring below * + len_(username) // upper bound on username length * + 5 // &key= * + 10 // api_key length * + 66 // &args=[... * + 10 // stream_token length * + 16 // "\", \"maxpoints\": " * + len_(maxpoints) * + 49 // }}]&kwargs={\"fileopt\": \"overwrite\", \"filename\": \" * + len_(filename) * + 1 // closing "} *------ * 202 + len_(username) + len_(maxpoints) + len_(filename) */ print_(F("\r\n\r\n")); print_(F("version=0.2&origin=plot&platform=arduino&un=")); print_(username_); print_(F("&key=")); print_(api_key_); print_(F("&args=[{\"y\": [], \"x\": [], \"type\": \"scatter\", \"stream\": {\"token\": \"")); print_(stream_token_); print_(F("\", \"maxpoints\": ")); print_(maxpoints); print_(F("}}]&kwargs={\"fileopt\": \"overwrite\", \"filename\": \"")); print_(filename_); 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!"; 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 < 3) Serial.println(F("... Sent message, 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.close(); } if(!proceed && LOG_LEVEL < 4){ Serial.print(F("... Error initializing stream, aborting. Try again or get in touch with Chris at [email protected]")); return; } if(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("")); } } /* * Start request to stream servers */ if(LOG_LEVEL < 3) Serial.println(F("... Connecting to plotly's streaming servers...")); #define STREAM_SERVER "stream.plot.ly" uint32_t stream_ip = 0; // Try looking up the website's IP address while (stream_ip == 0) { if (! cc3000.getHostByName(STREAM_SERVER, &stream_ip)) { if(LOG_LEVEL < 4) Serial.println(F("Couldn't resolve!")); } } client = cc3000.connectTCP(stream_ip, 80); while ( !client.connected() ) { if(LOG_LEVEL < 4) Serial.println(F("... Couldn\'t connect to servers.... trying again!")); delay(1000); } 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: 127.0.0.1\r\n")); print_(F("User-Agent: Python\r\n")); print_(F("Transfer-Encoding: chunked\r\n")); print_(F("Connection: close\r\n")); print_(F("plotly-streamtoken: ")); print_(stream_token_); print_(F("\r\nplotly-convertTimestamp: America/Montreal")); print_(F("\r\n\r\n")); if(LOG_LEVEL < 3) Serial.println(F("... Done initializing, ready to stream!")); }
void plotly::print_(unsigned long s){ if(VERBOSE){ Serial.print(s); } if(!DRY_RUN) { client.print(s); } nChar_ += len_(s); }