Hello All,
I'm trying to implement cellular connectivity using an Adafruit FONA. The Adafruit library uses software serial, which doesn't play nice with the RTOS that I'm trying to use, so I'm writing some code that uses UART 2 on the mega2560.
I made a small test sketch to send me a text message every 30 seconds, which works great. Unfortunately, I don't confirm the FONA's response before sending more commands; I'm using hardcoded delays between commands. For instance, I would like to resend "AT" until I get the "OK" response.
It looks like Adafruits library does not try to resend; the "sendCheckReply" function returns a boolean for if the response matches the desired response, but the boolean is never checked.
Any ideas for how to do implement a resend? Thanks for your time.
/*
This code is written to use UART #2 on an Arduino Mega2560 to send an SMS message to a fixed number every 30 seconds.
It is pretty dumb, but it's a proof of concept of the AT commands being used correctly without a library
that usually use SoftwareSerial.
*/
#define FONA_RST 4 //reset pin on the FONA is connected to Arduino pin 4, different than the stock FONA shield
//-------------------------------------------------------------------------------------
void setup() {
//start up the serial for debugging
while (!Serial);
Serial.begin(115200);
Serial.println(F("FONA SMS test"));
//reset the FONA for good measure
pinMode(FONA_RST, OUTPUT);
digitalWrite(FONA_RST, HIGH);
delay(10);
digitalWrite(FONA_RST, LOW);
delay(100);
digitalWrite(FONA_RST, HIGH);
Serial.println(F("FONA is restarting now, give it a sec"));
delay(10000); // give 10 generous seconds to reboot
//start up FONA connection and try to get the FONAs attention
Serial2.begin(9600);
Serial.println(F("Sending out AT")); //debug
Serial2.println("AT"); // response "OK";
delay(1000);
}
//-------------------------------------------------------------------------------------------
void loop() {
//Set FONA in Text Mode
Serial.println(F("Setting system to text mode"));
Serial2.println("AT+CMGF=1"); // AT command for set system into text mode, expect response OK
delay(1000);
//forming the SMS address and message body
Serial.println(F("sending sms"));
char sendto[21];
char smsMsg[128];
String number= "+XXXXXXXXXXX"; //replace with an actual phone number
String message = "It worked!";
number.toCharArray(sendto,21);
message.toCharArray(smsMsg,128);
//debug output of the address and message
Serial.print("Message Sending to: ");
Serial.println(sendto);
Serial.print("Body of message: ");
Serial.println(smsMsg);
//form the SMS address command using the given phone number and the AT command
char sendcmd[30] = "AT+CMGS=\"";
strncpy(sendcmd+9, sendto, 30-9-2); // 9 bytes beginning, 2 bytes for close quote + null
sendcmd[strlen(sendcmd)] = '\"';
//send the SMS address command
Serial2.println(sendcmd);
delay(1000);
//send the SMS message body
Serial2.println(smsMsg);
Serial2.write(0x1A); //CTRL+Z, which is ASCII character 26, to terminate message body
Serial.println(F("sent SMS"));
delay(30000);
}