/
alarma-esp-telegram-twitter.ino
158 lines (126 loc) · 4.99 KB
/
alarma-esp-telegram-twitter.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Librería WiFiManager https://github.com/tzapu/WiFiManager
// Comunicación con Telegram https://create.arduino.cc/projecthub/Arduino_Genuino/telegram-bot-library-ced4d4
// Twitter + Arduino arduino-tweet.appspot.com
// Si necesitas ayuda con este código escríbeme en twitter.com/spamloco
#include <ESP8266WiFi.h>
#include <Twitter.h>
#include <DNSServer.h> //Servidor DNS local para redireccionar peticiones al portal de configuración
#include <ESP8266WebServer.h> //Webserver local para el portal de configuración
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <SPI.h>
#include <TelegramBot.h>
byte sensorPin = 2; // Pin para el sensor de movimiento
byte movimiento = 0; // Variable para activar alarma
byte sinmovimiento = 0; // Resetea el contador a 0 cuando no hay movimiento durante un tiempo
long Numerorandom; // Número ramdom para enviar por Twitter
char msg[140]; // Mensaje para Twitter
Twitter twitter("xxxxxxxxxxxxxxxxxxxxxxxx"); // Token de Twitter se obtiene de arduino-tweet.appspot.com
int repiteloop = false;
const char* BotToken = "xxxxxxxxxxxxxxxxxxx"; // Token de Telegram se obtiene configurando el Bot
WiFiClientSecure client;
TelegramBot bot(BotToken,client);
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("Accediendo al Modo de Configuración");
Serial.println(WiFi.softAPIP());
//if you used auto generated SSID, print it
Serial.println(myWiFiManager->getConfigPortalSSID());
}
//flag para guardar datos
bool shouldSaveConfig = false;
//callback para guardar datos
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
void setup()
{
Serial.begin(115200);
delay(10);
pinMode(sensorPin,INPUT);
WiFiManager wifiManager;
wifiManager.setAPCallback(configModeCallback); //Cuando la conexión al WiFi falla, se entra en el modo AP
wifiManager.setConnectTimeout(30); // Se da un tiempo de 30 segundos para iniciar la conexion a la red WiFi
wifiManager.autoConnect("ESP01-SpamLoco"); // Nombre de la red WiFi creada por la ESP8266-01
// Se intenta una autoconexión y si falla se espera por una configuración
if(!wifiManager.autoConnect()) {
Serial.println("failed to connect and hit timeout");
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(1000);
}
// Conexión a la red wifi exitosa
Serial.println("Conectado de forma exitosa a la red WiFi");
//set config save notify callback
wifiManager.setSaveConfigCallback(saveConfigCallback);
}
void loop()
{
message m = bot.getUpdates(); // A la espera de nuevos mensajes de Telegram
// Si el mensaje es Alarmaon se activa la alarma, es sensible a mayúsculas
if (m.text.equals("Alarmaon"))
{
Serial.println("Mensaje recibido se activa la alarma");
bot.sendMessage(m.chat_id, "La Alarma está preparada y lista.");
movimiento = 0;
repiteloop = false;
while (!repiteloop)
{
byte state = digitalRead(sensorPin);
if(state == 1) {
Serial.println("Se ha detectado movimiento!");
delay(50);
movimiento++;
Serial.println(movimiento);
}
else if (state == 0) {
delay(10);
sinmovimiento++;
Serial.println("Sin movimiento!");
}
// Se resetea el contador de movimientos si no hay movimiento continuo
if (sinmovimiento == 10) {
sinmovimiento = 0;
movimiento = 0;
}
// Se activa la alarma y se envía el mensaje a Telegram
if (movimiento == 3)
{
bot.sendMessage(m.chat_id, "Alarma SpamLoco activada por movimiento!!"); //Mensaje de alerta Telegram
// Envía el Tweet junto a un número random para que los tweets sean diferentes
// Si son siempre iguales Twitter los bloquea
Numerorandom = random(101,1565);
Serial.println(Numerorandom);
sprintf(msg, "Alarma spamloco activada!: %d.", Numerorandom);
Serial.println(msg);
Serial.println("Conectando a Twitter…");
if (twitter.post(msg)) {
int status = twitter.wait(&Serial);
if (status == 200) {
Serial.println("200 OK");
delay(60001); // Si el mensaje se envía se espera 1 minuto para evitar spam en arduino-tweet.appspot.com
}
else {
Serial.print("Error : code ");
Serial.println(status);
}
} else {
Serial.println("Fallo en la conexión.");
}
movimiento = 0; // Variable de movimientos vuelve a cero
}
message m = bot.getUpdates(); // Leer nuevos mensajes de Telegram
if (m.text.equals("Alarmaoff"))
{
repiteloop = true;
Serial.println("Mensaje recibido, se apaga la alarma.");
bot.sendMessage(m.chat_id, "La Alarma está desactivada.");
}
}
}
else if (m.text.equals("Alarmaoff"))
{
Serial.println("Mensaje recibido, se apaga la alarma.");
bot.sendMessage(m.chat_id, "La Alarma está desactivada.");
repiteloop = false;
}
} // fin loop