#define BLYNK_TEMPLATE_ID "TMPL6BDx60xya"
#define BLYNK_TEMPLATE_NAME "BBM"
#define BLYNK_AUTH_TOKEN "pzoHRTmNm-lxjyF4hlkUJK_jhi9Ka52U"
#define BLYNK_PRINT Serial //Kita menggunakan Blynk serial
#include <BlynkSimpleEsp32.h>
#include <UniversalTelegramBot.h>
#include <WiFiClientSecure.h>
#include <SimpleUltrasonic.h>
#include <ArduinoJson.h>
#include <Blynk.h>
#include <WiFi.h> //Library WiFi
#include <WiFiClient.h> //Library WiFiClient
#include <BlynkSimpleEsp32.h> //Library BlynkESP32
char ssid[] = "Telkomsel 5G"; //Nama WiFi yang digunakan
char pass[] = "Telkomsel123"; //Password WiFi yang digunakan
BlynkTimer timer; //Untuk push data dibutuhkan blynk timer (untuk code push data dapat dilihat di blynk example)
#define BOT_TOKEN "7591889335:AAEnmisfXl1O8WWoUFHFWbKpahitY_9btY8"
#define CHAT_ID "7060442428"
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
#define TRIG_PIN 2
#define ECHO_PIN 15
SimpleUltrasonic TRIG_PIN, ECHO_PIN;
long lastTimeSent = 0;
long timerDelay = 60000; // 1 minute delay for sending notifications
int thresholdLevel = 20; // Threshold in cm for BBM level warning
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
// Initialize Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Set up Telegram bot server timeouts and TLS
client.setInsecure();
}
void loop() {
Blynk.run(); // Blynk connection
// Read the level of BBM from ultrasonic sensor
long distance = SimpleUltrasonic.Ranging();
Serial.print("Level BBM: ");
Serial.print(distance);
Serial.println("cm");
// Send BBM level to Blynk app
Blynk.virtualWrite(V1, distance); // V1 is the Virtual Pin for displaying the value in Blynk
// Check if level is below threshold, send Telegram notification
if (distance < thresholdLevel && (millis() - lastTimeSent > timerDelay)) {
sendTelegramNotification(distance);
lastTimeSent = millis(); // Update the last time notification was sent
}
delay(10000); // Update every 10 seconds
}
void sendTelegramNotification(long distance) {
String message = "Warning! BBM level is below the safe threshold: " + String(distance) + " cm.";
Serial.println("Sending Telegram message...");
bot.sendMessage(CHAT_ID, message, "");
}