Close

Speaking to PLOTLY

A project log for plotly + ESP8266

(Plotly has stopped support) A $6 Wireless Real-time Data Logger/Grapher

JohnnyJohnny 05/14/2015 at 04:100 Comments

The ESP8266 needs to talk to the "plot.ly" server to initialize and create your data file and prepare it for receiving data. To do this firstly you will need to connect to the "plot.ly" server on "port 80", and send a POST request similar to this:

POST /clientresp HTTP/1.1
Host: plot.ly:80
User-Agent: Arduino/0.6.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 241

version=2.3&origin=plot&platform=arduino&un=johnnyizz&key=xxxxxxxxxx&args=[{"y": [], "x": [], "type": "scatter", "stream": {"token": "xxxxxxxxxx", "maxpoints": 30}}]&kwargs={"fileopt": "overwrite", "filename": "test", "world_readable": true}

Then finally you disconnect.

This has created your file.

*note that your own user name (I used "johnnyizz"), api-key and token will be inserted here, and your own filename and graph settings. Therefore the calculated "Content-Length" will be different for you (the formula for calculating this can be seen in the arduino code below). Also multiple stream tokens can be used for more graphs.

// Calculate content length
unsigned int contentLength = 126 + strlen(userName) + strlen(fileopt) + nTraces*(87+strlen(maxpoints)) + (nTraces - 1)*2 + strlen(fileName);
if(world_readable){
  contentLength += 4;
}
else{
  contentLength += 5;
}

Once the file has been created in your account, we connect to the "arduino.plot.ly" server which will receive our data in jason form. Notice that the data is linked to your stream token. The following is the POST requests for sending data:

POST / HTTP/1.1
Host: arduino.plot.ly
User-Agent: Arduino
Transfer-Encoding: chunked
Connection: close
plotly-convertTimestamp: "Australia/Melbourne"

0x34
{"x": 1234, "y": 1234, "streamtoken": "xxxxxxxxxx"}

0

Here we can choose the time-stamp for our location (mine is "Australia/Melbourne"), the x and y data that's being sent, and our stream token.

Note that the "0x34" above the jason string represents the length of characters making the jason string in hexadecimal.

The following string equals 44 characters:

string = "{/"x/":  ,  /"y/":  ,  /"streamtoken/":  /"xxxxxxxxxx/"}/n";

44 + length of 'x' data + length of 'y' data = 52. Converted to hex is 0x34.

Each time you send data, you repeat this.

Finally, don't forget to use this formula for calculating the post length:

String xString = String(x);
String yString = String(y);
  
const char* xConstString = xString.c_str();
const char* yConstString = yString.c_str();
  
unsigned int jasonLength = 44 + strlen(xConstString) +  strlen(yConstString);
String jasonLengthString = String(jasonLength, HEX);
  
const char* ConstJasonLengthString = jasonLengthString.c_str();
unsigned int postLength = 167 + strlen(ConstJasonLengthString) + jasonLength;

Discussions