-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim800l_light.cpp
278 lines (251 loc) · 6.19 KB
/
sim800l_light.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* @file sim800l_light.cpp
* @author MakerStation ([email protected])
* @brief A light version of SIM800L communication library using AT commands through serial
* @version 0.1
* @date 2022-01-16
*
* @copyright Copyright (c) 2022
*
*/
// #TODO valutare di creare una unica funzione per gestire comandi semplici (vedi "_enableResultCode" e "_setMessageFormat")
#include "Arduino.h"
// #include <SoftwareSerial.h> // passing the serial object to function, the serial definition must be done in main code, so #include is not needed
#include "sim800l_light.h"
#include <avr/sleep.h>
// SoftwareSerial SIM(RX_PIN, TX_PIN); // passing the serial object to function, the serial definition must be done in main code
/**
* @brief Initialization of SIM800 Serial comm and first mandatory setup
*
* @param speed Speed of serial (max 19200 to avoid packet loss)
*/
void sim800l_light::begin(long speed, SoftwareSerial &serobj)
{
switch (speed)
{
case 300:
speed = 300;
break;
case 600:
speed = 600;
break;
case 1200:
speed = 1200;
break;
case 2400:
speed = 2400;
break;
case 4800:
speed = 4800;
break;
case 9600:
speed = 9600;
break;
case 19200:
speed = 19200;
break;
default:
speed = 19200;
bPrintSerialWarning = true;
break;
}
serobj.begin(speed);
while (!serobj)
{
;
}
// qui ci andrebbe funzione di controllo se entro 10 sec il modulo gsm ha risposto oppure no.
// si potrebbe usare la lib elapsedMillis https://github.com/pfeerick/elapsedMillis
// perchè se dopo 10 sec il moulo gsm non risponde occorre fargli un reset hw con il suo pin
// oppure fargli un power cycle.
// però mettere un delay() fa schifo perchè il programma si blocca per il tempo della delay()
// e non è bellissimo....
if (_tryToConnect(serobj))
{
_buffer.reserve(255); // reserve memory to prevent intern fragmention
}
/**
* Il seguente pezzo di codice servirebbe per avere un output visivo dell'esito della begin usando il built-in led.
* Valutare se togliere o no. Se no ragionare se metterlo dinamico con la possibilità di definire altri pin
* oppure se mantenere quello. !!ATTENZIONE!! verificare compatibilità con le varie schede arduino.
*
*/
#if (LED)
pinMode(OUTPUT, LED_PIN);
#endif
}
/**
* @brief Enable the showing of result code of any command
*
* @param serobj The serial object to use to communicate with SIM800L
* @return true The command completed successfully
* @return false Something goes wrong with the command
*/
bool sim800l_light::_enableResultCode(SoftwareSerial &serobj)
{
serobj.println(F("ATQ0"));
if (serobj.available())
{
if (serobj.readString().indexOf("OK") != -1)
{
return true;
}
else
{
return false;
}
}
}
/**
* @brief Set returned message format
*
* @param format 0/1 - 0 = PDU format (hex) / 1 = Clear Text
* @param serobj The serial object to use to communicate with SIM800L
* @return true
* @return false
*/
bool sim800l_light::_setMessageFormat(int format, SoftwareSerial serobj)
{
serobj.println(F("AT+CMGF=") + format);
if (serobj.available())
{
if (serobj.readString().indexOf("OK") != -1)
{
return true;
}
else
{
return false;
}
}
}
/**
* @brief Try to connect to SIM800L module
*
* @param serobj The serial object to use to communicate with SIM800L
* @return true Connect ok
* @return false Connect not ok
*/
bool sim800l_light::_tryToConnect(SoftwareSerial &serobj)
{
for (_iConnectTest = 0; _iConnectTest < 3; _iConnectTest++)
{
delay(10000);
serobj.println(F("AT"));
if (serobj.available())
{
if (serobj.readString().indexOf("OK") != -1)
{
_bInitDone = true;
}
else
{
_bInitDone = false;
}
}
switch (_bInitDone)
{
case true:
// doing all SIM800L init procedure needed to work correctly
if (_enableResultCode(serobj))
{
if (_setMessageFormat(1, serobj))
{
return true;
}
}
else
{
return false;
}
break;
case false:
// do SIM800L reset through the dedicated pin
pinMode(RESET_PIN, OUTPUT);
digitalWrite(RESET_PIN, LOW);
delay(500);
digitalWrite(RESET_PIN, HIGH);
default:
break;
}
}
/* the following code will "stop" the execution of the code, preventing function to return the false state */
if (_iConnectTest > 2)
{
Serial.println(" ****** W A R N I N G ******");
Serial.println("There was a problem during SIM800L Module init procedure.");
Serial.println("Module didn't respond after 10 sec and it has been already resetted 2 times...");
Serial.println("Check SIM800L Module, power and connection.");
Serial.println("");
Serial.println("=== PROGRAM HALTED ===");
// as a very HALT state doesn't exist in arduino (you can only power off it), we use deep sleep so to reduce drastically power consumption
sleep_enable(); // enabling sleep function
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // setting sleep mode
delay(1000); // time wait 1 sec - could be removed or reduced
power_all_disable(); // disabling all (btw it will disable also interrupts!)
sleep_mode(); // going to sleep
}
}
/**
* @brief
*
* @param commandToSearch
* @param serobj
* @param changeMe
* @return string
*/
string _readSMS(cmdType commandToSearch, SoftwareSerial &serobj, bool changeMe); //TODO richiamata da checkMessageForCommand
{
}
/**
* @brief Get the Message Count By Type object
*
* @param stat
* @param changeMe
* @param serobj
* @return int
*/
int getMessageCountByType(messageStatus stat, bool changeMe, SoftwareSerial &serobj); // TODO
{
}
/**
* @brief
*
* @param delType
* @param serobj
* @return true
* @return false
*/
bool deleteMessage(deleteType delType, SoftwareSerial &serobj); // TODO
{
}
/**
* @brief
*
* @param command
* @param serobj
*/
void checkMessageForCommand(cmdType command, SoftwareSerial & serobj); // TODO
{
}
/**
* @brief
*
* @param dest
* @param text
* @param Serobj
* @return true
* @return false
*/
bool sendSMS(string dest, string text, SoftwareSerial &Serobj); //TODO - Ricordarsi il CTRL+Z alla fine (ASCII 26) altrimenti il messaggio non va via
{
}
/**
* @brief
*
* @param serobj
* @return int
*/
int checkForMessage(SoftwareSerial &serobj); // TODO - controlla il +CMTI sulla seriale
{
}