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.h@7:9409a72ab6c0, 2014-07-29 (annotated)
- Committer:
- AndyA
- Date:
- Tue Jul 29 13:30:31 2014 +0000
- Revision:
- 7:9409a72ab6c0
- Parent:
- 5:fc8eefeb301b
- Parent:
- 6:e57d6e9313f4
- Child:
- 8:d4f705ba2ea5
Mid merge;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AndyA | 0:96532c59670f | 1 | #ifndef plotly_streaming_ethernet_h |
AndyA | 0:96532c59670f | 2 | #define plotly_streaming_ethernet_h |
AndyA | 0:96532c59670f | 3 | |
AndyA | 0:96532c59670f | 4 | #include <EthernetInterface.h> |
AndyA | 0:96532c59670f | 5 | #include <TCPSocketConnection.h> |
AndyA | 0:96532c59670f | 6 | |
AndyA | 4:33006c37c633 | 7 | // size of the large buffer used for constructing messages. |
AndyA | 2:d53d74ed68ac | 8 | #define k_bufferSize 512 |
AndyA | 2:d53d74ed68ac | 9 | |
AndyA | 0:96532c59670f | 10 | |
AndyA | 4:33006c37c633 | 11 | /** Create a plot on plot.ly |
AndyA | 4:33006c37c633 | 12 | * |
AndyA | 4:33006c37c633 | 13 | * Based on the Ardunio code supplied by plot.ly |
AndyA | 4:33006c37c633 | 14 | * |
AndyA | 4:33006c37c633 | 15 | * Creates a streaming X/Y scatter plot with line on the plot.ly site. |
AndyA | 4:33006c37c633 | 16 | * Update periods can be between 50ms and 60s due to limitations imposed by plot.ly. |
AndyA | 4:33006c37c633 | 17 | * |
AndyA | 4:33006c37c633 | 18 | * Requires an mbed with network support. |
AndyA | 4:33006c37c633 | 19 | * |
AndyA | 4:33006c37c633 | 20 | * Provided as is, it works for me but your mileage may vary. Sorry, I don't have time to offer much support on this. |
AndyA | 4:33006c37c633 | 21 | * |
AndyA | 4:33006c37c633 | 22 | * You will need to create a plot.ly account and then go to https://plot.ly/settings to get your API key and a streaming token. |
AndyA | 4:33006c37c633 | 23 | * |
AndyA | 4:33006c37c633 | 24 | * See Plotly_HelloWorld for a sample implimentation. |
AndyA | 4:33006c37c633 | 25 | * |
AndyA | 4:33006c37c633 | 26 | */ |
AndyA | 2:d53d74ed68ac | 27 | |
AndyA | 0:96532c59670f | 28 | class plotly |
AndyA | 0:96532c59670f | 29 | { |
AndyA | 0:96532c59670f | 30 | public: |
AndyA | 4:33006c37c633 | 31 | /** |
AndyA | 4:33006c37c633 | 32 | @param username Your plot.ly username |
AndyA | 4:33006c37c633 | 33 | @param api_key Your plot.ly API key |
AndyA | 7:9409a72ab6c0 | 34 | @param stream_tokens An array of plot.ly streaming token for your plot.ly account |
AndyA | 4:33006c37c633 | 35 | @param filename The name of the file to save the chart as |
AndyA | 7:9409a72ab6c0 | 36 | @param nTraces The number of traces (MUST match the size of stream_tokens, can be omitted for a single line) |
AndyA | 4:33006c37c633 | 37 | */ |
AndyA | 7:9409a72ab6c0 | 38 | plotly(const char *username, const char *api_key, const char ** stream_tokens, const char *filename, int nTraces = 1); |
AndyA | 7:9409a72ab6c0 | 39 | |
AndyA | 0:96532c59670f | 40 | ~plotly(); |
AndyA | 4:33006c37c633 | 41 | |
AndyA | 4:33006c37c633 | 42 | /** Initalises the chart |
AndyA | 6:e57d6e9313f4 | 43 | |
AndyA | 4:33006c37c633 | 44 | This fucntion creates a blank chart on the plot.ly system and configures it to recieve streamed data using the specified token |
AndyA | 4:33006c37c633 | 45 | |
AndyA | 4:33006c37c633 | 46 | Time taken for this function can vary depending on network delays. |
AndyA | 6:e57d6e9313f4 | 47 | |
AndyA | 4:33006c37c633 | 48 | If you wish to change any of the options line max points or world readability then make sure you change them BEFORE calling this function. |
AndyA | 4:33006c37c633 | 49 | */ |
AndyA | 0:96532c59670f | 50 | bool init(); |
AndyA | 4:33006c37c633 | 51 | |
AndyA | 4:33006c37c633 | 52 | /** |
AndyA | 4:33006c37c633 | 53 | Adds a point to the chart. The chart MUST be initalised before calling this. |
AndyA | 4:33006c37c633 | 54 | Note, if the streaming network port is closed then this will attempt to open the port and re-establish the stream connection, this could block for a while. |
AndyA | 7:9409a72ab6c0 | 55 | @param x The X value. |
AndyA | 7:9409a72ab6c0 | 56 | @param y The y value. |
AndyA | 7:9409a72ab6c0 | 57 | @param stream Which trace to add the point to counting from 0, can be omitted for charts with a single line. |
AndyA | 7:9409a72ab6c0 | 58 | */ |
AndyA | 7:9409a72ab6c0 | 59 | void plot(unsigned long x, int y, int stream = 0); |
AndyA | 0:96532c59670f | 60 | |
AndyA | 7:9409a72ab6c0 | 61 | /** |
AndyA | 7:9409a72ab6c0 | 62 | Adds a point to the chart. The chart MUST be initalised before calling this. |
AndyA | 7:9409a72ab6c0 | 63 | Note, if the streaming network port is closed then this will attempt to open the port and re-establish the stream connection, this could block for a while. |
AndyA | 7:9409a72ab6c0 | 64 | @param stream Which trace to add the point to counting from 0, can be omitted for charts with a single line. |
AndyA | 4:33006c37c633 | 65 | @param x The X value. |
AndyA | 4:33006c37c633 | 66 | @param y The y value. |
AndyA | 4:33006c37c633 | 67 | */ |
AndyA | 6:e57d6e9313f4 | 68 | void plot(unsigned long x, float y, int stream = 0); |
AndyA | 0:96532c59670f | 69 | |
AndyA | 4:33006c37c633 | 70 | /** |
AndyA | 5:fc8eefeb301b | 71 | Adds a point to the chart. The chart MUST be initalised before calling this. |
AndyA | 5:fc8eefeb301b | 72 | Note, if the streaming network port is closed then this will attempt to open the port and re-establish the stream connection, this could block for a while. |
AndyA | 5:fc8eefeb301b | 73 | |
AndyA | 5:fc8eefeb301b | 74 | @param x The X value. |
AndyA | 5:fc8eefeb301b | 75 | @param y The y value. |
AndyA | 7:9409a72ab6c0 | 76 | @param stream Which trace to add the point to counting from 0, can be omitted for charts with a single line. |
AndyA | 5:fc8eefeb301b | 77 | */ |
AndyA | 6:e57d6e9313f4 | 78 | void plot(float x, float y, int stream = 0); |
AndyA | 0:96532c59670f | 79 | |
AndyA | 5:fc8eefeb301b | 80 | |
AndyA | 5:fc8eefeb301b | 81 | /** |
AndyA | 7:9409a72ab6c0 | 82 | Opens all the streaming connections. |
AndyA | 4:33006c37c633 | 83 | |
AndyA | 7:9409a72ab6c0 | 84 | Normally you'd do this after a sucessful call to init() and before starting plotting in order to avoid a connection delays when you first call plot() |
AndyA | 7:9409a72ab6c0 | 85 | */ |
AndyA | 7:9409a72ab6c0 | 86 | void openStreams(); |
AndyA | 7:9409a72ab6c0 | 87 | |
AndyA | 7:9409a72ab6c0 | 88 | /** close all the streaming connections |
AndyA | 7:9409a72ab6c0 | 89 | |
AndyA | 7:9409a72ab6c0 | 90 | Call to tidy up and free up system resources once there is no more data to send |
AndyA | 4:33006c37c633 | 91 | */ |
AndyA | 7:9409a72ab6c0 | 92 | void closeStreams(); |
AndyA | 7:9409a72ab6c0 | 93 | |
AndyA | 4:33006c37c633 | 94 | /** |
AndyA | 7:9409a72ab6c0 | 95 | Opens a specific streaming connection. Normally you would use openStreams() however if you only want to update a single line on a multi-line chart then this can save time and memory. |
AndyA | 7:9409a72ab6c0 | 96 | |
AndyA | 7:9409a72ab6c0 | 97 | @param stream The line number (from 0) to open |
AndyA | 4:33006c37c633 | 98 | */ |
AndyA | 7:9409a72ab6c0 | 99 | bool openStream(int stream); |
AndyA | 7:9409a72ab6c0 | 100 | |
AndyA | 7:9409a72ab6c0 | 101 | /** |
AndyA | 7:9409a72ab6c0 | 102 | Close a specific streaming connection. |
AndyA | 7:9409a72ab6c0 | 103 | |
AndyA | 7:9409a72ab6c0 | 104 | @param stream The line number (from 0) to open |
AndyA | 7:9409a72ab6c0 | 105 | */ |
AndyA | 7:9409a72ab6c0 | 106 | void closeStream(int stream); |
AndyA | 0:96532c59670f | 107 | |
AndyA | 4:33006c37c633 | 108 | /** |
AndyA | 4:33006c37c633 | 109 | output message level |
AndyA | 4:33006c37c633 | 110 | Messages are sent to stderr (which defaults to the mBed USB programing/debug port). |
AndyA | 4:33006c37c633 | 111 | 0 = Debugging, 1 = Informational, 2 = Status, 3 = Errors (default), 4 = Quiet |
AndyA | 4:33006c37c633 | 112 | */ |
AndyA | 0:96532c59670f | 113 | int log_level; |
AndyA | 4:33006c37c633 | 114 | |
AndyA | 4:33006c37c633 | 115 | /** |
AndyA | 4:33006c37c633 | 116 | set true to not actually connect to the network.. |
AndyA | 4:33006c37c633 | 117 | */ |
AndyA | 0:96532c59670f | 118 | bool dry_run; |
AndyA | 4:33006c37c633 | 119 | |
AndyA | 4:33006c37c633 | 120 | /** |
AndyA | 4:33006c37c633 | 121 | Maximum points to display on the streaming chart, once you go over this old points will no longer be displayed. |
AndyA | 4:33006c37c633 | 122 | Defaults to 30 |
AndyA | 4:33006c37c633 | 123 | */ |
AndyA | 0:96532c59670f | 124 | int maxpoints; |
AndyA | 4:33006c37c633 | 125 | |
AndyA | 4:33006c37c633 | 126 | /** |
AndyA | 4:33006c37c633 | 127 | Sets whether the chart will be public or not. |
AndyA | 4:33006c37c633 | 128 | Defaults to true |
AndyA | 4:33006c37c633 | 129 | */ |
AndyA | 0:96532c59670f | 130 | bool world_readable; |
AndyA | 4:33006c37c633 | 131 | |
AndyA | 4:33006c37c633 | 132 | /** |
AndyA | 4:33006c37c633 | 133 | Converts timestamps to the local time zone |
AndyA | 4:33006c37c633 | 134 | */ |
AndyA | 0:96532c59670f | 135 | bool convertTimestamp; |
AndyA | 4:33006c37c633 | 136 | /** |
AndyA | 4:33006c37c633 | 137 | Timezone string to use |
AndyA | 4:33006c37c633 | 138 | */ |
AndyA | 0:96532c59670f | 139 | char *timezone; |
AndyA | 4:33006c37c633 | 140 | /** |
AndyA | 4:33006c37c633 | 141 | Sets what to do if the file already exists, valid options are: |
AndyA | 4:33006c37c633 | 142 | "new","overwrite" (default),"append","extend" |
AndyA | 4:33006c37c633 | 143 | */ |
AndyA | 0:96532c59670f | 144 | char *fileopt; |
AndyA | 0:96532c59670f | 145 | |
AndyA | 0:96532c59670f | 146 | private: |
AndyA | 6:e57d6e9313f4 | 147 | |
AndyA | 7:9409a72ab6c0 | 148 | void reconnectStream(int stream); |
AndyA | 4:33006c37c633 | 149 | |
AndyA | 6:e57d6e9313f4 | 150 | bool print_(int d,int stream = 0); |
AndyA | 6:e57d6e9313f4 | 151 | bool print_(unsigned long d,int stream = 0); |
AndyA | 6:e57d6e9313f4 | 152 | bool print_(float d,int stream = 0); |
AndyA | 6:e57d6e9313f4 | 153 | bool print_(char *d,int stream = 0) {return print_((const char *)d, stream);}; |
AndyA | 6:e57d6e9313f4 | 154 | bool print_(const char *d,int stream = 0); |
AndyA | 6:e57d6e9313f4 | 155 | bool printHex_(uint16_t d,int stream = 0); |
AndyA | 7:9409a72ab6c0 | 156 | bool printNetTerminator_(int stream = 0); |
AndyA | 7:9409a72ab6c0 | 157 | |
AndyA | 6:e57d6e9313f4 | 158 | bool sendFormatedText(char* data, int size,int stream = 0); |
AndyA | 0:96532c59670f | 159 | |
AndyA | 7:9409a72ab6c0 | 160 | char buffer[k_bufferSize]; |
AndyA | 7:9409a72ab6c0 | 161 | |
AndyA | 6:e57d6e9313f4 | 162 | TCPSocketConnection **sockets; |
AndyA | 0:96532c59670f | 163 | |
AndyA | 6:e57d6e9313f4 | 164 | const char *username_; |
AndyA | 6:e57d6e9313f4 | 165 | const char *api_key_; |
AndyA | 6:e57d6e9313f4 | 166 | const char** stream_tokens_; |
AndyA | 6:e57d6e9313f4 | 167 | const char *filename_; |
AndyA | 0:96532c59670f | 168 | int nTraces_; |
AndyA | 1:d532e96fca12 | 169 | |
AndyA | 1:d532e96fca12 | 170 | bool initalised; |
AndyA | 0:96532c59670f | 171 | |
AndyA | 0:96532c59670f | 172 | }; |
AndyA | 0:96532c59670f | 173 | #endif |