-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hshopvn_GSM.cpp
312 lines (254 loc) · 6.26 KB
/
Hshopvn_GSM.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "Hshopvn_GSM.h"
#ifdef hardwareSerial
HshopGSM::HshopGSM(HardwareSerial *refSer){
#else
#if ARDUINO >= 100
HshopGSM::HshopGSM(SoftwareSerial *refSer){
#else
HshopGSM::HshopGSM(NewSoftSerial *refSer){
#endif
#endif
myGMS = refSer;
at = "AT+";
crlf = "\r\n";
atd = "ATD";
ul_timeCheckConnect = millis();
set_timeWaitRespond(1000);
}
void HshopGSM::init(void (*p_void_)(), long baud, String _CountryCode){
begin(baud);
// myGMS->begin(baud);
setUserFunct(p_void_);
DB_init();
if(_CountryCode.indexOf("+")>-1){
CountryCode = _CountryCode;
}else{
CountryCode = "+84";
}
Re_init();
}
void HshopGSM::init(void (*p_void_)(), long baud){
begin(baud);
// myGMS->begin(baud);
setUserFunct(p_void_);
CountryCode = "+84";
Re_init();
}
void HshopGSM::Re_init(){
isGetdata = false;
uc_CountErr = 0;
/*Tat che do phan hoi*/
atcm("ATE0");
/*Bat che do hien thi thong tin cua nha mang*/
atcm(at+"CUSD=1");
/*Bat che do DTMF*/
atcm(at+"DDET=1");
/*Hien thi noi dung tin nhan truc tiep*/
atcm(at + "CNMI=2,2");
/*Chon che do Text*/
atcm(at + "CMGF=1");
atcm(at + "CSCS=\"GSM\"");
/* Hien thi SDT goi den*/
atcm(at + "CLIP=1");
getData = "";
isSendSMS = _no_act;
isRespond = _no_act;
isUser = _no_act;
}
void HshopGSM::call(String _callNumber){
atcm(atd + _callNumber + ";");
}
void HshopGSM::call(unsigned long _callNumber){
atcm(atd + CountryCode + _callNumber + ";");
}
void HshopGSM::hangcall(){
// myGMS->print("ATH"+crlf);
atcm("ATH"+crlf);
}
void HshopGSM::answer(){
// myGMS->print("ATA"+crlf);
atcm("ATA"+crlf);
}
void HshopGSM::sendsms(String _callNumber,String _content){
// atcm(at + "CMGF=1");
isSendSMS = _tr_act;
atcm(at + "CMGS=\"" + _callNumber + "\"");
if(isSendSMS == _re_act){
myGMS->print(_content);
myGMS->write(26); // Ctrl + Z see more Ascii table: http://www.asciitable.com/
isSendSMS == _no_act;
}else;
}
void HshopGSM::sendsms(unsigned long _callNumber,String _content){
// atcm(at + "CMGF=1");
isSendSMS = _tr_act;
atcm(at + "CMGS=\"" + CountryCode + String(_callNumber) + "\"");
if(isSendSMS == _re_act){
myGMS->print(_content);
myGMS->write(26); // Ctrl + Z see more Ascii table: http://www.asciitable.com/
isSendSMS == _no_act;
}else;
}
void HshopGSM::deleteSms(unsigned char Pos_){
if(Pos_ == 0){
atcm(at + "CMGDA=\"DEL ALL\"");
}else{
atcm(at + "CMGD=" + String(Pos_));
}
}
void HshopGSM::read(){
char temp_char;
while(myGMS->available() && (temp_char != '\n')){
temp_char = myGMS->read();
if(temp_char != '\n'){
getData += temp_char;
delayMicroseconds(1000);
}else if(temp_char == '\n'){
isComplete = true;
}else;
}
if((getData.length() > 2) && isComplete == true){
isGetdata = true;
UserData += getData;
GSM_funct();
// if(isUser == _no_act){
// isUser = _tr_act;
// p_UserFunct();
// isUser = _no_act;
// }else;
getData = "";
isComplete = false;
}else;
}
void HshopGSM::atcm(String _atcm_){
myGMS->print(_atcm_ + crlf);
// if(isUser == _no_act){
isRespond = _tr_act;
isGetdata = false;
unsigned long _gstart = millis();
while(((millis() - _gstart) < ul_timeWaitResp) && isGetdata == false){
read();
// isUser = _tr_act;
// p_UserFunct();
// isUser = _no_act;
}
if(isGetdata == false){
GSM_funct();
}else;
// }else;
}
/* Baud default: 9600 */
void HshopGSM::Setbaud(long baud){
if(baud == 0 || baud == 1200 || baud == 2400 || baud == 4800 || baud == 9600 || baud == 19200 || baud == 38400 || baud == 57600 || baud == 115200){
atcm(at+"IPR="+String(baud));
}else{
atcm(at+"IPR=9600");
}
}
void HshopGSM::set_timeWaitRespond(unsigned long _set_time_wait){
ul_timeWaitResp = _set_time_wait;
}
void HshopGSM::setUserFunct(void (*_p_UserFunct)()){
p_UserFunct = _p_UserFunct;
}
void HshopGSM::handle(){
read();
if(isUser == _no_act){
isUser = _tr_act;
if(p_UserFunct != NULL){
p_UserFunct();
}else;
isUser = _no_act;
UserData = "";
}else;
// UserData = "";
if(millis() - ul_timeCheckConnect >=5000){
ul_timeCheckConnect = millis();
checkConnect();
// DBshow(uc_CountErr);
// DBshown(" Check connect"); // just for Debug
}else;
}
void HshopGSM::checkConnect(){
if(uc_CountErr >= 2){
Re_init();
}else;
}
void HshopGSM::GSM_funct(){
if(isRespond == _tr_act){
if(isSendSMS == _tr_act && getData.indexOf(">") > -1){
isSendSMS = _re_act;
isRespond = _re_act;
getData = "";
// DBshown(">");
}else if(getData.indexOf("OK") > -1){
isRespond = _re_act;
// DBshown("G");
}else{
uc_CountErr++;
isRespond = _no_act;
// DBshown("F");
// DBshown(uc_CountErr);
}
}else;
}
void HshopGSM::delay(unsigned long timeDelay){
unsigned long temp_time = millis();
while(millis() - temp_time < timeDelay){
handle();
}
}
void HshopGSM::begin(long baud){
myGMS->begin(baud);
}
String HshopGSM::getDataGSM(){
// return getData;
return UserData;
}
void HshopGSM::clearData(){
getData = "";
}
void HshopGSM::addMoney(String _code){
myGMS->print(atd + "*100*" + _code + "#" + crlf);
}
void HshopGSM::checkMoney(){
myGMS->print(atd + "*101#" + crlf);
}
bool HshopGSM::checkData(String StringNeedCheck){
if(UserData.indexOf(StringNeedCheck)>-1){
// if(getData.indexOf(StringNeedCheck)>-1){
return true;
}else{
return false;
}
}
bool HshopGSM::checkData(unsigned long NumberNeedCheck){
if(UserData.indexOf(String(NumberNeedCheck))>-1){
return true;
}else{
return false;
}
}
String HshopGSM::splitString(String v_G_motherString, String v_G_Command, String v_G_Start_symbol, String v_G_Stop_symbol, unsigned char v_G_Offset){
unsigned char lenOfCommand=v_G_Command.length();
unsigned char posOfCommand=v_G_motherString.indexOf(v_G_Command);
int PosOfStartSymbol=v_G_motherString.indexOf(v_G_Start_symbol,posOfCommand+lenOfCommand);
while(v_G_Offset>0){
v_G_Offset--;
PosOfStartSymbol=v_G_motherString.indexOf(v_G_Start_symbol,PosOfStartSymbol+1);
}
int PosOfStopSymbol=v_G_motherString.indexOf(v_G_Stop_symbol,PosOfStartSymbol+1);
return v_G_motherString.substring(PosOfStartSymbol+1,PosOfStopSymbol);
}
#ifdef GSML_DB
void HshopGSM::DB_init(){
Serial.begin(9600);
Serial.println("DEBUG GMS START!");
}
#else
void HshopGSM::DB_init(){
}
#endif
void HshopGSM::VitualFunction(){
// just make program work!
}