/*
  Input_Output
  This program is to simplify your morning routine. It uses a light sensor to detect when the sun rises, a water sensor to monitor the water level of the plant-pot, and a servo motor to open the dog's feeding bowl. Also, it plays a morning song and sends a "Good Morning" email to start your day better.

  The circuit:
  input devices - Light Sensor, Water Sensor, DHT11 Sensor
  output devices - Servo Motor, WiFi connection


  Created By:
  Liron_Cohen #323929992
  Ariel_Jankelowitz #345537450
  Junil_Lee #000805387
*/
const int lightSensorPin = 34;
const int waterSensorPin = 27;
const int lightThreshHold = 2000;
const int waterThreshHold = 2250;
const int motorPin = 13;

#define BLYNK_TEMPLATE_ID "TMPL6GtOX9s6H"
#define BLYNK_TEMPLATE_NAME "FinalProject"
#define BLYNK_AUTH_TOKEN "vor-igsfLDKRcodjPQrOeQQ7njTyKH06"

#include <WiFi.h>
#include <HTTPClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Adafruit_Sensor.h>
#include <ESP32Servo.h>

// DHT
#define DHTPIN 15
#define DHTTYPE DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);

// Replace with your Wi-Fi credentials
const char* ssid = "Ariel";         
const char* password = "12345678"; 

// Replace with your laptop's IP address
const char* serverName = "http://192.168.231.103:8080/esp32-connect";
bool isDay = false;

Servo servo;


void setup() {
  Serial.begin(115200);
  pinMode(lightSensorPin, INPUT);
  pinMode(waterSensorPin, INPUT);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
  servo.attach(motorPin);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("\nConnected to the WiFi network");
}

void loop() {
  Blynk.run();
  int lightSensorValue = analogRead(lightSensorPin);
  int waterSensorValue = analogRead(waterSensorPin);

  // Read DHT sensor and Update Blynk app
  sensors_event_t tempEvent, humidityEvent;
  dht.temperature().getEvent(&tempEvent);
  dht.humidity().getEvent(&humidityEvent);
  
  // 
  if (!isnan(tempEvent.temperature)) {
  Blynk.virtualWrite(V1, tempEvent.temperature);
  }
   
  if (!isnan(humidityEvent.relative_humidity)) {
  Blynk.virtualWrite(V2, humidityEvent.relative_humidity);
  }
  Serial.println(lightSensorValue);
  if ((lightSensorValue < lightThreshHold) && (!isDay)) {
    playMusic(); 
    isDay = true; 
    servo.write(120);
  }

  if ((lightSensorValue >= lightThreshHold) && (isDay)) { 
    isDay = false; 
    servo.write(0);
  }

  if (waterSensorValue < waterThreshHold) {
      Blynk.logEvent("waterplants");
  }
  

  delay(1000);
}

void playMusic() {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.begin(serverName); // Specify the URL of the Flask server
    int httpResponseCode = http.GET(); // Send the HTTP GET request

  if (httpResponseCode > 0) {
      // Check the HTTP response code
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
      
      // Get the response payload
      String payload = http.getString();
      Serial.println("Response from server: " + payload);
    }
   else {
      Serial.print("Error code: ");
      Serial.println(httpResponseCode);
    }
    http.end(); // Free resources
  }
}
