 // Initialize as many plotters as you want! Each plotter will send data to the same plot as a separate line.
 // Make sure that you place these exact-same stream tokens in your `config.json` file on the Linino.
 
-plotly plotter1("rs7ymb8xpy"); plotly plotter2("3lkrelrfxt"); // plot.ly tokens
+plotly plotter1(PLOTLY_TOKEN_1); plotly plotter2(PLOTLY_TOKEN_2); // plot.ly tokens
 
 // Measurement interval in milliseconds
 int measurementInterval = 10000;

//power and energy
double power0, averagedPower0, power1, averagedPower1;

void setup() {

  // start-up the bridge
  Bridge.begin();
  FileSystem.begin();

  delay(2000);
  Console.begin();
  while (!Console) {
    ; // wait for Console port to connect.
  }

  Console.buffer(64);
  delay(2000);

  // attach interrupt routines
  attachInterrupt(0, onPulse0, FALLING); attachInterrupt(1, onPulse1, FALLING);

// Start up the OneWire library
  sensors.begin();

  plotter1.timezone  = "Europe/Berlin"; plotter2.timezone = "Europe/Berlin";
  millis_current = millis();

}

void loop()
{
  if (millis() - millis_current > measurementInterval) {

        // reset timer
    millis_current = millis();

    sensors.requestTemperatures(); // Send the command to get temperatures
    
    // Target: YY-MM-DD hh:mm:ss, elapsed Millis, analogValues
    dataString = getTimeStamp() + ",";
    dataString += String(averagedPower0); dataString += ",";
    dataString += String(averagedPower1); dataString += ",";
    dataString += String(sensors.getTempCByIndex(0));

    // open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another.
    // The FileSystem card is mounted at the following "/mnt/sda1"
    fileName = "/mnt/sda1/datalogs/" + getDate() + ".txt";
    // convert fileName string to char array
    fileName.toCharArray(__fileName, sizeof(__fileName));
    File dataFile = FileSystem.open(__fileName, FILE_APPEND);

    // if the file is available, write to it:
    if (dataFile) {
      dataFile.println(dataString);
      dataFile.close();
      // print to the serial port too:
      Console.println(dataString);
    }
    // if the file isn't open, pop up an error:
    else {
      Console.println("error opening datalog.txt");
    }

    //Plot the value
    plotter1.plot(averagedPower0); // Solar production
    plotter2.plot(averagedPower1); // Zweirichtungszaehler

    //Reset averaging
    averageCount0 = 0; accumulatedPulseTime0 = 0;
    averageCount1 = 0; accumulatedPulseTime1 = 0;
  }
}

//Interrupt routine 0 - PV production
void onPulse0()
{
  //pulseCounters
  pulseCount0++; averageCount0++;

  //used to measure time between pulses.
  lastTime0 = pulseTime0;
  pulseTime0 = micros();
  deltaTime0 = pulseTime0 - lastTime0;
  accumulatedPulseTime0 += deltaTime0;

  //Calculate power
  power0 = 3600000000.0 / (deltaTime0 * ppwh0);
  averagedPower0 = (3600000000.0 * averageCount0) / (accumulatedPulseTime0 * ppwh0);
  //  }
}

//Interrupt routine 1 - Zweirichtungszaehler
void onPulse1()
{
  //pulseCounters
  pulseCount1++; averageCount1++;

  //used to measure time between pulses.
  lastTime1 = pulseTime1;
  pulseTime1 = micros();
  deltaTime1 = pulseTime1 - lastTime1;
  accumulatedPulseTime1 += deltaTime1;

  //Calculate power
  power1 = 3600000000.0 / (deltaTime1 * ppwh1);
  averagedPower1 = (3600000000.0 * averageCount1) / (accumulatedPulseTime1 * ppwh1);
  //  }
}

// This function returns a string with the current date
String getDate() {
  String result;
  Process time;
  // date is a command line utility to get the date and the time
  // in different formats depending on the additional parameter
  time.begin("date");
  time.addParameter("+%Y-%m-%d");  // parameters: D for the complete date mm/dd/yy, T for the time hh:mm:ss
  time.run();  // run the command

  // read the output of the command
  while (time.available() > 0) {
    char c = time.read();
    if (c != '\n')
      result += c;
  }

  return result;
}

// This function returns a string with the time stamp
String getTimeStamp() {
  String result;
  Process time;
  // date is a command line utility to get the date and the time
  // in different formats depending on the additional parameter
  time.begin("date");
  time.addParameter("+%Y-%m-%d %T");  // parameters: D for the complete date mm/dd/yy, T for the time hh:mm:ss
  time.run();  // run the command

  // read the output of the command
  while (time.available() > 0) {
    char c = time.read();
    if (c != '\n')
      result += c;
  }

  return result;
}


// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Data logging and time stamping from http://arduino.cc/en/Tutorial/YunDatalogger
