#define BLYNK_TEMPLATE_ID "TMPL64PCroLaO"
#define BLYNK_TEMPLATE_NAME "IoT Garden Monitoring System"
#define BLYNK_AUTH_TOKEN "rXyRWWBunZ95j7BEK7JZTVmWGiw9hJkK"
#define BLYNK_PRINT Serial
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASS ""
const char* discordWebhook = "";
#include <WiFi.h>
#include <HTTPClient.h>
#include <BlynkSimpleEsp32.h>
#include <NewPing.h>
#define TRIG_PIN 12
#define ECHO_PIN 14
#define BUZZER_PIN 19
NewPing sonar(TRIG_PIN, ECHO_PIN, 200);
bool buzzer_enabled = true;
BLYNK_WRITE(V0) {
buzzer_enabled = param.asInt();
if (buzzer_enabled) {
digitalWrite(BUZZER_PIN, HIGH);
Blynk.virtualWrite (V0, 1);
} else {
digitalWrite(BUZZER_PIN, LOW);
Blynk.virtualWrite (V0, 0);
}
}
void sendSensor(){
unsigned int distance = sonar.ping_cm();
Blynk.virtualWrite (V1, distance);
if (buzzer_enabled == true && distance > 0 && distance < 200) {
sendDiscordNotification("ADA HAMAAAAAAAAA!!!");
tone(BUZZER_PIN, 9000);
}else {
noTone(BUZZER_PIN);
}
delay(50);
}
void sendDiscordNotification(const String& message) {
HTTPClient http;
http.begin(discordWebhook);
String payload = "{\"content\":\"" + message + "\"}";
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
Serial.println("Notification sent to Discord");
} else {
Serial.print("Error sending notification: ");
Serial.println(httpResponseCode);
}
http.end();
}
void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASS);
Blynk.begin(BLYNK_AUTH_TOKEN, WIFI_SSID, WIFI_PASS);
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
Blynk.run();
sendSensor();
}