82 lines
2.0 KiB
C++
82 lines
2.0 KiB
C++
#include "GSMMobileData.h"
|
|
#include <SoftwareSerial.h>
|
|
#include "GSMMobileDevice.h"
|
|
#include "DebugLog.h"
|
|
|
|
void GSMMobileDevice::end()
|
|
{
|
|
_gsmSerial.end();
|
|
DebugLog::log("GSM End ..");
|
|
}
|
|
|
|
void GSMMobileDevice::executeAtCommand(const String &atCommand)
|
|
{
|
|
DebugLog::log(atCommand);
|
|
_gsmSerial.print(atCommand + "\r");
|
|
delay(500);
|
|
}
|
|
|
|
String GSMMobileDevice::readStringSerialPort()
|
|
{
|
|
return _gsmSerial.readString();
|
|
}
|
|
|
|
void GSMMobileDevice::sendSMSToGSM(GSMMobileData gsmData)
|
|
{
|
|
DebugLog::log("SMS Start SEND SMS Id:" + gsmData.Id + " M:" + gsmData.MobileNumber + " with message " + gsmData.Sms);
|
|
|
|
/*executeAtCommand("AT+CMGF=1");
|
|
executeAtCommand("AT + CMGS =\"" + gsmData.MobileNumber + "\"");
|
|
executeAtCommand("ID:" + gsmData.Id + " Msg:" + gsmData.Sms);
|
|
|
|
delay(1000);
|
|
// Send a Ctrl+Z / ASCII SUB
|
|
_gsmSerial.print((char)26);
|
|
delay(500);
|
|
// Close the connection
|
|
_gsmSerial.print((char)27);*/
|
|
delay(500);
|
|
}
|
|
|
|
bool GSMMobileDevice::checkSMSError()
|
|
{
|
|
return true;
|
|
String response = readStringSerialPort();
|
|
if (response.indexOf("ERROR") != -1)
|
|
{
|
|
// If there was an error, try to reconnect and resend
|
|
_gsmSerial.println("AT+CFUN=1,1"); // Restart the module
|
|
delay(15000); // Wait for the module to restart
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
GSMMobileDevice::GSMMobileDevice(uint8_t pinRx, uint8_t pinTx, long baudRate): _gsmSerial(pinRx, pinTx)
|
|
{
|
|
_baudRate = baudRate;
|
|
// init software serial
|
|
_gsmSerial.begin(baudRate);
|
|
DebugLog::log("GSM Init ..");
|
|
//delay(5000); // initialization GSM module (give it some time to boot up)
|
|
}
|
|
|
|
void GSMMobileDevice::sendSMS(GSMMobileData gsmData)
|
|
{
|
|
init();
|
|
sendSMSToGSM(gsmData);
|
|
|
|
if (checkSMSError())
|
|
{
|
|
sendSMSToGSM(gsmData);
|
|
}
|
|
|
|
end();
|
|
}
|
|
|
|
// GSMMobileDevice::~GSMMobileDevice()
|
|
// {
|
|
// // // close gsmSerial
|
|
// // // close gsmSerial
|
|
// _gsmSerial.end();
|
|
// }
|