#include <ESP8266_Lib.h>

#include <Adafruit_CircuitPlayground.h>

#define BLYNK_PRINT Serial

#include <ESP8266_Lib.h>

#include <BlynkSimpleShieldEsp8266.h>

#include <Servo.h>

//blynk key
char auth[] = "YyGZSAEncyEdKN6isF6TYQfAGFcKNEsQ";

//this is my  network info, have fun with it i guess
char ssid[] = "bobi";
char pass[] = "0548062686";
//creating variables for the light level in the room, the temprture in the room, the info from the app on the state of the light switch and the fan switch, teh previes state of the switches and a variable for door closed detection
int light_level, tempC, light, fan, Plight, Pfan, tap = 0;
//a variable for keeping track of the temprature notification
bool temp_reset = true;
//network settings
#define EspSerial Serial1
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
//defining servos
Servo Slight;
Servo Sfan;
//reading light switch from the app
BLYNK_WRITE(V2)
{
  light = param.asInt(); // assigning incoming value from pin V1 to a variable
}
//reading fan switch from the app
BLYNK_WRITE(V3)
{
  fan = param.asInt(); // assigning incoming value from pin V1 to a variable
}
//closing door detaction
void tapTime(void) {
  Serial.print("Tap! ");
  Serial.println(millis()); // the time
  tap = 1;
}

void setup() {
  Serial.begin(9600);
  //settings for the board
  CircuitPlayground.begin();
  CircuitPlayground.setAccelRange(LIS3DH_RANGE_2_G);
  CircuitPlayground.setAccelTap(1, 80);
  //network settings
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  //servo settings
  Slight.attach(10);
  Sfan.attach(9);
  //more network settings
  Blynk.begin(auth, wifi, ssid, pass);
  //setting a door close as an interrupt
  attachInterrupt(digitalPinToInterrupt(CPLAY_LIS3DH_INTERRUPT), tapTime, FALLING);
}

void loop() {
  Blynk.run();
  //reading  the sensors
  light_level = CircuitPlayground.lightSensor();
  tempC = CircuitPlayground.temperature();
  //sending sensors info to the app
  Blynk.virtualWrite(V0, light_level);
  Blynk.virtualWrite(V1, tempC);
  //if the door is closed and the light is still on it sendes a notification to the phone
  if (tap == 1) {
    if (light_level >= 10) {
      Blynk.notify("hey hey! the door is closed and the light is on");
      delay(500);
    }
    tap = 0;
  }
  //sending a notification to the phone if the temprature is higher than 28C
  if (tempC > 28 && temp_reset) {
    Blynk.notify("hey hey! its a bit hot, would you like to turn on the fan?");
    temp_reset = false;
  }
  //allows the arduino to send a temprature notification to the phone again only after the room cools down to 25C
  if (tempC <= 25) {
    temp_reset = true;
  }
  //turning light on
  if (light == 1 && Plight != light) {
    Slight.write(0);
    delay(500);
    Slight.write(90);
    Serial.println(light);
    Plight = light;
  }
  //turning light off
  if (light == 0 && Plight != light) {
    Slight.write(180);
    delay(500);
    Slight.write(90);
    Plight = light;
  }
  //turning fan on
  if (fan == 1 && Pfan != fan) {
    Sfan.write(180);
    delay(500);
    Sfan.write(90);
    Pfan = fan;
  }
  //turning fan off
  if (fan == 0 && Pfan != fan) {
    Sfan.write(0);
    delay(500);
    Sfan.write(90);
    Pfan = fan;
  }
  delay(10);
}

/*
    .----.   @   @
   / .-"-.`.  \v/
   | | '\ \ \_/ )
 ,-\ `-.' /.'  /
'---`----'----'
*/
