#include <WiFi.
h>
#include <Wire.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#define TRIGGER_PIN 4 //D1
#define ECHO_PIN 2 //D2
// Wifi network station credentials
#define WIFI_SSID "Infinix"
#define WIFI_PASSWORD "W1ld@nnraa"
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "6333090330:AAFzB7HdO1uCAeVmJNucU4zU16UkjF9B3Ik"
// Use @myidbot (IDBot) to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
#define CHAT_ID "-938213490"
const char* host = "[Link]";
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
WiFiClient client;
const int httpPort = 80;
String url;
long duration, distance;
unsigned long timeout;
int h2, ht ;
void setup() {
[Link](115200);
[Link]();
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
ht = 20;
// attempt to connect to Wifi network:
[Link]("Connecting to Wifi SSID ");
[Link](WIFI_SSID);
[Link](WIFI_SSID, WIFI_PASSWORD);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for [Link]
while ([Link]() != WL_CONNECTED)
[Link](".");
delay(500);
[Link]("\nWiFi connected. IP address: ");
[Link]([Link]());
[Link]("Retrieving time: ");
configTime(0, 0, "[Link]"); // get UTC time via NTP
time_t now = time(nullptr);
while (now < 24 * 3600)
[Link](".");
delay(100);
now = time(nullptr);
[Link](now);
[Link](CHAT_ID, "Bot started up", "");
void baca_jarak(){
digitalWrite(TRIGGER_PIN, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(TRIGGER_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration/2) / 29.1;
h2 = ht - distance;
[Link](h2);
[Link](" cm");
void peringatan(){
void loop() {
if (h2 >= 10 && h2 < 15) {
String message = "Peringatan Siaga 1: Ketinggian air " + String(h2) + " cm!";
[Link](CHAT_ID, message, "");
}
else if (h2 >= 5 && h2 < 10) {
String message = "Peringatan Siaga 2: Ketinggian air " + String(h2) + " cm!";
[Link](CHAT_ID, message, "");
else if (h2 >= 0 && h2 < 5) {
String message = "Peringatan Siaga 3: Ketinggian air " + String(h2) + " cm!";
[Link](CHAT_ID, message, "");
[Link]("baca jarak ");
baca_jarak();
[Link]("connecting to ");
[Link](host);
if () {
[Link]("connection failed");
//return;
// We now create a URI for the request
url = "/belajar/[Link]/simpan/sensor?data=";
url += h2;
[Link]("Requesting URL: ");
[Link](url);
// This will send the request to the server
[Link](String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
timeout = millis();
while ([Link]() == 0) {
if (millis() - timeout > 5000) {
[Link](">>> Client Timeout !");
[Link]();
return;
// Read all the lines of the reply from server and print them to Serial
while([Link]()){
String line = [Link]('\r');
[Link](line);
[Link]();
[Link]("closing connection");
[Link]();
delay(12000);