Skip to content

Instantly share code, notes, and snippets.

@chriddyp
Created April 23, 2014 16:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriddyp/11222798 to your computer and use it in GitHub Desktop.
Save chriddyp/11222798 to your computer and use it in GitHub Desktop.
Streaming multiple traces to multiple plots with Arduino and Plotly
#include <SPI.h>
#include <Ethernet.h>
#include "plotly_streaming_ethernet.h"
// View your API key and streamtokens here: https://plot.ly/settings
#define nTracesT 2
#define nTracesH 2
// View your tokens here: https://plot.ly/settings
// Supply as many tokens as data traces
// e.g. if you want to ploty A0 and A1 vs time, supply two tokens
char *tokens1[nTracesT] = {"your_token_1", "your_token_2"};
char *tokens2[nTracesH] = {"your_token_3", "your_token_4"};
// arguments: username, api key, streaming token, filename
plotly graph1 = plotly("your_username", "your_api_key", tokens1, "graph1", nTracesT);
plotly graph2 = plotly("your_username", "your_api_key", tokens2, "graph2", nTracesH);
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
EthernetClient client;
void startEthernet(){
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
startEthernet();
bool success1, success2;
success1 = graph1.init();
if(!success1){while(true){}}
success2 = graph2.init();
if(!success2){while(true){}}
graph2.openStream();
}
unsigned long x;
int y;
void loop() {
graph2.plot(millis(), analogRead(A0), tokens1[0]);
graph2.plot(millis(), analogRead(A0)+10, tokens1[1]);
graph2.plot(millis(), analogRead(A1)+400, tokens2[0]);
graph2.plot(millis(), analogRead(A1)+500, tokens2[1]);
delay(25);
}
@chriddyp
Copy link
Author

It's confusing that we plot to graph2 in the loop. In the plotly library, each graph holds a connection to plotly's servers. However, the Arduino can only hold 1 connection open at a time, so when graph2 initializes (graph2.init()), the graph1 connection is closed. This is OK, because all plotly's streaming servers need are the stream tokens!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment