0% found this document useful (0 votes)
17 views

Coding Rumus

coding robotika

Uploaded by

Aditya Awal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Coding Rumus

coding robotika

Uploaded by

Aditya Awal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#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 = "192.168.43.234";

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() {

Serial.begin(115200);

Serial.println();

pinMode(TRIGGER_PIN, OUTPUT);

pinMode(ECHO_PIN, INPUT);

ht = 20;

// attempt to connect to Wifi network:

Serial.print("Connecting to Wifi SSID ");

Serial.print(WIFI_SSID);

WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org

while (WiFi.status() != WL_CONNECTED)

Serial.print(".");

delay(500);

Serial.print("\nWiFi connected. IP address: ");

Serial.println(WiFi.localIP());

Serial.print("Retrieving time: ");

configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP

time_t now = time(nullptr);

while (now < 24 * 3600)

Serial.print(".");

delay(100);
now = time(nullptr);

Serial.println(now);

bot.sendMessage(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;

Serial.print(h2);

Serial.println(" cm");

void peringatan(){

void loop() {

if (h2 >= 10 && h2 < 15) {

String message = "Peringatan Siaga 1: Ketinggian air " + String(h2) + " cm!";

bot.sendMessage(CHAT_ID, message, "");

}
else if (h2 >= 5 && h2 < 10) {

String message = "Peringatan Siaga 2: Ketinggian air " + String(h2) + " cm!";

bot.sendMessage(CHAT_ID, message, "");

else if (h2 >= 0 && h2 < 5) {

String message = "Peringatan Siaga 3: Ketinggian air " + String(h2) + " cm!";

bot.sendMessage(CHAT_ID, message, "");

Serial.print("baca jarak ");

baca_jarak();

Serial.print("connecting to ");

Serial.println(host);

if (!client.connect(host, httpPort)) {

Serial.println("connection failed");

//return;

// We now create a URI for the request

url = "/belajar/index.php/simpan/sensor?data=";

url += h2;

Serial.print("Requesting URL: ");

Serial.println(url);

// This will send the request to the server

client.print(String("GET ") + url + " HTTP/1.1\r\n" +

"Host: " + host + "\r\n" +


"Connection: close\r\n\r\n");

timeout = millis();

while (client.available() == 0) {

if (millis() - timeout > 5000) {

Serial.println(">>> Client Timeout !");

client.stop();

return;

// Read all the lines of the reply from server and print them to Serial

while(client.available()){

String line = client.readStringUntil('\r');

Serial.print(line);

Serial.println();

Serial.println("closing connection");

Serial.println();

delay(12000);

You might also like