Skip to content

Instantly share code, notes, and snippets.

Created April 23, 2014 16:39
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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:
#define nTracesT 2
#define nTracesH 2
// View your tokens here:
// 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:
// 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);
void setup() {
// Open serial communications and wait for port to open:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
bool success1, success2;
success1 = graph1.init();
success2 = graph2.init();
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]);
Copy link

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