Skip to content

Instantly share code, notes, and snippets.

@vinirodr
Last active June 17, 2016 07:40
Show Gist options
  • Save vinirodr/4a7f07664c726d981ae9d82e3c0e02a0 to your computer and use it in GitHub Desktop.
Save vinirodr/4a7f07664c726d981ae9d82e3c0e02a0 to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "vinirodr";
const char* password = "qwertyui";
IPAddress server(162,243,2,159);
const int httpPort = 80;
WiFiClient client;
const int LED_POS = 13; // D7
const int LED_POS2 = 12; // D6
const int BTN_POS = 4; // D2
const int BTN_POS2 = 15; // D8
const int PINtoRESET = 14; // D5
int pomodoroTime = 10000; // 10 seconds
// 25 min => 1500000
bool statusOk = false;
void setup(){
WiFi.begin(ssid, password);
delay(1000);
pinMode(LED_POS, OUTPUT);
pinMode(LED_POS2, OUTPUT);
pinMode(BTN_POS, INPUT);
attachInterrupt(digitalPinToInterrupt(BTN_POS2), buttonISR, FALLING);
pinMode(PINtoRESET, INPUT);
digitalWrite(PINtoRESET, LOW);
}
void loop(){
if(digitalRead(BTN_POS)) {
// If pressed start timer
startTimer();
sendFinished();
if(statusOk){
// When timer is done, turn off LED
digitalWrite(LED_POS, LOW);
redAlarm();
}
}
// Testing connection
// if(client.connect(server, httpPort)){
// digitalWrite(LED_POS, HIGH);
// } else {
// digitalWrite(LED_POS2, HIGH);
// }
}
void resetTimer(){
sendInterupted();
pinMode(PINtoRESET, OUTPUT);
delay(1000);
}
void startTimer(){
digitalWrite(LED_POS, HIGH);
delay(pomodoroTime);
}
void sendFinished(){
if(client.connect(server, httpPort)){
HTTPClient http;
http.begin("http://162.243.2.159/pomodoro/finished");
http.GET();
statusOk = true;
} else {
digitalWrite(LED_POS, HIGH);
digitalWrite(LED_POS2, HIGH);
client.stop();
}
}
void sendInterupted(){
if(client.connect(server, httpPort)){
HTTPClient http;
http.begin("http://162.243.2.159/pomodoro/interupted");
http.GET();
statusOk = true;
} else {
digitalWrite(LED_POS, HIGH);
digitalWrite(LED_POS2, HIGH);
client.stop();
}
}
void redAlarm(){
for(int i = 0; i < 10; i++){
digitalWrite(LED_POS2, HIGH);
delay(300);
digitalWrite(LED_POS2, LOW);
delay(300);
}
}
void greenAlarm(){
for(int i = 0; i < 10; i++){
digitalWrite(LED_POS, HIGH);
delay(300);
digitalWrite(LED_POS, LOW);
delay(300);
}
}
// Interuption function, resets loop
void buttonISR(){
resetTimer();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment