Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

Advanced Button

This example will:

Start a time entry when a momentary button is pressed.

A LED will also light up while the timer is running!

The timer will stop when the button is pressed a second time.

The LED will also turn of once the timer is stopped!

The LED will also light up if a timer is started/ running through the website. The LED will also turn off if the timer is stopped remotely.

It is also able to stop a timer that is started through the website.

NOTE: This example is slower than the simple button but adds a fluid transition between the website and the independent "ESP Button".
/*
This is an "advanced" version of the Button example.

It uses a momentary button to start and stop a timer.
The timer will start if no timer is active, otherwise it will stop the timer.

A LED will light up and indicate if a timer is started or running.
The LED will turn of if the timer is stopped either through a button press or the website.

*/

#include <Toggl.h>

const char* SSID = "SSID";
const char* PASS = "PASSWORD";

// Toggl information
int const PID{123456789};                   //Project ID is specific to each user project
String const Token{"TOKEN"};                //API Token is found in "Profile Settings" 

char const Description[]{"HelloWorld"};     // Timer description
char const TagName[]{" "};                  // Timer tag name



const int ButtonPin = D7;
const int LedPin =  D8;

bool TOGGLE = true;
bool BUTTON = false;
bool TimerActive = false;


void ICACHE_RAM_ATTR HandleInterrupt();


Toggl toggl;


void setup(){

  pinMode(LedPin, OUTPUT);
  pinMode(ButtonPin, INPUT);
  digitalWrite(LedPin, LOW);
  Serial.begin(115200);
  
  attachInterrupt(digitalPinToInterrupt(ButtonPin), HandleInterrupt, FALLING); // Triggers on falling edge, change it to "RISING" depending on your button.
  
  toggl.init(SSID,PASS);
  toggl.setAuth(Token);
    
}


void HandleInterrupt() {
  BUTTON = true;
}



void loop(){

  TimerActive = toggl.isTimerActive();

  // Button is pressed and the timer starts
  if(TOGGLE == true && BUTTON == true && TimerActive == false){
    digitalWrite(LedPin, HIGH);
    toggl.StartTimeEntry(Description, TagName, PID, "ESP");
    TimerActive = true;
    TOGGLE = false;
    BUTTON = false;   
  }

  // Button is pressed and the timer stops
  if (TOGGLE == false && BUTTON == true && TimerActive == true){
    digitalWrite(LedPin, LOW);
    toggl.StopTimeEntry(toggl.getTimerID());
    TimerActive = false;
    TOGGLE = true;
    BUTTON = false;
  }

  // Keeping LED active if timer is running
  else if(TimerActive == true){
    digitalWrite(LedPin, HIGH);
    TOGGLE = false;
  }
  
  // Keeping LED off if timer is not running
  else if (TimerActive == false){
    digitalWrite(LedPin, LOW);
    TOGGLE = true;   
  }
}