-
Notifications
You must be signed in to change notification settings - Fork 3
/
ESP2866-Telegram-Messenger.ino
75 lines (63 loc) · 2.22 KB
/
ESP2866-Telegram-Messenger.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#include <stdbool.h>
bool messageSent = false;
bool ledOn = false;
const char* ssid = "WIFI NAME"; // Replace with your wifi name
const char* password = "PASSWORD"; // Replace with your wifi password
#define BOTtoken "TOKEN" // Replace with your telegram Bot Token
#define CHAT_ID "CHAT_ID" // Replace with your Chat ID
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
int buttonPin = 4;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
configTime(0, 0, "pool.ntp.org");
client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
if (messageSent == false && ledOn == true) {
bot.sendMessage(CHAT_ID, "Alert: Adam needs help", "");
messageSent = true;
}
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(100);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// turns on the LED every second button press by checking whether buttonPushCounter can / 2
if (buttonPushCounter % 2 == 0) {
digitalWrite(LED_BUILTIN, HIGH);
ledOn = true;
} else {
digitalWrite(LED_BUILTIN, LOW);
ledOn = false;
messageSent = false;
}
}