Close

Speaking to PLOTLY

A project log for plotly + Arduino + ESP8266

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

johnnyJohnny 05/06/2015 at 03:460 Comments

1. First step is to create a plotly account and go to your setting and generate a token. Write down your username, api-key, and streaming token. This information needs to be added to the arduino code.

2. Your microcontroller 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 your arduino will need to connect to the "plot.ly" server using the ESP8266. The AT command looks like this:

AT+CIPSTART=0,"TCP","plot.ly",8

Followed by a send command with the post length (formula code seen below). In this example "336":

AT+CIPSEND=0,336

Then a POST request is sent to the "plot.ly" server as follows:

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:

AT+CIPCLOSE="0"

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 code for calculating this can be seen below). Also multiple stream tokens can be used for more graphs.

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;
}
  
String contentLengthString = String(contentLength);
const char* contentLengthConstString = contentLengthString.c_str();
  
unsigned int postLength = contentLength + 94 + strlen(contentLengthConstString);

3. 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 (see the formula code below).

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