plotly interface based on ardunio sample code
Library for plotting a simple x/y scatter chart on the plot.ly website.
See plotly_HelloWorld for sample usage.
Diff: plotly.cpp
- Revision:
- 1:d532e96fca12
- Parent:
- 0:96532c59670f
- Child:
- 2:d53d74ed68ac
--- a/plotly.cpp Wed Jul 02 08:47:09 2014 +0000 +++ b/plotly.cpp Wed Jul 02 10:13:24 2014 +0000 @@ -24,12 +24,15 @@ convertTimestamp = true; timezone = "America/Montreal"; fileopt = "overwrite"; + + initalised = false; } -plotly::~plotly() { -closeStream(); - } +plotly::~plotly() +{ + closeStream(); +} bool plotly::init() { @@ -43,16 +46,21 @@ fprintf(stderr,"... Attempting to connect to plotly's REST servers\n"); } - while ( !socket.connect(plotlyURL, 80) ) { - if(log_level < 4) { + socket.set_blocking(false); + + do { + + socket.connect(plotlyURL, 80); + wait(10); + if(!(socket.is_connected()) && (log_level < 4)) { fprintf(stderr,"... Couldn\'t connect to plotly's REST servers... trying again!\n"); } - fibonacci_ += fibonacci_; - wait(10); - } - fibonacci_ = 1; - if(log_level < 3) {} fprintf(stderr,"... Connected to plotly's REST servers\n"); - if(log_level < 3) {} fprintf(stderr,"... Sending HTTP Post to plotly\n"); + } while (!(socket.is_connected())); + + if(log_level < 3) fprintf(stderr,"... Connected to plotly's REST servers\n"); + + if(log_level < 3) fprintf(stderr,"... Sending HTTP Post to plotly\n"); + print_("POST /clientresp HTTP/1.1\r\n"); print_("Host: 107.21.214.199\r\n"); print_("User-Agent: Arduino/0.5.1\r\n"); @@ -123,15 +131,12 @@ // 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; char c; @@ -142,7 +147,11 @@ if(!dry_run) { while(!proceed) { - uint32_t dataIn = socket.receive_all(rxBuffer,127); + int32_t dataIn = socket.receive(rxBuffer,127); + if (dataIn < 0) { + if(log_level < 3) fprintf(stderr,"error reading network socket\n"); + break; + } if(dataIn > 0) { rxBuffer[dataIn]=0; @@ -216,11 +225,15 @@ fprintf(stderr,"\n"); } } + initalised = proceed || dry_run; return proceed; } void plotly::openStream() { + + if (!initalised) + return; // // Start request to stream servers // @@ -247,7 +260,7 @@ } print_("\r\n\r\n"); - if(log_level < 3) {} fprintf(stderr,"... Done initializing, ready to stream!\n"); + if(log_level < 3) fprintf(stderr,"... Done initializing, ready to stream!\n"); } void plotly::closeStream() @@ -255,8 +268,12 @@ print_("0\r\n\r\n"); socket.close(); } + void plotly::reconnectStream() { + if (!initalised) + return; + while(!socket.is_connected()) { if(log_level<4) fprintf(stderr,"... Disconnected from streaming servers\n"); closeStream(); @@ -339,34 +356,85 @@ jsonEnd(token); } -void plotly::print_(int d) +bool plotly::print_(int d) { - uint32_t len = snprintf(txBuffer,128,"%d",d); - if(log_level < 2) fprintf(stderr,"%s",txBuffer); - if(!dry_run) socket.send_all(txBuffer,len); // skip the trailing 0 -} -void plotly::print_(unsigned long d) -{ - uint32_t len = snprintf(txBuffer,128,"%lu",d); - if(log_level < 2) fprintf(stderr,"%s",txBuffer); - if(!dry_run) socket.send_all(txBuffer,len); // skip the trailing 0 + int32_t len = snprintf(txBuffer,128,"%d",d); + if(log_level < 2) + fprintf(stderr,"%s",txBuffer); + if(!dry_run) { + int32_t sent = socket.send(txBuffer,len); + if (sent == len) + return true; + else { + fprintf(stderr,"\nTX failed to send _%s_ Sent %d of %d bytes\n",txBuffer,sent,len); + return false; + } + } else + return true; + } -void plotly::print_(float d) +bool plotly::print_(unsigned long d) { - uint32_t len = snprintf(txBuffer,128,"%f",d); - if(log_level < 2) fprintf(stderr,"%s",txBuffer); - if(!dry_run) socket.send_all(txBuffer,len); // skip the trailing 0 + int32_t len = snprintf(txBuffer,128,"%lu",d); + if(log_level < 2) + fprintf(stderr,"%s",txBuffer); + if(!dry_run) { + int32_t sent = socket.send(txBuffer,len); + if (sent == len) + return true; + else { + fprintf(stderr,"\nTX failed to send _%s_ Sent %d of %d bytes\n",txBuffer,sent,len); + return false; + } + } else + return true; } -void plotly::print_(char *d) +bool plotly::print_(float d) { - uint32_t len = snprintf(txBuffer,128,"%s",d); - if(log_level < 2) fprintf(stderr,"%s",txBuffer); - if(!dry_run) socket.send_all(txBuffer,len); // skip the trailing 0 + int32_t len = snprintf(txBuffer,128,"%f",d); + if(log_level < 2) + fprintf(stderr,"%s",txBuffer); + if(!dry_run) { + int32_t sent = socket.send(txBuffer,len); + if (sent == len) + return true; + else { + fprintf(stderr,"\nTX failed to send _%s_ Sent %d of %d bytes\n",txBuffer,sent,len); + return false; + } + } else + return true; +} +bool plotly::print_(char *d) +{ + int32_t len = snprintf(txBuffer,128,"%s",d); + if(log_level < 2) + fprintf(stderr,"%s",txBuffer); + if(!dry_run) { + int32_t sent = socket.send(txBuffer,len); + if (sent == len) + return true; + else { + fprintf(stderr,"\nTX failed to send _%s_ Sent %d of %d bytes\n",txBuffer,sent,len); + return false; + } + } else + return true; } -void plotly::printHex_(uint16_t d) +bool plotly::printHex_(uint16_t d) { - uint32_t len = snprintf(txBuffer,128,"%X",d); - if(log_level < 2) fprintf(stderr,"%s",txBuffer); - if(!dry_run) socket.send_all(txBuffer,len); // skip the trailing 0 + int32_t len = snprintf(txBuffer,128,"%X",d); + if(log_level < 2) + fprintf(stderr,"%s",txBuffer); + if(!dry_run) { + int32_t sent = socket.send(txBuffer,len); + if (sent == len) + return true; + else { + fprintf(stderr,"\nTX failed to send _%s_ Sent %d of %d bytes\n",txBuffer,sent,len); + return false; + } + } else + return true; }