-
Notifications
You must be signed in to change notification settings - Fork 1
/
GSM_alarm.ino
217 lines (174 loc) · 4.27 KB
/
GSM_alarm.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <String.h>
#include <GSM.h>
#define PINNUMBER ""
#define PHONENUMBER "0038641455792"
#define ALARM_MSG "ALARM"
#define MAX_ALARMS 4
#define KOMORA1_PIN A0
#define KOMORA2_PIN A1
#define KOMORA3_PIN A2
#define KOMORA4_PIN A3
#define ALARM_LED_KOMORA1_PIN 8
#define ALARM_LED_KOMORA2_PIN 9
#define ALARM_LED_KOMORA3_PIN 10
#define ALARM_LED_KOMORA4_PIN 11
#define READY_LED_PIN A5
#define RESET_BTN_PIN 12
//LOW = ALARM ON CURCUIT CLOSED
//HIGH = AlARM ON CURCUIT OPEND
#define ALARM_ON LOW
GSM g_gsmModule;
GSM_SMS g_sms;
unsigned char g_GSMConnected;
unsigned char g_alarmState; //up to 8 allarms (every bit)
unsigned char g_alarmStateSended; //up to 8 allarms (every bit)
unsigned int g_komoraPins[MAX_ALARMS];
unsigned int g_komoraAlarmLedPins[MAX_ALARMS];
unsigned char readButtonWithDelay(int pin);
void clearAllAlarms();
void setAlarm(unsigned char komoraID);
unsigned char checkAlarm();
void setup()
{
Serial.begin(9600);
Serial.println("SMS Messages Sender");
g_alarmState=0x00;
g_komoraPins[0]=KOMORA1_PIN;
g_komoraPins[1]=KOMORA2_PIN;
g_komoraPins[2]=KOMORA3_PIN;
g_komoraPins[3]=KOMORA4_PIN;
g_komoraAlarmLedPins[0]=ALARM_LED_KOMORA1_PIN;
g_komoraAlarmLedPins[1]=ALARM_LED_KOMORA2_PIN;
g_komoraAlarmLedPins[2]=ALARM_LED_KOMORA3_PIN;
g_komoraAlarmLedPins[3]=ALARM_LED_KOMORA4_PIN;
for(int i=0; i<MAX_ALARMS; i++)
{
//set input for alarm switch
pinMode(g_komoraPins[i],INPUT);
digitalWrite(g_komoraPins[i], HIGH);
//setoutputs for led
pinMode(g_komoraAlarmLedPins[i],OUTPUT);
digitalWrite(g_komoraAlarmLedPins[i],LOW);
}
pinMode(READY_LED_PIN, OUTPUT);
pinMode(RESET_BTN_PIN, INPUT_PULLUP);
g_GSMConnected=0;
}
void clearAllAlarms(){
g_alarmState=0x00;
g_alarmStateSended=0x00;
}
unsigned char sendMessage(unsigned char komoraID)
{
if(g_GSMConnected){
if(g_sms.ready()==0){
delay(2000);
return 0;
}
Serial.println("Sending sms");
Serial.print("ready=");
Serial.println(g_sms.ready());
g_sms.beginSMS(PHONENUMBER);
String komoraStr = String(komoraID+1,DEC);
String msg = String(ALARM_MSG + komoraStr);
g_sms.print(msg);
g_sms.endSMS();
Serial.println("sms send");
Serial.print("ready=");
Serial.println(g_sms.ready());
if(g_sms.ready()==1){
g_alarmStateSended = g_alarmStateSended | (0x01<<komoraID);
return 1;
}
return 0;
}
}
void setAlarm(unsigned char komoraID)
{
if(!(g_alarmState & (0x01<<komoraID))){
g_alarmState=g_alarmState | (0x01<<komoraID);
sendMessage(komoraID);
}
}
unsigned char checkReset(){
if(readButtonWithDelay(RESET_BTN_PIN))
clearAllAlarms();
}
unsigned char checkAlarm()
{
for(int i=0; i<MAX_ALARMS; i++){
if(readAlarmWithDelay(g_komoraPins[i])){
setAlarm(i);
}
}
}
unsigned char readAlarmWithDelay(int pin)
{
int state=0;
state=digitalRead(pin);
if(state==ALARM_ON){
delay(100);
state=digitalRead(pin);
if(state==ALARM_ON)
return 1;
}
return 0;
}
unsigned char readButtonWithDelay(int pin)
{
int state=0;
state=digitalRead(pin);
if(state==LOW){
delay(100);
state=digitalRead(pin);
if(state==LOW)
return 1;
}
return 0;
}
void setAlarmLed(){
for(int i=0; i<MAX_ALARMS; i++){
if((g_alarmState & (0x01<<i)))
digitalWrite(g_komoraAlarmLedPins[i], HIGH);
else
digitalWrite(g_komoraAlarmLedPins[i], LOW);
}
}
void checkUnsendAlarms(){
if(g_alarmState==g_alarmStateSended || !g_GSMConnected)
return;
int retry=10;
for(int i=0; i<MAX_ALARMS; i++){
if((g_alarmState & (0x01<<i)) && !(g_alarmStateSended & (0x01<<i))){
Serial.print("Send again");
Serial.println(i);
if(!sendMessage(i)){
if(retry>0){
retry--;
i--;
}else{
g_GSMConnected=0;
}
}
}
}
}
void loop()
{
if (g_firstStart)
if(!g_GSMConnected){
digitalWrite(READY_LED_PIN, LOW);
Serial.println("Trying to connect");
if(g_gsmModule.begin(PINNUMBER)==GSM_READY){
g_GSMConnected=1;
Serial.println("Connected");
}
digitalWrite(READY_LED_PIN, HIGH);
}
checkAlarm();
setAlarmLed();
checkReset();
checkUnsendAlarms();
//if(g_GSMConnected)
//delay(2000);
}