-
Notifications
You must be signed in to change notification settings - Fork 1
/
SIM900.cpp
executable file
·202 lines (161 loc) · 3.5 KB
/
SIM900.cpp
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
/*
* @title SIM900
* @description Minimalist library for SIM900
* @author Grégory G. & Laurent T.
* @date 31/07/2015 (dd/mm/yyyy) 09:33
*/
#include "Arduino.h"
#include "SIM900.h"
/* PUBLIC FUNCTIONS */
void SIM900::begin(String pin)
{
Serial1.begin(9600);
simpleAT("ATE0");
sendAT("ATZ", "OK", 500);
sendAT("AT", "OK", 1000);
if(testPin() == 0){
sendAT("AT+CPIN="+pin, "OK", 2000);
}
sendAT("AT+CGREG?", "+CGREG: 0,5", 1000);
sendAT("AT+IFC=1,1","OK", 500);
sendAT("AT+CMGF=1", "OK", 500);
sendAT("AT+CLIP=1", "OK", 500);
sendAT("AT+COLP=1", "OK", 2000);
}
void SIM900::beginGPRS()
{
sendAT("AT+CGATT=1", "OK", 500);
sendAT("AT+CGDCONT=1,\"IP\",\"APN\"", "OK", 500);
sendAT("AT+CSTT=\"free\",\"\",\"\"", "OK", 500);
simpleAT("AT+CIICR");
delay(2000);
simpleAT("AT+CIFSR");
delay(1000);
sendAT("AT+CIPHEAD=1", "OK", 1000);
}
void SIM900::sms(String number, String msg)
{
simpleAT("AT+CMGS=\""+number+"\"");
delay(1000);
simpleAT(msg);
delay(1000);
sendCTR(false);
}
String SIM900::web(String ip, String adress)
{
sendAT("AT+CIPSTART=\"TCP\",\""+ip+"\",\"80\"", "OK", 1000);
simpleAT("AT+CIPSEND");
delay(2000);
simpleAT("GET "+adress);
simpleAT(" HTTP/1.1");
simpleAT(" HOST: "+ip);
simpleAT(" Connection: close");
return sendCTR(true);
}
/* SEND AT COMMAND */
String SIM900::sendAT(String cmd, String wait, int time)
{
unsigned int timeout = 10000;
boolean isTimeout = false;
unsigned int waiting_at = millis();
char buffer;
String response;
Serial.println("--------------------------");
Serial.println(cmd);
Serial1.println(cmd);
while(!strstr(&response[0], &wait[0]) && !isTimeout) {
if(millis() - waiting_at < timeout){
if(Serial1.available()){
buffer = Serial1.read();
response += buffer;
}
}
else {
isTimeout = true;
}
}
Serial.println(response);
Serial.println();
delay(time);
return response;
}
void SIM900::simpleAT(String cmd)
{
Serial.println(cmd);
Serial1.println(cmd);
}
String SIM900::simpleRead()
{
String response;
char buffer;
while(Serial1.available()){
buffer = Serial1.read();
response += buffer;
}
return response;
}
String SIM900::sendCTR(boolean gprs)
{
serialFlush();
Serial1.println((char)26);
if(gprs){
unsigned int timeout = 10000;
boolean isTimeout = false;
unsigned int waiting_at = millis();
char buffer;
String response;
while(!strstr(&response[0], "</S>") && !isTimeout) {
if(millis() - waiting_at < timeout){
if(Serial1.available()){
buffer = Serial1.read();
response += buffer;
}
}
else {
isTimeout = true;
response = "timeout";
}
}
Serial.println(response);
return response;
}
delay(500);
}
void SIM900::serialFlush()
{
char buf;
while(Serial1.available()){
buf = Serial1.read();
}
}
/* FUNC */
boolean SIM900::testPin()
{
simpleAT("AT+CPIN?");
delay(500);
String value = simpleRead();
Serial.println(value);
boolean pin = false;
if (strstr(&value[0], "+CPIN: READY"))
{
pin = true;
}
else
{
pin = false;
}
return pin;
}
/* Rechercher une valeur dans s
@param String s Chaine dans laquelle on cherche des valeurs
@param String search Valeur qu'on recherche
@return String cRetourValeur Chaine entre la valeur cherchée
*/
String SIM900::parse(String s, String search)
{
String cRetourValeur = "";
int TagR1Debut = s.indexOf("<"+search+">");
int TagR1Fin = s.indexOf("</"+search+">");
cRetourValeur = s.substring(TagR1Debut + 2 + search.length(), TagR1Fin );
return cRetourValeur;
}