-
Notifications
You must be signed in to change notification settings - Fork 43
/
PushNotificaitonDemo.ino
136 lines (110 loc) · 3.91 KB
/
PushNotificaitonDemo.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*******************************************************************
* Push Notificaiton Demo *
* Using IFTTT and Telegram to send alerts to your phone *
* *
* Telegram Library: *
* https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot *
* *
* IFTTT Maker Library: *
* https://github.com/witnessmenow/arduino-ifttt-maker *
* *
* By Brian Lough *
*******************************************************************/
//Including the two libraries
#include <UniversalTelegramBot.h>
#include <IFTTTMaker.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
//------- WiFi Settings -------
char ssid[] = "xxx"; // your network SSID (name)
char password[] = "yyyy"; // your network key
#define TELEGRAM_BUTTON_PIN D5
#define IFTTT_BUTTON_PIN D6
// ------- IFTTT Maker config --------
#define KEY "zzzzzzzzzzzzzzzzzzzzzzz" // Get it from this page https://ifttt.com/services/maker/settings
#define EVENT_NAME "button_pressed" // Name of your event name, set when you are creating the applet
// ------- Telegram config --------
#define BOT_TOKEN "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
#define CHAT_ID "-128380507" // 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;
IFTTTMaker ifttt(KEY, client);
UniversalTelegramBot bot(BOT_TOKEN, client);
String ipAddress = "";
volatile bool telegramButtonPressedFlag = false;
volatile bool iftttButtonPressedFlag = false;
void setup() {
Serial.begin(115200);
// Initlaze the buttons
pinMode(TELEGRAM_BUTTON_PIN, INPUT);
pinMode(IFTTT_BUTTON_PIN, INPUT);
// NOTE:
// It is important to use interupts when making network calls in your sketch
// if you just checked the status of te button in the loop you might
// miss the button press.
attachInterrupt(TELEGRAM_BUTTON_PIN, telegramButtonPressed, RISING);
attachInterrupt(IFTTT_BUTTON_PIN, iftttButtonPressed, RISING);
// 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 telegramButtonPressed() {
Serial.println("telegramButtonPressed");
int button = digitalRead(TELEGRAM_BUTTON_PIN);
if(button == HIGH)
{
telegramButtonPressedFlag = true;
}
return;
}
void iftttButtonPressed() {
Serial.println("iftttButtonPressed");
int button = digitalRead(IFTTT_BUTTON_PIN);
if(button == HIGH)
{
iftttButtonPressedFlag = true;
}
return;
}
void triggerIftttEvent() {
if(ifttt.triggerEvent(EVENT_NAME, ssid, ipAddress)){
Serial.println("IFTTT Successfully sent");
}
iftttButtonPressedFlag = false;
}
void sendTelegramMessage() {
String message = "SSID: ";
message.concat(ssid);
message.concat("\n");
message.concat("IP: ");
message.concat(ipAddress);
message.concat("\n");
if(bot.sendMessage(CHAT_ID, message, "Markdown")){
Serial.println("TELEGRAM Successfully sent");
}
telegramButtonPressedFlag = false;
}
void loop() {
if ( iftttButtonPressedFlag ) {
triggerIftttEvent();
}
if ( telegramButtonPressedFlag ) {
sendTelegramMessage();
}
}