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:
Tue Jul 29 13:30:31 2014 +0000
Revision:
7:9409a72ab6c0
Mid merge;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AndyA 7:9409a72ab6c0 1 #ifndef plotly_streaming_ethernet_h
AndyA 7:9409a72ab6c0 2 #define plotly_streaming_ethernet_h
AndyA 7:9409a72ab6c0 3
AndyA 7:9409a72ab6c0 4 #include <EthernetInterface.h>
AndyA 7:9409a72ab6c0 5 #include <TCPSocketConnection.h>
AndyA 7:9409a72ab6c0 6
AndyA 7:9409a72ab6c0 7 // size of the large buffer used for constructing messages.
AndyA 7:9409a72ab6c0 8 #define k_bufferSize 400
AndyA 7:9409a72ab6c0 9
AndyA 7:9409a72ab6c0 10
AndyA 7:9409a72ab6c0 11 /** Create a plot on plot.ly
AndyA 7:9409a72ab6c0 12 *
AndyA 7:9409a72ab6c0 13 * Based on the Ardunio code supplied by plot.ly
AndyA 7:9409a72ab6c0 14 *
AndyA 7:9409a72ab6c0 15 * Creates a streaming X/Y scatter plot with line on the plot.ly site.
AndyA 7:9409a72ab6c0 16 * Update periods can be between 50ms and 60s due to limitations imposed by plot.ly.
AndyA 7:9409a72ab6c0 17 *
AndyA 7:9409a72ab6c0 18 * Requires an mbed with network support.
AndyA 7:9409a72ab6c0 19 *
AndyA 7:9409a72ab6c0 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 7:9409a72ab6c0 21 *
AndyA 7:9409a72ab6c0 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 7:9409a72ab6c0 23 *
AndyA 7:9409a72ab6c0 24 * See Plotly_HelloWorld for a sample implimentation.
AndyA 7:9409a72ab6c0 25 *
AndyA 7:9409a72ab6c0 26 */
AndyA 7:9409a72ab6c0 27
AndyA 7:9409a72ab6c0 28 class plotly
AndyA 7:9409a72ab6c0 29 {
AndyA 7:9409a72ab6c0 30 public:
AndyA 7:9409a72ab6c0 31 /**
AndyA 7:9409a72ab6c0 32 @param username Your plot.ly username
AndyA 7:9409a72ab6c0 33 @param api_key Your plot.ly API key
AndyA 7:9409a72ab6c0 34 @param stream_token A plot.ly streaming token for your plot.ly account
AndyA 7:9409a72ab6c0 35 @param filename The name of the file to save the chart as
AndyA 7:9409a72ab6c0 36 */
AndyA 7:9409a72ab6c0 37 plotly(char *username, char *api_key, char* stream_token, char *filename);
AndyA 7:9409a72ab6c0 38 /**
AndyA 7:9409a72ab6c0 39 */
AndyA 7:9409a72ab6c0 40 ~plotly();
AndyA 7:9409a72ab6c0 41
AndyA 7:9409a72ab6c0 42 /** Initalises the chart
AndyA 7:9409a72ab6c0 43
AndyA 7:9409a72ab6c0 44 This fucntion creates a blank chart on the plot.ly system and configures it to recieve streamed data using the specified token
AndyA 7:9409a72ab6c0 45
AndyA 7:9409a72ab6c0 46 Time taken for this function can vary depending on network delays.
AndyA 7:9409a72ab6c0 47
AndyA 7:9409a72ab6c0 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 7:9409a72ab6c0 49 */
AndyA 7:9409a72ab6c0 50 bool init();
AndyA 7:9409a72ab6c0 51
AndyA 7:9409a72ab6c0 52 /**
AndyA 7:9409a72ab6c0 53 Adds a point to the chart. The chart MUST be initalised before calling this.
AndyA 7:9409a72ab6c0 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
AndyA 7:9409a72ab6c0 56 @param x The X value.
AndyA 7:9409a72ab6c0 57 @param y The y value.
AndyA 7:9409a72ab6c0 58 */
AndyA 7:9409a72ab6c0 59 void plot(unsigned long x, int y);
AndyA 7:9409a72ab6c0 60 /**
AndyA 7:9409a72ab6c0 61 Adds a point to the chart. The chart MUST be initalised before calling this.
AndyA 7:9409a72ab6c0 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 7:9409a72ab6c0 63
AndyA 7:9409a72ab6c0 64 @param x The X value.
AndyA 7:9409a72ab6c0 65 @param y The y value.
AndyA 7:9409a72ab6c0 66 */
AndyA 7:9409a72ab6c0 67 void plot(unsigned long x, float y);
AndyA 7:9409a72ab6c0 68
AndyA 7:9409a72ab6c0 69 /**
AndyA 7:9409a72ab6c0 70 Adds a point to the chart. The chart MUST be initalised before calling this.
AndyA 7:9409a72ab6c0 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 7:9409a72ab6c0 72
AndyA 7:9409a72ab6c0 73 @param x The X value.
AndyA 7:9409a72ab6c0 74 @param y The y value.
AndyA 7:9409a72ab6c0 75 */
AndyA 7:9409a72ab6c0 76 void plot(float x, float y);
AndyA 7:9409a72ab6c0 77
AndyA 7:9409a72ab6c0 78 /**
AndyA 7:9409a72ab6c0 79 Opens the streaming connection.
AndyA 7:9409a72ab6c0 80
AndyA 7:9409a72ab6c0 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 7:9409a72ab6c0 82 */
AndyA 7:9409a72ab6c0 83 bool openStream();
AndyA 7:9409a72ab6c0 84 /**
AndyA 7:9409a72ab6c0 85 closes the streaming connection
AndyA 7:9409a72ab6c0 86 */
AndyA 7:9409a72ab6c0 87 void closeStream();
AndyA 7:9409a72ab6c0 88
AndyA 7:9409a72ab6c0 89 /**
AndyA 7:9409a72ab6c0 90 output message level
AndyA 7:9409a72ab6c0 91 Messages are sent to stderr (which defaults to the mBed USB programing/debug port).
AndyA 7:9409a72ab6c0 92 0 = Debugging, 1 = Informational, 2 = Status, 3 = Errors (default), 4 = Quiet
AndyA 7:9409a72ab6c0 93 */
AndyA 7:9409a72ab6c0 94 int log_level;
AndyA 7:9409a72ab6c0 95
AndyA 7:9409a72ab6c0 96 /**
AndyA 7:9409a72ab6c0 97 set true to not actually connect to the network..
AndyA 7:9409a72ab6c0 98 */
AndyA 7:9409a72ab6c0 99 bool dry_run;
AndyA 7:9409a72ab6c0 100
AndyA 7:9409a72ab6c0 101 /**
AndyA 7:9409a72ab6c0 102 Maximum points to display on the streaming chart, once you go over this old points will no longer be displayed.
AndyA 7:9409a72ab6c0 103 Defaults to 30
AndyA 7:9409a72ab6c0 104 */
AndyA 7:9409a72ab6c0 105 int maxpoints;
AndyA 7:9409a72ab6c0 106
AndyA 7:9409a72ab6c0 107 /**
AndyA 7:9409a72ab6c0 108 Sets whether the chart will be public or not.
AndyA 7:9409a72ab6c0 109 Defaults to true
AndyA 7:9409a72ab6c0 110 */
AndyA 7:9409a72ab6c0 111 bool world_readable;
AndyA 7:9409a72ab6c0 112
AndyA 7:9409a72ab6c0 113 /**
AndyA 7:9409a72ab6c0 114 Converts timestamps to the local time zone
AndyA 7:9409a72ab6c0 115 */
AndyA 7:9409a72ab6c0 116 bool convertTimestamp;
AndyA 7:9409a72ab6c0 117 /**
AndyA 7:9409a72ab6c0 118 Timezone string to use
AndyA 7:9409a72ab6c0 119 */
AndyA 7:9409a72ab6c0 120 char *timezone;
AndyA 7:9409a72ab6c0 121 /**
AndyA 7:9409a72ab6c0 122 Sets what to do if the file already exists, valid options are:
AndyA 7:9409a72ab6c0 123 "new","overwrite" (default),"append","extend"
AndyA 7:9409a72ab6c0 124 */
AndyA 7:9409a72ab6c0 125 char *fileopt;
AndyA 7:9409a72ab6c0 126
AndyA 7:9409a72ab6c0 127 private:
AndyA 7:9409a72ab6c0 128
AndyA 7:9409a72ab6c0 129 void reconnectStream();
AndyA 7:9409a72ab6c0 130
AndyA 7:9409a72ab6c0 131 bool print_(int d);
AndyA 7:9409a72ab6c0 132 bool print_(unsigned long d);
AndyA 7:9409a72ab6c0 133 bool print_(float d);
AndyA 7:9409a72ab6c0 134 bool print_(char *d);
AndyA 7:9409a72ab6c0 135 bool printNetTerminator_();
AndyA 7:9409a72ab6c0 136
AndyA 7:9409a72ab6c0 137
AndyA 7:9409a72ab6c0 138 bool sendFormatedText(char* data, int size);
AndyA 7:9409a72ab6c0 139
AndyA 7:9409a72ab6c0 140 char buffer[512];
AndyA 7:9409a72ab6c0 141 // char rxBuffer[128];
AndyA 7:9409a72ab6c0 142 TCPSocketConnection *socket;
AndyA 7:9409a72ab6c0 143
AndyA 7:9409a72ab6c0 144 char *username_;
AndyA 7:9409a72ab6c0 145 char *api_key_;
AndyA 7:9409a72ab6c0 146 char *stream_token_;
AndyA 7:9409a72ab6c0 147 char *filename_;
AndyA 7:9409a72ab6c0 148
AndyA 7:9409a72ab6c0 149 bool initalised;
AndyA 7:9409a72ab6c0 150
AndyA 7:9409a72ab6c0 151 };
AndyA 7:9409a72ab6c0 152 #endif