#include <UniversalTelegramBot.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <SoftwareSerial.h> // Software Serial library
#include "NewPing.h"
#define ECHOPIN D2// Pin to receive echo pulse
#define TRIGPIN D1// Pin to send trigger pulse
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
boolean SendingMessage = true;
boolean SendingMessage2 = true;
NewPing sonar(TRIGPIN, ECHOPIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
long duration; // declare duration as long. Long can store a bigger number than int.
int dist = 0; // declare dist as integer
//------- WiFi Settings -------
char ssid[] = ""; // your network SSID (name)
char password[] = ""; // your network key
// ------- Telegram config --------
#define BOT_TOKEN "" // your Bot Token (Get from Botfather)
#define CHAT_ID "" // Chat ID of where you want the message to go (You can use MyIdBot to get the chat ID)
// SSL client needed for both libraries
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
String ipAddress = "";
volatile bool telegramButton1PressedFlag = false;
volatile bool telegramButton2PressedFlag = false;
void setup() {
Serial.begin(9600);
pinMode(TRIGPIN, OUTPUT); // Sets the trigPin as an Output
pinMode(ECHOPIN, INPUT);// Sets the echoPin as an Input
digitalWrite(ECHOPIN, HIGH);
// Set WiFi to station mode and disconnect from an AP if it was Previously
// connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
IPAddress ip = WiFi.localIP();
Serial.println(ip);
ipAddress = ip.toString();
}
void telegramButton1Pressed() {
Serial.println("Telegram Button-1 Pressed");
int echo = digitalRead(ECHOPIN );
if(dist <10 && SendingMessage)
{
telegramButton1PressedFlag = true;
SendingMessage = false;
telegramButton2PressedFlag = true;
}if (dist >10 && SendingMessage2)
SendingMessage2 = true;
return;
}
void sendTelegramMessage1() {
String message = "Dustbin is full,needed to clean up!";
if(bot.sendMessage(CHAT_ID, message, "Markdown")){
Serial.println("TELEGRAM Message 1 Successfully sent");
}
telegramButton1PressedFlag = false;
}
void telegramButton2Pressed() {
Serial.println("Telegram Button-2 Pressed");
int echo = digitalRead(ECHOPIN );
if(dist >10 && SendingMessage2)
{
telegramButton2PressedFlag = true;
SendingMessage2 = false;
}
return;
}
void sendTelegramMessage2() {
String message2 = "Dustbin is been clean up!";
if(bot.sendMessage(CHAT_ID, message2, "Markdown")){
Serial.println("TELEGRAM Message 2 Successfully sent");
}
telegramButton2PressedFlag = false;
}
void loop() {
dist = sonar.ping_cm();
Serial.print(dist);
Serial.println(" cm");
delay(100);
if (dist <10 && SendingMessage) {
Serial.println("sent notifcation1 to Telegram");
sendTelegramMessage1();
SendingMessage = false;
if(dist >10 && SendingMessage2){
Serial.println("sent notifcation2 to Telegram");
sendTelegramMessage2();
SendingMessage2 = false;
}
}
}