-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTelegramAlert.ino
58 lines (46 loc) · 1.7 KB
/
TelegramAlert.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <TelegramBot.h>
const int trigPin = 9; //trigPin is connected to pin 9
const int echoPin = 10; //echoPin is connected to pin 10
// defines variables
long duration;
int distance;
const char* ssid = "<Your WiFi Name or SSID>"; //Enter the WiFi name where you need to connect
const char* password = "<Your WiFi Password>"; //Enter the password as well
const char BotToken[] = "<Enter the token which you got using bot father>"; //Bot token which is the token used to send alert message in telegram
WiFiClientSecure net_ssl;
TelegramBot bot (BotToken, net_ssl);
void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200);
while (!Serial) {} //Start running when the serial is open
delay(3000);
Serial.print("Connecting WiFi.");
Serial.println(ssid);
while (WiFi.begin(ssid, password) != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
bot.begin(); // Begins the bot in telegram
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
distance= duration*0.034/2; // Calculating the distance
//Checking the distance is less than 6 cm, if it so, then alert message will send to the telegram channel
if (distance<6)
{
bot.sendMessage(m.chat_id, "Dustbin is full and it needs maintance");
}
}