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.
plotly.cpp@5:fc8eefeb301b, 2014-07-28 (annotated)
- Committer:
- AndyA
- Date:
- Mon Jul 28 10:56:42 2014 +0000
- Revision:
- 5:fc8eefeb301b
- Parent:
- 4:33006c37c633
- Child:
- 7:9409a72ab6c0
Added plot (float,float)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AndyA | 0:96532c59670f | 1 | #include "plotly.h" |
AndyA | 0:96532c59670f | 2 | #include "mbed.h" |
AndyA | 0:96532c59670f | 3 | |
AndyA | 3:967be3d46701 | 4 | #define plotlyURL "plot.ly" |
AndyA | 0:96532c59670f | 5 | #define dataURL "arduino.plot.ly" |
AndyA | 0:96532c59670f | 6 | |
AndyA | 4:33006c37c633 | 7 | plotly::plotly(char *username, char *api_key, char* stream_token, char *filename) |
AndyA | 0:96532c59670f | 8 | { |
AndyA | 4:33006c37c633 | 9 | log_level = 3; // 0 = Debugging, 1 = Informational, 2 = Status, 3 = Errors, 4 = Quiet (// Serial Off) |
AndyA | 0:96532c59670f | 10 | dry_run = false; |
AndyA | 0:96532c59670f | 11 | username_ = username; |
AndyA | 0:96532c59670f | 12 | api_key_ = api_key; |
AndyA | 4:33006c37c633 | 13 | stream_token_ = stream_token; |
AndyA | 0:96532c59670f | 14 | filename_ = filename; |
AndyA | 0:96532c59670f | 15 | maxpoints = 30; |
AndyA | 0:96532c59670f | 16 | world_readable = true; |
AndyA | 4:33006c37c633 | 17 | convertTimestamp = false; |
AndyA | 0:96532c59670f | 18 | timezone = "America/Montreal"; |
AndyA | 0:96532c59670f | 19 | fileopt = "overwrite"; |
AndyA | 1:d532e96fca12 | 20 | |
AndyA | 2:d53d74ed68ac | 21 | socket = NULL; |
AndyA | 4:33006c37c633 | 22 | |
AndyA | 1:d532e96fca12 | 23 | initalised = false; |
AndyA | 0:96532c59670f | 24 | } |
AndyA | 0:96532c59670f | 25 | |
AndyA | 0:96532c59670f | 26 | |
AndyA | 1:d532e96fca12 | 27 | plotly::~plotly() |
AndyA | 1:d532e96fca12 | 28 | { |
AndyA | 1:d532e96fca12 | 29 | closeStream(); |
AndyA | 4:33006c37c633 | 30 | |
AndyA | 1:d532e96fca12 | 31 | } |
AndyA | 0:96532c59670f | 32 | |
AndyA | 4:33006c37c633 | 33 | |
AndyA | 0:96532c59670f | 34 | bool plotly::init() |
AndyA | 0:96532c59670f | 35 | { |
AndyA | 0:96532c59670f | 36 | |
AndyA | 0:96532c59670f | 37 | // |
AndyA | 4:33006c37c633 | 38 | // Create plot with a REST post to plotly |
AndyA | 4:33006c37c633 | 39 | // See the clientresp section of https://plot.ly/rest/ for details |
AndyA | 0:96532c59670f | 40 | // |
AndyA | 0:96532c59670f | 41 | if(dry_run && log_level < 3) { |
AndyA | 0:96532c59670f | 42 | fprintf(stderr,"... This is a dry run, we are not connecting to plotly's servers...\n"); |
AndyA | 0:96532c59670f | 43 | } else if(log_level < 3) { |
AndyA | 0:96532c59670f | 44 | fprintf(stderr,"... Attempting to connect to plotly's REST servers\n"); |
AndyA | 0:96532c59670f | 45 | } |
AndyA | 0:96532c59670f | 46 | |
AndyA | 2:d53d74ed68ac | 47 | if (!dry_run) { |
AndyA | 4:33006c37c633 | 48 | int pause = 1; |
AndyA | 2:d53d74ed68ac | 49 | socket = new TCPSocketConnection(); |
AndyA | 2:d53d74ed68ac | 50 | while (socket->connect(plotlyURL, 80) < 0) { |
AndyA | 4:33006c37c633 | 51 | if (pause > 30) { |
AndyA | 4:33006c37c633 | 52 | fprintf(stderr,"Failed to connect. :-(\n"); |
AndyA | 4:33006c37c633 | 53 | delete socket; |
AndyA | 4:33006c37c633 | 54 | return false; |
AndyA | 4:33006c37c633 | 55 | } |
AndyA | 0:96532c59670f | 56 | fprintf(stderr,"... Couldn\'t connect to plotly's REST servers... trying again!\n"); |
AndyA | 4:33006c37c633 | 57 | wait(pause); |
AndyA | 4:33006c37c633 | 58 | pause *= 2; |
AndyA | 0:96532c59670f | 59 | } |
AndyA | 2:d53d74ed68ac | 60 | } |
AndyA | 1:d532e96fca12 | 61 | |
AndyA | 1:d532e96fca12 | 62 | if(log_level < 3) fprintf(stderr,"... Connected to plotly's REST servers\n"); |
AndyA | 2:d53d74ed68ac | 63 | |
AndyA | 1:d532e96fca12 | 64 | if(log_level < 3) fprintf(stderr,"... Sending HTTP Post to plotly\n"); |
AndyA | 2:d53d74ed68ac | 65 | |
AndyA | 0:96532c59670f | 66 | print_("POST /clientresp HTTP/1.1\r\n"); |
AndyA | 0:96532c59670f | 67 | print_("Host: 107.21.214.199\r\n"); |
AndyA | 3:967be3d46701 | 68 | print_("User-Agent: Arduino/0.5.1\r\n"); |
AndyA | 2:d53d74ed68ac | 69 | print_("plotly-streamtoken: "); |
AndyA | 4:33006c37c633 | 70 | print_(stream_token_); |
AndyA | 4:33006c37c633 | 71 | printNetTerminator_(); |
AndyA | 0:96532c59670f | 72 | |
AndyA | 0:96532c59670f | 73 | print_("Content-Length: "); |
AndyA | 2:d53d74ed68ac | 74 | |
AndyA | 4:33006c37c633 | 75 | // calculate the length of the packet payload and store it in the big buffer. |
AndyA | 3:967be3d46701 | 76 | int lineLen = snprintf(buffer,k_bufferSize,"version=2.3&origin=plot&platform=arduino&un=%s&key=%s&args=[",username_,api_key_); |
AndyA | 4:33006c37c633 | 77 | lineLen += snprintf((buffer+lineLen),k_bufferSize-lineLen,"{\"y\": [], \"x\": [], \"type\": \"scatter\", \"stream\": {\"token\": \"%s\", \"maxpoints\": %d}}",stream_token_,maxpoints); |
AndyA | 2:d53d74ed68ac | 78 | lineLen += snprintf((buffer+lineLen),k_bufferSize-lineLen,"]&kwargs={\"fileopt\": \"%s\", \"filename\": \"%s\", \"world_readable\": %s}",fileopt,filename_,world_readable?"true":"false"); |
AndyA | 2:d53d74ed68ac | 79 | |
AndyA | 4:33006c37c633 | 80 | print_(lineLen); |
AndyA | 2:d53d74ed68ac | 81 | |
AndyA | 4:33006c37c633 | 82 | printNetTerminator_(); |
AndyA | 4:33006c37c633 | 83 | printNetTerminator_(); |
AndyA | 4:33006c37c633 | 84 | |
AndyA | 2:d53d74ed68ac | 85 | sendFormatedText(buffer,lineLen); |
AndyA | 4:33006c37c633 | 86 | |
AndyA | 4:33006c37c633 | 87 | printNetTerminator_(); |
AndyA | 0:96532c59670f | 88 | |
AndyA | 0:96532c59670f | 89 | // |
AndyA | 0:96532c59670f | 90 | // Wait for a response |
AndyA | 0:96532c59670f | 91 | // Parse the response for the "All Streams Go!" and proceed to streaming |
AndyA | 0:96532c59670f | 92 | // if we find it |
AndyA | 0:96532c59670f | 93 | // |
AndyA | 2:d53d74ed68ac | 94 | |
AndyA | 0:96532c59670f | 95 | char allStreamsGo[] = "All Streams Go!"; |
AndyA | 0:96532c59670f | 96 | int asgCnt = 0; // asg stands for All Streams Go |
AndyA | 0:96532c59670f | 97 | char url[] = "\"url\": \"http://107.21.214.199/~"; |
AndyA | 0:96532c59670f | 98 | char fid[4]; |
AndyA | 0:96532c59670f | 99 | int fidCnt = 0; |
AndyA | 0:96532c59670f | 100 | int urlCnt = 0; |
AndyA | 0:96532c59670f | 101 | int usernameCnt = 0; |
AndyA | 0:96532c59670f | 102 | bool proceed = false; |
AndyA | 0:96532c59670f | 103 | bool fidMatched = false; |
AndyA | 0:96532c59670f | 104 | char c; |
AndyA | 0:96532c59670f | 105 | |
AndyA | 0:96532c59670f | 106 | if(log_level < 2) { |
AndyA | 0:96532c59670f | 107 | fprintf(stderr,"... Sent message, waiting for plotly's response...\n"); |
AndyA | 0:96532c59670f | 108 | } |
AndyA | 0:96532c59670f | 109 | |
AndyA | 0:96532c59670f | 110 | if(!dry_run) { |
AndyA | 0:96532c59670f | 111 | while(!proceed) { |
AndyA | 2:d53d74ed68ac | 112 | int32_t dataIn = socket->receive(buffer,k_bufferSize -1); |
AndyA | 1:d532e96fca12 | 113 | if (dataIn < 0) { |
AndyA | 1:d532e96fca12 | 114 | if(log_level < 3) fprintf(stderr,"error reading network socket\n"); |
AndyA | 1:d532e96fca12 | 115 | break; |
AndyA | 1:d532e96fca12 | 116 | } |
AndyA | 0:96532c59670f | 117 | if(dataIn > 0) { |
AndyA | 2:d53d74ed68ac | 118 | buffer[dataIn]=0; |
AndyA | 0:96532c59670f | 119 | |
AndyA | 2:d53d74ed68ac | 120 | if(log_level < 2) fprintf(stderr,buffer); |
AndyA | 0:96532c59670f | 121 | |
AndyA | 0:96532c59670f | 122 | for (int i = 0; i<dataIn; i++) { |
AndyA | 2:d53d74ed68ac | 123 | c = buffer[i]; |
AndyA | 0:96532c59670f | 124 | // |
AndyA | 0:96532c59670f | 125 | // Attempt to read the "All streams go" msg if it exists |
AndyA | 0:96532c59670f | 126 | // by comparing characters as they roll in |
AndyA | 0:96532c59670f | 127 | // |
AndyA | 0:96532c59670f | 128 | |
AndyA | 4:33006c37c633 | 129 | if(asgCnt == strlen(allStreamsGo) && !proceed) { |
AndyA | 0:96532c59670f | 130 | proceed = true; |
AndyA | 0:96532c59670f | 131 | } else if(allStreamsGo[asgCnt]==c) { |
AndyA | 0:96532c59670f | 132 | asgCnt += 1; |
AndyA | 0:96532c59670f | 133 | } else if(asgCnt > 0) { |
AndyA | 0:96532c59670f | 134 | // reset counter |
AndyA | 0:96532c59670f | 135 | asgCnt = 0; |
AndyA | 0:96532c59670f | 136 | } |
AndyA | 0:96532c59670f | 137 | |
AndyA | 0:96532c59670f | 138 | // |
AndyA | 0:96532c59670f | 139 | // Extract the last bit of the URL from the response |
AndyA | 0:96532c59670f | 140 | // The url is in the form http://107.21.214.199/~USERNAME/FID |
AndyA | 0:96532c59670f | 141 | // We'll character-count up through char url[] and through username_, then start |
AndyA | 0:96532c59670f | 142 | // filling in characters into fid |
AndyA | 0:96532c59670f | 143 | // |
AndyA | 0:96532c59670f | 144 | |
AndyA | 0:96532c59670f | 145 | if(log_level < 3) { |
AndyA | 4:33006c37c633 | 146 | if(url[urlCnt]==c && urlCnt < strlen(url)) { |
AndyA | 0:96532c59670f | 147 | urlCnt += 1; |
AndyA | 4:33006c37c633 | 148 | } else if(urlCnt > 0 && urlCnt < strlen(url)) { |
AndyA | 0:96532c59670f | 149 | // Reset counter |
AndyA | 0:96532c59670f | 150 | urlCnt = 0; |
AndyA | 0:96532c59670f | 151 | } |
AndyA | 4:33006c37c633 | 152 | if(urlCnt == strlen(url) && fidCnt < 4 && !fidMatched) { |
AndyA | 0:96532c59670f | 153 | // We've counted through the url, start counting through the username |
AndyA | 4:33006c37c633 | 154 | if(usernameCnt < strlen(username_)+2) { |
AndyA | 0:96532c59670f | 155 | usernameCnt += 1; |
AndyA | 0:96532c59670f | 156 | } else { |
AndyA | 0:96532c59670f | 157 | // the url ends with " |
AndyA | 0:96532c59670f | 158 | if(c != '"') { |
AndyA | 0:96532c59670f | 159 | fid[fidCnt] = c; |
AndyA | 0:96532c59670f | 160 | fidCnt += 1; |
AndyA | 0:96532c59670f | 161 | } else if(fidCnt>0) { |
AndyA | 0:96532c59670f | 162 | fidMatched = true; |
AndyA | 0:96532c59670f | 163 | } |
AndyA | 0:96532c59670f | 164 | |
AndyA | 0:96532c59670f | 165 | } |
AndyA | 0:96532c59670f | 166 | } |
AndyA | 0:96532c59670f | 167 | } |
AndyA | 0:96532c59670f | 168 | } |
AndyA | 0:96532c59670f | 169 | } |
AndyA | 0:96532c59670f | 170 | wait(0.1); |
AndyA | 0:96532c59670f | 171 | } |
AndyA | 0:96532c59670f | 172 | } |
AndyA | 0:96532c59670f | 173 | |
AndyA | 0:96532c59670f | 174 | if(!dry_run && !proceed && log_level < 4) { |
AndyA | 0:96532c59670f | 175 | fprintf(stderr,"... Error initializing stream, aborting. Try again or get in touch with Chris at chris@plot.ly\n"); |
AndyA | 0:96532c59670f | 176 | } |
AndyA | 0:96532c59670f | 177 | |
AndyA | 0:96532c59670f | 178 | if(!dry_run && proceed && log_level < 3) { |
AndyA | 0:96532c59670f | 179 | fprintf(stderr,"... A-ok from plotly, All Streams Go!\n"); |
AndyA | 0:96532c59670f | 180 | if(fidMatched) { |
AndyA | 0:96532c59670f | 181 | fprintf(stderr,"... View your streaming plot here: https://plot.ly/~"); |
AndyA | 0:96532c59670f | 182 | fprintf(stderr,username_); |
AndyA | 0:96532c59670f | 183 | fprintf(stderr,"/"); |
AndyA | 0:96532c59670f | 184 | for(int i=0; i<fidCnt; i++) { |
AndyA | 4:33006c37c633 | 185 | fprintf(stderr,"%c",fid[i]); |
AndyA | 0:96532c59670f | 186 | } |
AndyA | 0:96532c59670f | 187 | fprintf(stderr,"\n"); |
AndyA | 0:96532c59670f | 188 | } |
AndyA | 0:96532c59670f | 189 | } |
AndyA | 2:d53d74ed68ac | 190 | |
AndyA | 2:d53d74ed68ac | 191 | if (proceed || dry_run) { |
AndyA | 4:33006c37c633 | 192 | initalised = true; |
AndyA | 2:d53d74ed68ac | 193 | } |
AndyA | 2:d53d74ed68ac | 194 | if (socket) { |
AndyA | 4:33006c37c633 | 195 | delete socket; |
AndyA | 4:33006c37c633 | 196 | socket=NULL; |
AndyA | 4:33006c37c633 | 197 | } |
AndyA | 2:d53d74ed68ac | 198 | return initalised; |
AndyA | 0:96532c59670f | 199 | } |
AndyA | 0:96532c59670f | 200 | |
AndyA | 4:33006c37c633 | 201 | bool plotly::openStream() |
AndyA | 0:96532c59670f | 202 | { |
AndyA | 1:d532e96fca12 | 203 | |
AndyA | 1:d532e96fca12 | 204 | if (!initalised) |
AndyA | 4:33006c37c633 | 205 | return false; |
AndyA | 4:33006c37c633 | 206 | |
AndyA | 0:96532c59670f | 207 | // |
AndyA | 0:96532c59670f | 208 | // Start request to stream servers |
AndyA | 0:96532c59670f | 209 | // |
AndyA | 2:d53d74ed68ac | 210 | |
AndyA | 2:d53d74ed68ac | 211 | if (socket) { |
AndyA | 2:d53d74ed68ac | 212 | delete socket; |
AndyA | 2:d53d74ed68ac | 213 | socket = NULL; |
AndyA | 4:33006c37c633 | 214 | } |
AndyA | 4:33006c37c633 | 215 | |
AndyA | 2:d53d74ed68ac | 216 | |
AndyA | 2:d53d74ed68ac | 217 | if(log_level < 3) fprintf(stderr,"... Connecting to plotly's streaming servers...\n"); |
AndyA | 2:d53d74ed68ac | 218 | |
AndyA | 2:d53d74ed68ac | 219 | |
AndyA | 4:33006c37c633 | 220 | if (!dry_run) { |
AndyA | 4:33006c37c633 | 221 | int pause = 1; |
AndyA | 2:d53d74ed68ac | 222 | socket = new TCPSocketConnection(); |
AndyA | 2:d53d74ed68ac | 223 | while (socket->connect(dataURL, 80) < 0) { |
AndyA | 4:33006c37c633 | 224 | if (pause > 30) { |
AndyA | 4:33006c37c633 | 225 | fprintf(stderr,"Failed to connect. :-(\n"); |
AndyA | 4:33006c37c633 | 226 | delete socket; |
AndyA | 4:33006c37c633 | 227 | return false; |
AndyA | 4:33006c37c633 | 228 | } |
AndyA | 4:33006c37c633 | 229 | fprintf(stderr,"... Couldn\'t connect to plotly's REST servers... trying again!\n"); |
AndyA | 4:33006c37c633 | 230 | wait(pause); |
AndyA | 4:33006c37c633 | 231 | pause *= 2; |
AndyA | 2:d53d74ed68ac | 232 | } |
AndyA | 0:96532c59670f | 233 | } |
AndyA | 2:d53d74ed68ac | 234 | |
AndyA | 4:33006c37c633 | 235 | |
AndyA | 2:d53d74ed68ac | 236 | if(log_level < 3) fprintf(stderr,"... Connected to plotly's streaming servers\n... Initializing stream\n"); |
AndyA | 0:96532c59670f | 237 | |
AndyA | 0:96532c59670f | 238 | print_("POST / HTTP/1.1\r\n"); |
AndyA | 3:967be3d46701 | 239 | print_("Host: arduino.plot.ly\r\n"); |
AndyA | 3:967be3d46701 | 240 | print_("User-Agent: Python\r\n"); |
AndyA | 0:96532c59670f | 241 | print_("Transfer-Encoding: chunked\r\n"); |
AndyA | 3:967be3d46701 | 242 | print_("Connection: close\r\n"); |
AndyA | 2:d53d74ed68ac | 243 | print_("plotly-streamtoken: "); |
AndyA | 4:33006c37c633 | 244 | print_(stream_token_); |
AndyA | 4:33006c37c633 | 245 | printNetTerminator_(); |
AndyA | 4:33006c37c633 | 246 | if(convertTimestamp) { |
AndyA | 4:33006c37c633 | 247 | print_("plotly-convertTimestamp: \""); |
AndyA | 4:33006c37c633 | 248 | print_(timezone); |
AndyA | 4:33006c37c633 | 249 | print_("\""); |
AndyA | 4:33006c37c633 | 250 | printNetTerminator_(); |
AndyA | 4:33006c37c633 | 251 | } |
AndyA | 4:33006c37c633 | 252 | printNetTerminator_(); |
AndyA | 0:96532c59670f | 253 | |
AndyA | 1:d532e96fca12 | 254 | if(log_level < 3) fprintf(stderr,"... Done initializing, ready to stream!\n"); |
AndyA | 4:33006c37c633 | 255 | return true; |
AndyA | 0:96532c59670f | 256 | } |
AndyA | 0:96532c59670f | 257 | |
AndyA | 0:96532c59670f | 258 | void plotly::closeStream() |
AndyA | 0:96532c59670f | 259 | { |
AndyA | 2:d53d74ed68ac | 260 | if (socket) { |
AndyA | 2:d53d74ed68ac | 261 | if (socket->is_connected()) { |
AndyA | 4:33006c37c633 | 262 | print_("0\r\n\r\n"); |
AndyA | 4:33006c37c633 | 263 | socket->close(); |
AndyA | 2:d53d74ed68ac | 264 | } |
AndyA | 2:d53d74ed68ac | 265 | delete socket; |
AndyA | 4:33006c37c633 | 266 | socket=NULL; |
AndyA | 2:d53d74ed68ac | 267 | } |
AndyA | 0:96532c59670f | 268 | } |
AndyA | 1:d532e96fca12 | 269 | |
AndyA | 0:96532c59670f | 270 | void plotly::reconnectStream() |
AndyA | 0:96532c59670f | 271 | { |
AndyA | 2:d53d74ed68ac | 272 | while(!dry_run && (!socket || !socket->is_connected())) { |
AndyA | 0:96532c59670f | 273 | if(log_level<4) fprintf(stderr,"... Disconnected from streaming servers\n"); |
AndyA | 0:96532c59670f | 274 | closeStream(); |
AndyA | 0:96532c59670f | 275 | openStream(); |
AndyA | 0:96532c59670f | 276 | } |
AndyA | 0:96532c59670f | 277 | } |
AndyA | 2:d53d74ed68ac | 278 | |
AndyA | 4:33006c37c633 | 279 | void plotly::plot(unsigned long x, int y) |
AndyA | 0:96532c59670f | 280 | { |
AndyA | 4:33006c37c633 | 281 | if (!initalised) |
AndyA | 4:33006c37c633 | 282 | return; |
AndyA | 4:33006c37c633 | 283 | |
AndyA | 4:33006c37c633 | 284 | reconnectStream(); |
AndyA | 4:33006c37c633 | 285 | |
AndyA | 4:33006c37c633 | 286 | // need to prefix with the length so print it once to get the content lenght and then a second time with the prefix. |
AndyA | 4:33006c37c633 | 287 | int len = snprintf(buffer,k_bufferSize,"{\"x\": %lu, \"y\": %d}\n", x,y); |
AndyA | 4:33006c37c633 | 288 | len = snprintf(buffer,k_bufferSize,"%x\r\n{\"x\": %lu, \"y\": %d}\n\r\n",len, x,y); |
AndyA | 4:33006c37c633 | 289 | sendFormatedText(buffer,len); |
AndyA | 0:96532c59670f | 290 | } |
AndyA | 0:96532c59670f | 291 | |
AndyA | 4:33006c37c633 | 292 | void plotly::plot(unsigned long x, float y) |
AndyA | 0:96532c59670f | 293 | { |
AndyA | 2:d53d74ed68ac | 294 | if (!initalised) |
AndyA | 2:d53d74ed68ac | 295 | return; |
AndyA | 2:d53d74ed68ac | 296 | |
AndyA | 0:96532c59670f | 297 | reconnectStream(); |
AndyA | 2:d53d74ed68ac | 298 | |
AndyA | 4:33006c37c633 | 299 | // need to prefix with the length so print it once to get the content lenght and then a second time with the prefix. |
AndyA | 2:d53d74ed68ac | 300 | int len = snprintf(buffer,k_bufferSize,"{\"x\": %lu, \"y\": %.3f}\n", x,y); |
AndyA | 2:d53d74ed68ac | 301 | len = snprintf(buffer,k_bufferSize,"%x\r\n{\"x\": %lu, \"y\": %.3f}\n\r\n",len, x,y); |
AndyA | 2:d53d74ed68ac | 302 | sendFormatedText(buffer,len); |
AndyA | 0:96532c59670f | 303 | } |
AndyA | 0:96532c59670f | 304 | |
AndyA | 5:fc8eefeb301b | 305 | void plotly::plot(float x, float y) |
AndyA | 5:fc8eefeb301b | 306 | { |
AndyA | 5:fc8eefeb301b | 307 | if (!initalised) |
AndyA | 5:fc8eefeb301b | 308 | return; |
AndyA | 5:fc8eefeb301b | 309 | |
AndyA | 5:fc8eefeb301b | 310 | reconnectStream(); |
AndyA | 5:fc8eefeb301b | 311 | |
AndyA | 5:fc8eefeb301b | 312 | // need to prefix with the length so print it once to get the content lenght and then a second time with the prefix. |
AndyA | 5:fc8eefeb301b | 313 | int len = snprintf(buffer,k_bufferSize,"{\"x\": %.3f, \"y\": %.3f}\n", x,y); |
AndyA | 5:fc8eefeb301b | 314 | len = snprintf(buffer,k_bufferSize,"%x\r\n{\"x\": %.3f, \"y\": %.3f}\n\r\n",len, x,y); |
AndyA | 5:fc8eefeb301b | 315 | sendFormatedText(buffer,len); |
AndyA | 5:fc8eefeb301b | 316 | } |
AndyA | 5:fc8eefeb301b | 317 | |
AndyA | 5:fc8eefeb301b | 318 | |
AndyA | 1:d532e96fca12 | 319 | bool plotly::print_(int d) |
AndyA | 0:96532c59670f | 320 | { |
AndyA | 4:33006c37c633 | 321 | char smallBuffer[10]; |
AndyA | 4:33006c37c633 | 322 | int32_t len = snprintf(smallBuffer,10,"%d",d); |
AndyA | 4:33006c37c633 | 323 | return sendFormatedText(smallBuffer,len); |
AndyA | 2:d53d74ed68ac | 324 | } |
AndyA | 1:d532e96fca12 | 325 | |
AndyA | 1:d532e96fca12 | 326 | bool plotly::print_(unsigned long d) |
AndyA | 0:96532c59670f | 327 | { |
AndyA | 4:33006c37c633 | 328 | char smallBuffer[12]; |
AndyA | 4:33006c37c633 | 329 | int32_t len = snprintf(smallBuffer,12,"%lu",d); |
AndyA | 4:33006c37c633 | 330 | return sendFormatedText(smallBuffer,len); |
AndyA | 2:d53d74ed68ac | 331 | } |
AndyA | 2:d53d74ed68ac | 332 | |
AndyA | 2:d53d74ed68ac | 333 | bool plotly::print_(float d) |
AndyA | 2:d53d74ed68ac | 334 | { |
AndyA | 4:33006c37c633 | 335 | char smallBuffer[12]; |
AndyA | 4:33006c37c633 | 336 | int32_t len = snprintf(smallBuffer,12,"%f",d); |
AndyA | 4:33006c37c633 | 337 | return sendFormatedText(smallBuffer,len); |
AndyA | 2:d53d74ed68ac | 338 | } |
AndyA | 2:d53d74ed68ac | 339 | |
AndyA | 4:33006c37c633 | 340 | bool plotly::printNetTerminator_() |
AndyA | 4:33006c37c633 | 341 | { |
AndyA | 4:33006c37c633 | 342 | return sendFormatedText("\r\n",2); |
AndyA | 4:33006c37c633 | 343 | } |
AndyA | 4:33006c37c633 | 344 | |
AndyA | 4:33006c37c633 | 345 | |
AndyA | 4:33006c37c633 | 346 | bool plotly::print_(char *d) // strings could be long, use the big buffer |
AndyA | 2:d53d74ed68ac | 347 | { |
AndyA | 2:d53d74ed68ac | 348 | int32_t len = snprintf(buffer,k_bufferSize,"%s",d); |
AndyA | 2:d53d74ed68ac | 349 | return sendFormatedText(buffer,len); |
AndyA | 2:d53d74ed68ac | 350 | } |
AndyA | 2:d53d74ed68ac | 351 | |
AndyA | 2:d53d74ed68ac | 352 | |
AndyA | 2:d53d74ed68ac | 353 | bool plotly::sendFormatedText(char* data, int size) |
AndyA | 2:d53d74ed68ac | 354 | { |
AndyA | 2:d53d74ed68ac | 355 | if(log_level < 2) { |
AndyA | 2:d53d74ed68ac | 356 | fprintf(stderr,"%s",data); |
AndyA | 2:d53d74ed68ac | 357 | } |
AndyA | 1:d532e96fca12 | 358 | if(!dry_run) { |
AndyA | 2:d53d74ed68ac | 359 | if (!socket) { |
AndyA | 2:d53d74ed68ac | 360 | fprintf(stderr,"\nTX failed, No network socket exists\n"); |
AndyA | 2:d53d74ed68ac | 361 | return false; |
AndyA | 4:33006c37c633 | 362 | } |
AndyA | 2:d53d74ed68ac | 363 | if (!(socket->is_connected())) { |
AndyA | 2:d53d74ed68ac | 364 | fprintf(stderr,"\nTX failed, Network socket not connected\n"); |
AndyA | 4:33006c37c633 | 365 | return false; |
AndyA | 4:33006c37c633 | 366 | } |
AndyA | 4:33006c37c633 | 367 | |
AndyA | 2:d53d74ed68ac | 368 | int32_t sent = socket->send_all(data,size); |
AndyA | 2:d53d74ed68ac | 369 | if (sent == size) |
AndyA | 1:d532e96fca12 | 370 | return true; |
AndyA | 1:d532e96fca12 | 371 | else { |
AndyA | 2:d53d74ed68ac | 372 | fprintf(stderr,"\nTX failed to send _%s_ Sent %d of %d bytes\n",data,sent,size); |
AndyA | 1:d532e96fca12 | 373 | return false; |
AndyA | 1:d532e96fca12 | 374 | } |
AndyA | 1:d532e96fca12 | 375 | } else |
AndyA | 1:d532e96fca12 | 376 | return true; |
AndyA | 0:96532c59670f | 377 | } |
AndyA | 0:96532c59670f | 378 |