// Device libraries (Arduino ESP32/ESP8266 Cores)
#include <Arduino.h>
#include <WiFi.h>
#include "CTBot.h"
#include <Servo.h>
static const int servoPin1 = 5; //pin servo
Servo servo1;
int trigPin = 23; //pin triger ultrasonik
int echoPin = 22; //pin echo ultrasonik
long distance;
long duration;
int OPEN = 120; //open angle
int CLOSE = 0; //close angle
#define sensor 15 //pin infrared
#define led 18 //pin led 1
#define led2 19 //pin led 2
CTBot myBot;
String ssid = "xxxx"; //ssid WiFi
String pass = "xxxx"; //password Wifi
String token = "xxxxx"; //token bot telegram
const int id = 123456; //id telegram
void setup()
{
Serial.begin(9600);
servo1.attach(servoPin1);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(sensor, INPUT_PULLUP);
digitalWrite(led, LOW);
digitalWrite(led2, LOW);
servo1.write(0);
Serial.println("Starting TelegramBot...");
myBot.wifiConnect(ssid, pass);
myBot.setTelegramToken(token);
if (myBot.testConnection()) {
Serial.println("Good Connection");
} else {
Serial.println("Bad Connection");
}
myBot.sendMessage(id, "Hello, Welcome to Bot Prototype Package Receiving Box");
Serial.println("Pesan Terkirim");
}
void loop()
{
TBMessage msg;
Serial.println(distance);
//Opening Door
digitalWrite(trigPin, LOW);
delayMicroseconds(1);
digitalWrite(trigPin, HIGH);
delayMicroseconds(1);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.017;
if(distance <= 30 && distance !=0){
myBot.sendMessage(id, "Punten Paket, Mau buka pintu? /bukapintu");
}
//Sinyal paket diterima
if (digitalRead(sensor) == LOW){
digitalWrite(led, HIGH);
delay(500);
myBot.sendMessage(id, "Paket Telah Diterima... Tutup Pintu? /tutupintu");
digitalWrite(led, LOW);
delay(500);
}
if (myBot.getNewMessage(msg, "/bukapintu")) {
servo1.write(OPEN);
delay(100);
digitalWrite(led2, HIGH);
myBot.sendMessage(id, "Pintu dibuka");
}else if (myBot.getNewMessage(msg, "/tutupintu")) {
servo1.write(CLOSE);
delay(100);
digitalWrite(led2, LOW);
myBot.sendMessage(id, "Pintu Ditutup");
}
}
Comments