-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ino
90 lines (76 loc) · 2.92 KB
/
index.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
#define WIFI_SSID "*********"
#define WIFI_PASS "*********"
#define BOT_TOKEN "*************************************"
#define CHAT_ID "****"
#include <FastBot.h>
FastBot bot(BOT_TOKEN);
const int iArm = D2;
const int iMove1 = D3;
const int iMove2 = D4;
bool xStatus = false;
bool motionDetected1 = false;
bool motionDetected2 = false;
/*-----------------------------INTERRUPTS-------------------------------*/
void ICACHE_RAM_ATTR detectsMovement1() {
if (xStatus == true) {motionDetected1 = true;}}
void ICACHE_RAM_ATTR detectsMovement2() {
if (xStatus == true) {motionDetected2 = true;}}
/*-------------------------------SETUP---------------------------------*/
void setup()
{
/*pinMode(iStatus, INPUT);*/
pinMode(iArm, OUTPUT);
pinMode(iMove1, INPUT);
pinMode(iMove2, INPUT);
digitalWrite(iArm, true);
connectWiFi();
bot.attach(newMsg);
attachInterrupt(digitalPinToInterrupt(iMove1), detectsMovement1, FALLING);
attachInterrupt(digitalPinToInterrupt(iMove2), detectsMovement2, FALLING);
bot.sendMessage("\nСоединение с сетью WIFI установлено", CHAT_ID);
bot.sendMessage("IP-адрес: 192.168.88.203", CHAT_ID);
if (xStatus){ bot.sendMessage("Режим: ОХРАНА", CHAT_ID);}
else{ bot.sendMessage("Режим: НАБЛЮДЕНИЕ", CHAT_ID); }
}
/*----------------------------FUN_FASTBOT-----------------------------*/
void newMsg(FB_msg& msg)
{
if (msg.text == "/arm")
{ digitalWrite(iArm, false); xStatus = true;}
else if (xStatus == true and msg.text == "/disarm")
{ xStatus = false; digitalWrite(iArm, true);}
else if (msg.text == "/status")
{ if (xStatus){ bot.sendMessage("Режим: ОХРАНА", msg.chatID);}
else{
bot.sendMessage("Режим: НАБЛЮДЕНИЕ", msg.chatID);
}
if (motionDetected1){ bot.sendMessage("Датчик движения комната: АКТИВЕН", msg.chatID);}
else{
bot.sendMessage("Датчик движения комната: НЕ активен", msg.chatID);
}
if (motionDetected2){ bot.sendMessage("Датчик движения коридор: АКТИВЕН", msg.chatID);}
else{
bot.sendMessage("Датчик движения коридор: НЕ активен", msg.chatID);
}
}
}
/*---------------------------LOOP----------------------------------*/
void loop() {
bot.tick();
if (motionDetected1 == true){ bot.sendMessage("Движение комната!", CHAT_ID); motionDetected1 = false; }
if (motionDetected2 == true){ bot.sendMessage("Движение коридор!", CHAT_ID); motionDetected2 = false; }
}
void connectWiFi() {
delay(2000);
Serial.begin(115200);
Serial.println();
WiFi.begin(WIFI_SSID, WIFI_PASS);
//WiFi.config(ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (millis() > 15000) ESP.restart();
}
Serial.println("Connected");
Serial.println(WiFi.localIP());
}