// First Partner Code: This code connects an Adafruit CPX board with an ESP8266 Wi-Fi module to the Blynk IoT platform.
// It monitors sound levels and temperature, sending data to Blynk for remote tracking.
// If sound exceeds a threshold, red LEDs flash; if temperature exceeds limits, blue LEDs flash.
// Users can activate custom alarm melodies and LED patterns via the Blynk app. The left button assures all is well. 

// link: https://www.instructables.com/Monitoring-the-Needs-and-Desires-of-the-Dog/

#define BLYNK_TEMPLATE_ID           "TMPL6Jzqs5WCC"
#define BLYNK_TEMPLATE_NAME         "My CPX"
#define BLYNK_AUTH_TOKEN            "8PyXK2UUaC-srWWK20R_xS8bW9NLD5NO"

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT SerialUSB



#include <ESP8266_Lib.h>
#include <Adafruit_CircuitPlayground.h>
#include <BlynkSimpleShieldEsp8266.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "HOTWiFi-36FF";
char pass[] = "20304050";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);

#define SENSOR_INTERVAL 3000 // Interval for reading the sensor (10 seconds)
int highFreq = 1500;
int lowFreq = 500;
int alramCheck = 0;
int alarmOn = 0;

void setup() {
  // put your setup code here, to run once:
  // Debug console
  SerialUSB.begin(115200);
  Serial.begin(115200);
  CircuitPlayground.begin();
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);

  delay(10);

  Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass);
}
unsigned long previousSensorMillis = 0;
unsigned long lastSent;
float tempC;
void loop() {
  // put your main code here, to run repeatedly:
  Blynk.run();
  readTemperature();
  int value = 0;
  value=CircuitPlayground.mic.soundPressureLevel(10);
  Blynk.virtualWrite(V0,value);
  Blynk.virtualWrite(V2,tempC);
  if(value > 90){
    Blynk.virtualWrite(V3,"Alma is Hungry!!!");
    alarmEffect1();
  }
  if(CircuitPlayground.leftButton()){
    Blynk.virtualWrite(V3,"All good..");
    }
}

void readTemperature() {
  unsigned long currentMillis = millis();

  // Check if it's time to read the temperature
  if (currentMillis - previousSensorMillis >= SENSOR_INTERVAL) {
    previousSensorMillis = currentMillis;

    // Read the temperature from the onboard sensor
    float tempC = CircuitPlayground.temperature();

    // Check if the measurement was successful
    if (isnan(tempC)) {
      Serial.println("Error reading temperature!");
    } else {
      // Print the temperature value in Celsius
      Serial.print("Temperature: ");
      Serial.print(tempC);
      Serial.println(" °C");
      // Send the temperature value to Blynk
      if(tempC > 0){
      Blynk.virtualWrite(V2, tempC);
      }
          if(tempC > 34.5){
          Blynk.virtualWrite(V3,"You Need To Cool Her Wather!!!");
          alarmEffect2();
          }
          if(tempC <27.5){
          Blynk.virtualWrite(V3,"All good..");
          }
    }
  }
}
void alarmEffect1() {
  
  
  for (int i = 0; i < 30; i++) { // Blink the LEDs for 3 cycles
    // Turn on all the NeoPixels to RED (255, 0, 0)
    for (int j = 0; j < 10; j++) {
      CircuitPlayground.setPixelColor(j, 255, 0, 0);
      delay(10);
    }
    
  
    // Turn off all the NeoPixels
    for (int j = 0; j < 10; j++) {
      CircuitPlayground.setPixelColor(j, 0, 0, 0);
    }
    
    
  }
}
void alarmEffect2() { 
  for (int i = 0; i < 30; i++) { // Blink the LEDs for 3 cycles
    // Turn on all the NeoPixels to RED (255, 0, 0)
    for (int j = 0; j < 10; j++) {
      CircuitPlayground.setPixelColor(j, 0, 0, 255);
      delay(10);
    }

    // Turn off all the NeoPixels
    for (int j = 0; j < 10; j++) {
      CircuitPlayground.setPixelColor(j, 0, 0, 0);
    }
    
    
  }
}
BLYNK_WRITE(V5)
{
  Serial.println("This is the checking part!*");
  alarmOn = param.asInt();
  if (alarmOn >= 5){
    for (int i=0; i < 10; i++) {
    CircuitPlayground.setPixelColor(i, 200,70,70);
    }
  }
  CircuitPlayground.playTone(523, 50);
  CircuitPlayground.playTone(587, 50);
  CircuitPlayground.playTone(659, 50);
  CircuitPlayground.playTone(698, 100);
  CircuitPlayground.playTone(784, 50);
  CircuitPlayground.playTone(880, 50);
  CircuitPlayground.playTone(987, 50);
  CircuitPlayground.playTone(1046, 100);
  CircuitPlayground.playTone(1174, 50);
  CircuitPlayground.playTone(1318, 50);
  CircuitPlayground.playTone(1396, 200);

  CircuitPlayground.clearPixels();

  delay(500);  
    {

    delay(100);
    CircuitPlayground.clearPixels();
    CircuitPlayground.playTone(lowFreq, 100);
    delay(100);
  }

  Serial.println(param.asString());
}
