/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL1wGx9V1Z"
#define BLYNK_DEVICE_NAME "temp n humidity"
#define BLYNK_AUTH_TOKEN "6_UuKgUsTqTR_i2ZuGniSO5Thjqbkeke"

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "Safwan_zadora";  // type your wifi name
char pass[] = "safwan5979";  // type your wifi password

#define led2_PIN 4
#define buzzer_PIN 5

WidgetLED motion(V3);  //motion sensor
WidgetLCD lcd(V6);     //motion display lcd
WidgetLED led2(V2);    //led
WidgetLED buzzer(V5);  //buzzer

#include <DHT.h>
BlynkTimer timer;

WidgetLED speed(V4);    // port for blynk V4 Virtual port 4
const byte pin_rpm = 19;
int volatile rev = 0;
int rpm = 0;

unsigned long cur_time, old_time;

#define DHTPIN 2 //Connect Out pin to D2 in NODE MCU
#define DHTTYPE DHT22  
DHT dht(DHTPIN, DHTTYPE);

void isr() {
  rev++;
}
void setup()
{
  // Debug console
  Serial.begin(115200);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  // You can also specify server:
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);

   pinMode(14, INPUT);           //motion sensor
   pinMode(buzzer_PIN, OUTPUT);  //buzzer
   pinMode(led2_PIN, OUTPUT);    //beacon

  //lcd.clear(); //Use it to clear the LCD Widget
  //lcd.print(4, 0, "Hello"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
  //lcd.print(4, 1, "World");
  // Please use timed events when LCD printintg in void loop to avoid sending too many commands
  // It will cause a FLOOD Error, and connection will be dropped
  pinMode(pin_rpm, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(pin_rpm),isr, RISING);
  timer.setInterval(1000L, sensor_motion);

  dht.begin();
  timer.setInterval(100L, sendSensor); 

}

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
    Blynk.virtualWrite(V0, t);
    Blynk.virtualWrite(V1, h);
    Serial.print("Temperature : ");
    Serial.print(t);
    Serial.print("    Humidity : ");
    Serial.println(h);
}

void sensor_motion() 
{
  bool motion=digitalRead(14);
  if (motion==1){
  Serial.println("Motion Detected");
  digitalWrite(5, LOW);	//LED OFF
  digitalWrite(4, LOW);		//BUZZER OFF
  led2.off();
  buzzer.off();
  lcd.clear(); //Use it to clear the LCD Widget
  lcd.print(0, 0, "radar start"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
  
  }
  else{
  Serial.println("No Motion Detected!!!");
  digitalWrite(4, HIGH);	//LED ON
  delay(500);				//TIME DIFFERENT BETWEEN HIGH & LOW
  digitalWrite(5, 1200);			//BUZZER ON TONE(PIN,FREQ)
  delay(500);				//TIME DIFFERENT BETWEEN HIGH & LOW
  digitalWrite(4, LOW);	//LED OFF
  delay(500);				//TIME DIFFERENT BETWEEN HIGH & LOW
  digitalWrite(5, HIGH);				//BUZZER ON
  delay(500);
  digitalWrite(5, LOW);				//BUZZER OFF
  delay(500);
  led2.on();
  buzzer.on();
  //delay(1500);
  lcd.clear(); //Use it to clear the LCD Widget
  lcd.print(0, 0, "radar stop"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
  //Blynk.email("[email protected]", "Alert", "RADAR STOP! PLEASE DO SOMETHING");
  Blynk.logEvent("radar_alert","RADAR STOP! PLEASE CHECK RADAR"); 
  }
}

void loop()
{
  Blynk.run();
  timer.run();
  cur_time = millis();
  if(cur_time - old_time >= 1000) {
    detachInterrupt(digitalPinToInterrupt(pin_rpm)); //mematikan interrupt
    Serial.println(rev);
    speed.setValue(rev);
    rev = 0;
    attachInterrupt(digitalPinToInterrupt(pin_rpm),isr, RISING); //menghidupkan interrupt
    old_time = millis();
    }
}