-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevel measurement.ino
More file actions
83 lines (60 loc) · 2.54 KB
/
Copy pathLevel measurement.ino
File metadata and controls
83 lines (60 loc) · 2.54 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "CTBot.h"
CTBot myBot;
String ssid = "<ssid>"; // REPLACE mySSID WITH YOUR WIFI SSID
String pass = "<password>"; // REPLACE myPassword YOUR WIFI PASSWORD, IF ANY
String token = "<token>"; // REPLACE myToken WITH YOUR TELEGRAM BOT TOKEN , you can get this token in Botfather
uint8_t led = 2; // the onboard ESP8266 LED.
// If you have a NodeMCU you can use the BUILTIN_LED pin
// (replace 2 with BUILTIN_LED)
const int trigP = 2; //D4 Or GPIO-2 of nodemcu
const int echoP = 0; //D3 Or GPIO-0 of nodemcu
long duration;
int distance;
float per_dis;
String distance_1;
void setup() {
// initialize the Serial
Serial.begin(9600);
Serial.println("Starting TelegramBot...");
pinMode(trigP, OUTPUT); // Sets the trigPin as an Output
pinMode(echoP, INPUT); // Sets the echoPin as an Input
// connect the ESP8266 to the desired access point
myBot.wifiConnect(ssid, pass);
// set the telegram bot token
myBot.setTelegramToken(token);
// check if all things are ok
if (myBot.testConnection())
Serial.println("\ntestConnection OK");
else
Serial.println("\ntestConnection NOK");
// set the pin connected to the LED to act as output pin
pinMode(led, OUTPUT);
digitalWrite(led, HIGH); // turn off the led (inverted logic!)
}
void loop() {
digitalWrite(trigP, LOW); // Makes trigPin low
delayMicroseconds(2); // 2 micro second delay
digitalWrite(trigP, HIGH); // tigPin high
delayMicroseconds(10); // trigPin high for 10 micro seconds
digitalWrite(trigP, LOW); // trigPin low
duration = pulseIn(echoP, HIGH); //Read echo pin, time in microseconds
distance= duration*0.034/2; //Calculating actual/real distance
distance= 122-distance;
// a variable to store telegram message data
TBMessage msg;
Serial.print("\nNew data \n");
Serial.println(distance);
if (distance<=8) {
myBot.sendMessage(msg.sender.id, "Alert : Petrol Bunk 1 needs to refilled"); // notify the sender
}
per_dis=((float)distance/122)*100;
distance_1=String(per_dis);
// if there is an incoming message...
if (myBot.getNewMessage(msg)) {
if (msg.text.equalsIgnoreCase("Bunk 1 Update me")) {
myBot.sendMessage(msg.sender.id,distance_1); // notify the sender
}
}
// wait 500 milliseconds
delay(500);
}