Cameron Frary
Published © GPL3+

IFTTT Connected Timer

Wire up this simple internet-connected countdown timer to get notifications in your busy life.

BeginnerFull instructions provided1 hour3,093
IFTTT Connected Timer

Things used in this project

Story

Read more

Schematics

Countdown_Wiring

Wiring diagram for the IFTTT connected countdown timer.

Code

IFTTT_Countdown.ino

Arduino
Code for the IFTTT connected countdown timer
#include <ESP8266WiFi.h>

// Setting up our variables
int numOfMins = 0; // Number of minutes for the timer
int numOfSecs = 0; // Number of seconds for the timer
int totalMillisecs = 0; // Total number of milliseconds 

int frequency = 500; // YOU CAN SET THIS TO WHATEVER YOU WANT

// Setting up the pin variables
int startPin = 14;
int minutePin = 13;
int secondPin = 12;

int buzzerPin = 2;

// Wifi setup
const char *ssid     = "YOUR_SSID";
const char *password = "YOUR_PASSWORD";

// IFTTT setup
const char *host = "maker.ifttt.com";
const char *privateKey = "YOUR_SECRET_KEY";

void setup() {
  // Setting up the pin modes
  pinMode(startPin, INPUT);
  pinMode(minutePin, INPUT);
  pinMode(secondPin, INPUT);

  pinMode(buzzerPin, OUTPUT);

  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  // Wait for the connection, flashing the LED while we wait
  int led = HIGH;  
  while (WiFi.status() != WL_CONNECTED) {
    delay(200);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void send_event(const char *event){

  Serial.print("Connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("Connection failed");
    return;
  }
  
  // We now create a URI for the request
  String url = "/trigger/";
  url += event;
  url += "/with/key/";
  url += privateKey;
  
  Serial.print("Requesting URL: ");
  Serial.println(url);
  
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");

  // Read all the lines of the reply from server and print them to Serial,
  // the connection will close when the server has sent all the data.
  while(client.connected())
  {
    if(client.available())
    {
      String line = client.readStringUntil('\r');
      Serial.print(line);
    } else {
      // No data yet, wait a bit
      delay(50);
    };
  }
  
  // All done
  Serial.println();
  Serial.println("closing connection");

  client.stop();
}

// Setting the # of minutes on the countdown
void addMins() {
  numOfMins++;
  totalMillisecs = totalMillisecs + (60 * 1000);
  delay(400);
}

// Setting the # of seconds on the countdown
void addSecs() {
  numOfSecs++;
  totalMillisecs = totalMillisecs + 1000;
  delay(400);
}

void confirmTime() {

  // Long buzz for each minute set on the countdown
  for(int mins = 0; mins < numOfMins; mins++){
    tone(buzzerPin, frequency, 500);
    delay(750);
  }

  // Short buzz for each second set on the countdown
  for(int secs = 0; secs < numOfSecs; secs++){
    tone(buzzerPin, frequency, 250);
    delay(500);
  }
}

void countdown() {
  // Countdown 'till 5 secs before "GO"
  Serial.println("Delay started");
  Serial.println();
  Serial.println(totalMillisecs / 1000);
  delay(totalMillisecs - 5000);

  for (int i = 0; i < 5; i++) {
    tone(buzzerPin, frequency, 250);
    delay(1000);
  }

  // Sending event to IFTTT
  send_event("timer_expired");
  
  // Final tone
  tone(buzzerPin, frequency, 2000);
  totalMillisecs = 0;
}

void readPins(){
  if (digitalRead(13)) {
    addMins();
    Serial.println("Minute Added");
  }

  if(digitalRead(12)){
    addSecs();
    Serial.println("Second Added");
  }

  if (digitalRead(14)) {
    confirmTime();
    delay(500);
    countdown();
  }
}

void loop() {
  readPins();
}

Credits

Cameron Frary

Cameron Frary

1 project • 1 follower

Comments