33 Serial2.begin(DEFAULT_BAUD_RATE, SERIAL_8N1, RX, TX);
38 _pSerialHandler =
new SerialHandler(&Serial2);
39 _pParser =
new ParserGSM(_pSerialHandler, &_newMsg, &_lastMsgIndex);
40 _pPhoneBuffer = _pParser->getPointPhoneBuf();
50Stream *pGsmSerial =
nullptr;
54 pGsmSerial =
new SoftwareSerial();
55 ((SoftwareSerial*) pGsmSerial)->begin(baud, SWSERIAL_8N1, rx, tx,
false, RX_BUF_SIZE, 0);
57 pGsmSerial =
new SoftwareSerial(rx, tx);
58 ((SoftwareSerial*) pGsmSerial)->begin(baud);
62 Serial2.begin(baud, SERIAL_8N1, rx, tx);
66 pGsmSerial = &Serial2;
68 _pSerialHandler =
new SerialHandler(pGsmSerial);
69 _pParser =
new ParserGSM(_pSerialHandler, &_newMsg, &_lastMsgIndex);
70 _pPhoneBuffer = _pParser->getPointPhoneBuf();
79 _pSerialHandler =
new SerialHandler(pGsmSerial);
80 _pParser =
new ParserGSM(_pSerialHandler, &_newMsg, &_lastMsgIndex);
81 _pPhoneBuffer = _pParser->getPointPhoneBuf();
92 _pSerialHandler->periodicSerialCheck();
94 if(_pSerialHandler->isRxBufferAvailable()){
95 if(_pParser->identifyIncomingMsg(incomingSms)){
96 if(sendCommand(_pParser->makeDynamicCmd(smsReading, _lastMsgIndex))){
97 _pParser->getPhoneNumber();
99 _pMsgBuffer = _pParser->getPointMsgBuf();
100 if(_lastMsgIndex > 10){
108 Serial.println(F(
"ERR"));
113 _pSerialHandler->setRxBufferAvailability(
false);
138 return _pPhoneBuffer;
146 while(sendCommand(basicCommand) !=
true){
148 Serial.println(F(
"GSM IS OFFLINE"));
150 Serial.println(F(
"GSM IS ONLINE"));
152 while(sendCommand(gsmMode) !=
true){
154 Serial.println(F(
"CONFIG FAILED"));
156 Serial.println(F(
"GSM IS CONFIGURED"));
158 while(sendCommand(plainTextMode) !=
true){
160 Serial.println(F(
"MSG SETTING FAILED"));
162 Serial.println(F(
"MSG SET TO TEXT"));
171bool GSM::sendCommand(
const char* cmd){
172 _pSerialHandler->serialWrite(cmd);
173 return _pParser->getResponse(confirmFeedback);
179void GSM::deleteMsg(){
180 if(sendCommand(_pParser->makeDynamicCmd(deleteSms, _lastMsgIndex))){
181 Serial.println(F(
"MSG DELETED"));
185 Serial.println(F(
"DELETE ERR"));
192void GSM::deleteMsgGsmStack(){
193 while(_lastMsgIndex != 0){
204GSM::ParserGSM::ParserGSM(GSM::SerialHandler* pSerialHandler,
bool* newMsg, uint8_t* lastMsgIndex){
205 _pSerialHandler = pSerialHandler;
207 _pLastMsgIndex = lastMsgIndex;
215bool GSM::ParserGSM::getResponse(
const char* searchedChar){
216 _pSerialHandler->feedbackSerialCheck();
217 _pRxBuffer = _pSerialHandler->getRxBufferP();
218 if(_pSerialHandler->isRxBufferAvailable()){
219 if(strstr(_pRxBuffer,
"OK") !=
nullptr){
220 _pSerialHandler->setRxBufferAvailability(
false);
233void GSM::ParserGSM::getMsg(){
234 _pRxBuffer = _pSerialHandler->getRxBufferP();
235 char* tmpStr = strrchr(_pRxBuffer,
'\"') + 3;
237 if(strrchr(tmpStr,
';') !=
nullptr){
238 char* endMsgPointer = strrchr(tmpStr,
';') + 1;
241 if(_msgBuffer !=
nullptr){
244 _msgBuffer = (
char*)malloc(strlen(tmpStr) - strlen(endMsgPointer) + 1);
245 while(&tmpStr[counter] != endMsgPointer){
246 _msgBuffer[counter] = tmpStr[counter];
249 _msgBuffer[counter] =
'\0';
257void GSM::ParserGSM::getPhoneNumber(){
258 _pRxBuffer = _pSerialHandler->getRxBufferP();
259 char* tmpStr = strstr(_pRxBuffer,
"\"+") + 2;
260 char* endMsgPointer = strstr(tmpStr,
"\",\"\",\"");
263 memset(_phoneBuffer, 0,
sizeof(_phoneBuffer));
264 while(&tmpStr[counter] != endMsgPointer){
265 _phoneBuffer[counter] = tmpStr[counter];
268 _phoneBuffer[counter] =
'\0';
275char* GSM::ParserGSM::getPointMsgBuf(){
283char* GSM::ParserGSM::getPointPhoneBuf(){
292bool GSM::ParserGSM::identifyIncomingMsg(
const char* command){
293 _pRxBuffer = _pSerialHandler->getRxBufferP();
294 char *tmpStr = strstr(_pRxBuffer, command);
295 if(tmpStr !=
nullptr){
296 if(strncmp(tmpStr, incomingSms, strlen(incomingSms)) == 0){
297 *_pLastMsgIndex = GetIndex(tmpStr,
',');
298 _pSerialHandler->setRxBufferAvailability(
false);
302 _pSerialHandler->setRxBufferAvailability(
false);
312char* GSM::ParserGSM::makeDynamicCmd(
const char* command, uint8_t
id){
313 if(_cmdBuffer !=
nullptr){
318 _cmdBuffer = (
char*)malloc(strlen(command) + 3);
319 strcpy(_cmdBuffer, command);
320 sprintf(&_cmdBuffer[strlen(command)],
"%d",
id);
321 _cmdBuffer[strlen(command) + 2] =
'\0';
331uint8_t GSM::ParserGSM::GetIndex(
char* buffer,
char startSym){
332 char *tmpStr = strchr(buffer, startSym) + 1;
333 uint8_t indexLenCount = 0;
334 while(tmpStr[indexLenCount] >= 0x31 && tmpStr[indexLenCount] <= 0x39){
337 tmpStr[indexLenCount] =
'\0';
338 return (uint8_t)atoi(tmpStr);
345GSM::SerialHandler::SerialHandler(Stream* pGsmSerial){
346 _pGsmSerial = pGsmSerial;
352void GSM::SerialHandler::serialWrite(
const char* command){
353 _periodicReading =
false;
354 _pGsmSerial->println(command);
355 _pGsmSerial->flush();
357 _periodicReading =
true;
364void GSM::SerialHandler::periodicSerialCheck(){
365 if(_periodicReading){
366 if(_periodicReadingFlag){
367 _lastReadTime = millis();
368 _periodicReadingFlag =
false;
371 if((millis() - _lastReadTime) >= PERIODIC_READ_TIME){
373 var = _pGsmSerial->available();
375 if(var > 0 && _periodicReading){
378 _periodicReadingFlag =
true;
386void GSM::SerialHandler::feedbackSerialCheck(){
387 unsigned long timeFlag = millis();
388 while(millis() < (timeFlag + 1000)){
390 var = _pGsmSerial->available();
403char* GSM::SerialHandler::getRxBufferP(){
411bool GSM::SerialHandler::isRxBufferAvailable(){
412 return _rxBufferAvailable;
419void GSM::SerialHandler::setRxBufferAvailability(
bool var){
420 _rxBufferAvailable = var;
427void GSM::SerialHandler::serialRead(uint8_t incomingBytes){
428 if(_rxBuffer !=
nullptr){
432 _rxBuffer = (
char*)malloc(
sizeof(
char) * (incomingBytes + 1));
433 _pGsmSerial->readBytes(_rxBuffer, incomingBytes);
434 _rxBuffer[incomingBytes] =
'\0';
435 _rxBufferAvailable =
true;
Adeon library for GSM modules.
void checkGsmOutput()
Checks for incoming SMS. Checks serial for new incoming message. If message is detected,...
GSM(uint8_t rx, uint8_t tx, long baud=DEFAULT_BAUD_RATE)
Constructor for the class GSM.
bool isNewMsgAvailable()
Returns new message availability.
char * getPhoneNum()
Returns pointer to phone number buffer array.
void begin()
Sets GSM module. Performs standard AT test, sets GSM mode and message in plain text.
char * getMsg()
Returns pointer to message buffer array.