plotly interface based on ardunio sample code

Dependents:   Plotly_HelloWorld

Library for plotting a simple x/y scatter chart on the plot.ly website.

See plotly_HelloWorld for sample usage.

Committer:
AndyA
Date:
Fri Jul 11 10:10:16 2014 +0000
Revision:
4:33006c37c633
Parent:
2:d53d74ed68ac
Child:
5:fc8eefeb301b
Tidy up and add documentation

Who changed what in which revision?

UserRevisionLine numberNew 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 4:33006c37c633 8 #define k_bufferSize 400
AndyA 0:96532c59670f 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 4:33006c37c633 34 @param stream_token A 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 4:33006c37c633 36 */
AndyA 4:33006c37c633 37 plotly(char *username, char *api_key, char* stream_token, char *filename);
AndyA 4:33006c37c633 38 /**
AndyA 4:33006c37c633 39 */
AndyA 0:96532c59670f 40 ~plotly();
AndyA 4:33006c37c633 41
AndyA 4:33006c37c633 42 /** Initalises the chart
AndyA 4:33006c37c633 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 4:33006c37c633 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 4:33006c37c633 55
AndyA 4:33006c37c633 56 @param x The X value.
AndyA 4:33006c37c633 57 @param y The y value.
AndyA 4:33006c37c633 58 */
AndyA 4:33006c37c633 59 void plot(unsigned long x, int y);
AndyA 4:33006c37c633 60 /**
AndyA 4:33006c37c633 61 Adds a point to the chart. The chart MUST be initalised before calling this.
AndyA 4:33006c37c633 62 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 4:33006c37c633 63
AndyA 4:33006c37c633 64 @param x The X value.
AndyA 4:33006c37c633 65 @param y The y value.
AndyA 4:33006c37c633 66 */
AndyA 4:33006c37c633 67 void plot(unsigned long x, float y);
AndyA 0:96532c59670f 68
AndyA 4:33006c37c633 69 /**
AndyA 4:33006c37c633 70 Opens the streaming connection.
AndyA 4:33006c37c633 71
AndyA 4:33006c37c633 72 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 4:33006c37c633 73 */
AndyA 4:33006c37c633 74 bool openStream();
AndyA 4:33006c37c633 75 /**
AndyA 4:33006c37c633 76 closes the streaming connection
AndyA 4:33006c37c633 77 */
AndyA 4:33006c37c633 78 void closeStream();
AndyA 0:96532c59670f 79
AndyA 4:33006c37c633 80 /**
AndyA 4:33006c37c633 81 output message level
AndyA 4:33006c37c633 82 Messages are sent to stderr (which defaults to the mBed USB programing/debug port).
AndyA 4:33006c37c633 83 0 = Debugging, 1 = Informational, 2 = Status, 3 = Errors (default), 4 = Quiet
AndyA 4:33006c37c633 84 */
AndyA 0:96532c59670f 85 int log_level;
AndyA 4:33006c37c633 86
AndyA 4:33006c37c633 87 /**
AndyA 4:33006c37c633 88 set true to not actually connect to the network..
AndyA 4:33006c37c633 89 */
AndyA 0:96532c59670f 90 bool dry_run;
AndyA 4:33006c37c633 91
AndyA 4:33006c37c633 92 /**
AndyA 4:33006c37c633 93 Maximum points to display on the streaming chart, once you go over this old points will no longer be displayed.
AndyA 4:33006c37c633 94 Defaults to 30
AndyA 4:33006c37c633 95 */
AndyA 0:96532c59670f 96 int maxpoints;
AndyA 4:33006c37c633 97
AndyA 4:33006c37c633 98 /**
AndyA 4:33006c37c633 99 Sets whether the chart will be public or not.
AndyA 4:33006c37c633 100 Defaults to true
AndyA 4:33006c37c633 101 */
AndyA 0:96532c59670f 102 bool world_readable;
AndyA 4:33006c37c633 103
AndyA 4:33006c37c633 104 /**
AndyA 4:33006c37c633 105 Converts timestamps to the local time zone
AndyA 4:33006c37c633 106 */
AndyA 0:96532c59670f 107 bool convertTimestamp;
AndyA 4:33006c37c633 108 /**
AndyA 4:33006c37c633 109 Timezone string to use
AndyA 4:33006c37c633 110 */
AndyA 0:96532c59670f 111 char *timezone;
AndyA 4:33006c37c633 112 /**
AndyA 4:33006c37c633 113 Sets what to do if the file already exists, valid options are:
AndyA 4:33006c37c633 114 "new","overwrite" (default),"append","extend"
AndyA 4:33006c37c633 115 */
AndyA 0:96532c59670f 116 char *fileopt;
AndyA 0:96532c59670f 117
AndyA 0:96532c59670f 118 private:
AndyA 4:33006c37c633 119
AndyA 4:33006c37c633 120 void reconnectStream();
AndyA 4:33006c37c633 121
AndyA 1:d532e96fca12 122 bool print_(int d);
AndyA 1:d532e96fca12 123 bool print_(unsigned long d);
AndyA 1:d532e96fca12 124 bool print_(float d);
AndyA 1:d532e96fca12 125 bool print_(char *d);
AndyA 4:33006c37c633 126 bool printNetTerminator_();
AndyA 4:33006c37c633 127
AndyA 2:d53d74ed68ac 128
AndyA 2:d53d74ed68ac 129 bool sendFormatedText(char* data, int size);
AndyA 0:96532c59670f 130
AndyA 2:d53d74ed68ac 131 char buffer[512];
AndyA 2:d53d74ed68ac 132 // char rxBuffer[128];
AndyA 2:d53d74ed68ac 133 TCPSocketConnection *socket;
AndyA 0:96532c59670f 134
AndyA 0:96532c59670f 135 char *username_;
AndyA 0:96532c59670f 136 char *api_key_;
AndyA 4:33006c37c633 137 char *stream_token_;
AndyA 0:96532c59670f 138 char *filename_;
AndyA 1:d532e96fca12 139
AndyA 1:d532e96fca12 140 bool initalised;
AndyA 0:96532c59670f 141
AndyA 0:96532c59670f 142 };
AndyA 0:96532c59670f 143 #endif