-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGSM
112 lines (98 loc) · 2.61 KB
/
GSM
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
/**
* @author Manu Devappa
*
* @copyright Copyright (c) 2021
* dmanu.techie@gmail.com
*/
#include <SoftwareSerial.h>
SoftwareSerial GPRS(3, 2);
String textMessage;
String lampState;
const int relay = 13; //If you're using a relay to switch, if not reverse all HIGH and LOW on the code
int ringCount;
void setup() {
ringCount = 0;
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW); // The current state of the light is ON
Serial.begin(9600);
GPRS.begin(9600);
delay(5000);
Serial.print("GPRS ready...\r\n");
GPRS.print("AT+CMGF=1\r\n");
delay(1000);
GPRS.print("AT+CNMI=2,2,0,0,0\r\n");
delay(1000);
}
void loop(){
if(GPRS.available()>0){
textMessage = GPRS.readString();
Serial.print(textMessage);
delay(10);
if(textMessage.indexOf("RING")>=0){
ringCount ++;
Serial.println(ringCount);
}
if(textMessage.indexOf("NO CARRIER")>=0){
if(ringCount == 2){
on();
ringCount = 0;
}
else if(ringCount == 4){
off();
ringCount = 0;
}
else{
ringCount = 0;
}
}
}
if(textMessage.indexOf("ON")>=0){ //If you sent "ON" the lights will turn on
on();
}
if(textMessage.indexOf("OFF")>=0){
off();
}
if(textMessage.indexOf("STATUS")>=0){
status_reg();
}
}
void status_reg(){
if(lampState = "ON"){
GPRS.println("AT+CMGS=\"+91xxxxxxxxxx\""); // RECEIVER: change the phone number here with international code
delay(500);
GPRS.print("Lamp is still ON.\r");
GPRS.write( 0x1a );
delay(1000);
}
else if(lampState = "OFF"){
GPRS.println("AT+CMGS=\"+91xxxxxxxxxx\""); // RECEIVER: change the phone number here with international code
delay(500);
GPRS.print("Lamp is still OFF.\r");
GPRS.write( 0x1a );
delay(1000);
}
}
void on(){
// Turn on relay and save current state
digitalWrite(relay, HIGH);
lampState = "ON";
Serial.println("Lamp set to ON\r\n");
textMessage = "";
GPRS.println("AT+CMGS=\"+91xxxxxxxxxx\""); // RECEIVER: change the phone number here with international code
delay(500);
GPRS.print("Lamp was finally switched ON.\r");
GPRS.write( 0x1a );
delay(1000);
}
void off(){
// Turn off relay and save current state
digitalWrite(relay, LOW);
lampState = "OFF";
Serial.println("Lamp set to OFF\r\n");
textMessage = "";
GPRS.println("AT+CMGS=\"+91xxxxxxxxxx\""); /// RECEIVER: change the phone number here with international code
delay(500);
GPRS.print("Lamp was finally switched OFF.\r");
GPRS.write( 0x1a );
delay(1000);
}