Streaming multiple traces to multiple plots with Arduino and Plotly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's confusing that we plot to
graph2
in theloop
. In theplotly
library, eachgraph
holds a connection to plotly's servers. However, the Arduino can only hold 1 connection open at a time, so whengraph2
initializes (graph2.init()
), thegraph1
connection is closed. This is OK, because all plotly's streaming servers need are the stream tokens!