// This #include statement was automatically added by the Spark IDE.
#include "Adafruit_DHT/Adafruit_DHT.h"

// This #include statement was automatically added by the Spark IDE.
#include "spark-plotly/spark-plotly.h"

#define DHTPIN 6 //Temperature and humidity sensor will connect to digital pin 6
#define DHTTYPE DHT22  // DHT 22  (AM2302)

#define NUM_TRACES 4 //There will be 4 data traces in the stream

DHT dht(DHTPIN, DHTTYPE); //Create a DHT 22 object on digital pin 6

int lightPin = A6; //Light sensor will connect on analog pin 6
int uvPin = A7; //UV sensor will connect on analog pin 7

int lightVal = 0;  //Variable to store light sensor data
int uvVal = 0; //Variable to store UV sensor data

char *streaming_tokens[NUM_TRACES] = {“<<INSERT TOKEN 1>>“, "<<INSERT TOKEN 2>>", "<<INSERT TOKEN 3>>", "<<INSERT TOKEN 4>>"}; //Enter the tokens you generated under Plotly API settings

plotly graph = plotly(“<<YOUR PLOTLY USERNAME>>“, “<<YOUR API KEY>>“, streaming_tokens, “<<ENTER GRAPH NAME>>“, NUM_TRACES); //Create a plotly graph with your username, API key, streaming tokens, and graph name

void setup() {
    
    pinMode(lightPin, INPUT); //Specify pin 6 as an input
    pinMode(uvPin, INPUT); //Specify pin 7 as an input
    
    dht.begin(); //Fire up the DHT 22
    delay(1000); //Wait a second
    
    graph.fileopt = "extend"; //This will keep adding data to the file instead of overwriting in case your Core ever needs to restart
    graph.convertTimestamp = "true"; //Convert millis to a date/time stamp
    graph.timezone = "America/Denver"; //Choose your timezone
    graph.init(); //Initialize the graph
    graph.openStream(); //Fire up the stream
}

void loop() {
    
    float t = dht.getTempFarenheit(); //Get temperature from the DHT 22
    float h = dht.getHumidity(); Get humidity from the DHT 22
    lightVal = analogRead(lightPin);
    uvVal = analogRead(uvPin);
    
    unsigned long x = millis(); //Variable to store the time in milliseconds that your Core has been alive
    
    graph.plot(x, t, streaming_tokens[0]); //Add a timestamped temperature data point to the graph
    graph.plot(x, h, streaming_tokens[1]); //Add a timestamped humidity data point to the graph
    graph.plot(x, lightVal, streaming_tokens[2]); //Add a timestamped light data point to the graph
    graph.plot(x, uvVal, streaming_tokens[3]); //Add a timestamped UV data point to the graph
    delay(4000);  //Wait a while
}