forked from carrascoacd/ArduinoSIM800L
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSim800.cpp
227 lines (197 loc) · 5.21 KB
/
Sim800.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Sim800.cpp
* A library for SeeedStudio seeeduino GPRS shield
*
* Original work Copyright (c) 2013 seeed technology inc. [lawliet zou]
* Modified work Copyright 2021 Antonio Carrasco
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "Sim800.h"
// In power down the current is 60uA
const char POWER_DOWN[] PROGMEM = "AT+CPOWD=1\r\n";
const char SLEEP_MODE_2[] PROGMEM = "AT+CSCLK=2\r\n";
const char SLEEP_MODE_1[] PROGMEM = "AT+CSCLK=1\r\n";
const char SLEEP_MODE_0[] PROGMEM = "AT+CSCLK=0\r\n";
const char AT_OK[] PROGMEM = "OK";
const char POWER_DOWN_OK[] PROGMEM = "DOWN\r\n";
const char AT[] PROGMEM = "AT\r\n";
int SIM800::preInit(void)
{
pinMode(resetPin, OUTPUT);
digitalWrite(resetPin, HIGH);
delay(200);
digitalWrite(resetPin, LOW);
delay(2000);
digitalWrite(resetPin, HIGH);
delay(3000);
purgeSerial();
serialSIM800.flush();
sendATTest();
return TRUE;
}
int SIM800::checkReadable(void)
{
return serialSIM800.available();
}
int SIM800::readBuffer(char *buffer, int count, unsigned int timeOut)
{
int i = 0;
unsigned long timerStart = millis();
while (1)
{
while (serialSIM800.available())
{
char c = serialSIM800.read();
buffer[i] = c;
buffer[i + 1] = '\0';
++i;
if (i > count - 1)
break;
}
if (i > count - 1)
break;
unsigned long timerEnd = millis();
if (timerEnd - timerStart > timeOut)
{
break;
}
}
while (serialSIM800.available())
{
serialSIM800.read();
}
return TRUE;
}
void SIM800::cleanBuffer(char *buffer, int count)
{
for (int i = 0; i < count; i++)
{
buffer[i] = '\0';
}
}
void SIM800::sendCmd(const char *cmd, unsigned int delayBeforeSend)
{
serialSIM800.listen();
serialSIM800.flush();
delay(delayBeforeSend);
write(cmd);
serialSIM800.flush();
}
int SIM800::sendATTest(void)
{
int ret = sendCmdAndWaitForResp_P(AT, AT_OK, DEFAULT_TIMEOUT);
return ret;
}
int SIM800::waitForResp(const char *resp, unsigned int timeout)
{
int len = strlen(resp);
int sum = 0;
unsigned long timerStart = millis();
while (1)
{
if (serialSIM800.available())
{
char c = serialSIM800.read();
#ifdef DEBUG
Serial.print(c);
#endif
sum = (c == resp[sum] || resp[sum] == 'X') ? sum + 1 : 0;
if (sum == len)
break;
}
unsigned long timerEnd = millis();
if (timerEnd - timerStart > timeout)
{
return FALSE;
}
}
while (serialSIM800.available())
{
serialSIM800.read();
}
return TRUE;
}
void SIM800::sendEndMark(void)
{
serialSIM800.println((char)26);
}
int SIM800::sendCmdAndWaitForResp(const char *cmd, const char *resp, unsigned timeout)
{
sendCmd(cmd);
return waitForResp(resp, timeout);
}
int SIM800::sendCmdAndWaitForResp_P(const char *cmd, const char *resp, unsigned timeout)
{
char cmdBuff[128];
char respBuff[32];
strcpy_P(cmdBuff, cmd);
strcpy_P(respBuff, resp);
return sendCmdAndWaitForResp(cmdBuff, respBuff, timeout);
}
void SIM800::serialDebug(void)
{
while (1)
{
if (serialSIM800.available())
{
Serial.write(serialSIM800.read());
}
if (Serial.available())
{
serialSIM800.write(Serial.read());
}
}
}
void SIM800::purgeSerial()
{
while (serialSIM800.available())
serialSIM800.read();
}
void SIM800::write(const char *data)
{
serialSIM800.listen();
serialSIM800.write(data);
}
void SIM800::write(const char *data, unsigned int size)
{
serialSIM800.listen();
serialSIM800.write(data, size);
}
int SIM800::sleep(bool force)
{
if (force)
{
return sendCmdAndWaitForResp_P(SLEEP_MODE_1, AT_OK, 2000);
}
else
{
return sendCmdAndWaitForResp_P(SLEEP_MODE_2, AT_OK, 2000);
}
}
int SIM800::powerDown()
{
return sendCmdAndWaitForResp_P(POWER_DOWN, POWER_DOWN_OK, 2000);
}
void SIM800::wakeUp()
{
preInit();
}