-
Notifications
You must be signed in to change notification settings - Fork 14
/
SIM808.cpp
75 lines (55 loc) · 1.48 KB
/
SIM808.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
#include "SIM808.h"
TOKEN(RDY);
SIM808::SIM808(uint8_t resetPin, uint8_t pwrKeyPin, uint8_t statusPin)
{
_resetPin = resetPin;
_pwrKeyPin = pwrKeyPin;
_statusPin = statusPin;
pinMode(_resetPin, OUTPUT);
if(_pwrKeyPin != SIM808_UNAVAILABLE_PIN) pinMode(_pwrKeyPin, OUTPUT);
if (_statusPin != SIM808_UNAVAILABLE_PIN) pinMode(_statusPin, INPUT);
if(_pwrKeyPin != SIM808_UNAVAILABLE_PIN) digitalWrite(_pwrKeyPin, HIGH);
digitalWrite(_resetPin, HIGH);
}
SIM808::~SIM808() { }
#pragma region Public functions
void SIM808::init()
{
SIM808_PRINT_SIMPLE_P("Init...");
reset();
waitForReady();
delay(1500);
setEcho(SIM808Echo::Off);
}
void SIM808::reset()
{
digitalWrite(_resetPin, HIGH);
delay(10);
digitalWrite(_resetPin, LOW);
delay(200);
digitalWrite(_resetPin, HIGH);
}
void SIM808::waitForReady()
{
do
{
SIM808_PRINT_SIMPLE_P("Waiting for echo...");
sendAT(S_F(""));
// Despite official documentation, we can get an "AT" back without a "RDY" first.
} while (waitResponse(TO_F(TOKEN_AT)) != 0);
// we got AT, waiting for RDY
while (waitResponse(TO_F(TOKEN_RDY)) != 0);
}
bool SIM808::setEcho(SIM808Echo mode)
{
sendAT(S_F("E"), (uint8_t)mode);
return waitResponse() == 0;
}
size_t SIM808::sendCommand(const char *cmd, char *response, size_t responseSize)
{
flushInput();
sendAT(cmd);
uint16_t timeout = SIMCOMAT_DEFAULT_TIMEOUT;
readNext(response, responseSize, &timeout);
}
#pragma endregion