[SOLVED] Logging time with Blynk

Hi,

The code below turns a widget LED ON every time a relay contact is shorted to ground (negative pulse on pin 2 of the Yun board).

When the LED is turned on, datetime is also stored onto a SD card. Because the relay is monitored every 5 seconds, large data is stored by the end of the day even though the relay is not actuated often.

What I’m after is to know how long the LED is turned on for. Having said that, is there a way I can log the time when the LED is turned ON and when it’s turned OFF using Blynk?

TIA

    //#define BLYNK_DEBUG // Optional, this enables lots of prints
    //#define BLYNK_PRINT Serial // Comment this out to disable prints and save space

    #include <FileIO.h>
    #include <BlynkSimpleYun.h>
    #include <SimpleTimer.h>

    /* used by Blynk */
    SimpleTimer timer;

    /* Blynk auth token */
    char auth[] = "6e";

    int const contactSPin = 2;

    WidgetLED ledS(V2);

    String dataString;  //string to be saved on SD card

    void setup()
    {
      Bridge.begin();
      Console.begin();
      FileSystem.begin();

      pinMode(contactSPin, INPUT_PULLUP);

      Blynk.begin(auth);

      timer.setInterval(5000L, contactSPinTask);
    }

    void loop() {
      timer.run();
      Blynk.run(); // has to be the last line
    }

    void contactSPinTask() {
      bool pinState = (digitalRead(contactSPin) == HIGH);

      if (pinState) {
        ledS.off();
      }
      else {
        ledS.on();
        writeData("S", "ON");
      }
    }

    void writeData(String x, String y) {
      dataString += getDatetimeStamp();
      dataString += " = ";
      dataString += x;
      dataString += ",";
      dataString += y;

      File dataFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_APPEND);
      // If the file opens successfully, write the string to it, close the file,
      // and print the information to the Serial monitor.

      if (dataFile) {
        dataFile.println(dataString);
        dataFile.close();
        Console.println(dataString);
      }
      // If there is a problem opening the file, send an error to the Serial monitor
      else {
        Console.println("error opening datalog.txt");
      }
      dataString = "";  //empty out string
    }

    String getDatetimeStamp() {
      String result;
      Process time;
      time.addParameter("+%D-%T");
      time.run();
      while (time.available() > 0) {
        char c = time.read();
        if (c != '\n')
          result += c;
      }
      return result;
    }

create a static or global variable:
unsigned long rlyOnTimestamp;

when the relay is turned on, assign uptime to this variable:
rlyOnTimestamp = millis();

when the relay is turned off, do
millis() - rlyOnTimestamp
that will be the relay on time in milliseconds. than you can do whatever you want with that value.

@wanek thank you for your reply.

I did try that earlier but the data is been written on every loop when the relay is activated. The activation may last between 30 and 50 minutes. Therefore, rlyOnTimestamp will retain the last millis before the relay turns OFF thus, will have incorrect data.

Somehow, I need to store millis only once when the LED is turned ON and then do the arithmetic operation you suggested when it’s turned OFF.
Perhaps I need to find a way to log millis when there a transition between ON to OFF and OFF to ON?

then you should try something like this:

static bool firstInstance = true;

if (digitalRead(relaypin) && firstInstance) {
rlyOnTimestamp = millis();
firstinstance = false;
}

if (!digitalRead(relaypin) && !firstInstance) {
onTime = millis() - rlyOnTimestamp;
firstinstane = true;
}

Great! Thanks.

please test and report back if this has solved your problem, than we can mark this case as solved!

thanks!

I updated my test code based on your suggestion and, looking at it, should work however, I’ll not have access to my Yun till Monday.

    //#define BLYNK_DEBUG // Optional, this enables lots of prints
    //#define BLYNK_PRINT Serial // Comment this out to disable prints and save space

    #include <FileIO.h>
    #include <BlynkSimpleYun.h>
    #include <SimpleTimer.h>

    /* used by Blynk */
    SimpleTimer timer;

    /* Blynk auth token */
    char auth[] = "6e";

    int const contactSPin = 2;

    WidgetLED ledS(V2);

    String dataString;  //string to be saved on SD card
    static bool firstInstance = true;
    unsigned long rlyOnTimestamp;

    void setup()
    {
      Bridge.begin();
      Console.begin();
      FileSystem.begin();

      pinMode(contactSPin, INPUT_PULLUP);

      Blynk.begin(auth);

      timer.setInterval(5000L, contactSPinTask);  //5 seconds timer
    }

    void loop() {
      timer.run();
      Blynk.run(); // has to be the last line
    }

    void contactSPinTask() {
      bool pinState = (digitalRead(contactSPin) == HIGH);

      if (pinState) {
        ledS.off();
        if (!firstInstance) {
          rlyOnTimestamp = millis() - rlyOnTimestamp;
          firstInstance = true;
          writeData("S", "OFF", rlyOnTimestamp);
        }
      }
      else {
        ledS.on();
        if (firstInstance) {
          rlyOnTimestamp = millis();
          firstInstance = false;
          writeData("S", "ON", rlyOnTimestamp);
        }
      }
    }

    void writeData(String x, String y, unsigned long z) {
      dataString += getDatetimeStamp();
      dataString += " = ";
      dataString += x;
      dataString += ",";
      dataString += y;
      dataString += ",";
      dataString += z/1000; //seconds LED ON

      File dataFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_APPEND);
      // If the file opens successfully, write the string to it, close the file,
      // and print the information to the Serial monitor.

      if (dataFile) {
        dataFile.println(dataString);
        dataFile.close();
        Console.println(dataString);
      }
      // If there is a problem opening the file, send an error to the Serial monitor
      else {
        Console.println("error opening datalog.txt");
      }
      dataString = "";  //empty out string
    }

    String getDatetimeStamp() {
      String result;
      Process time;
      time.addParameter("+%D-%T");
      time.run();
      while (time.available() > 0) {
        char c = time.read();
        if (c != '\n')
          result += c;
      }
      return result;
    }

@wanek worked just fine. Thank you!!

1 Like