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:
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?

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