Dear all,
i am working with an Arduino mega, connected to a SIM800L GPRS module. I use the adafruit FONA library to interact with the module.
I want to use the module to send an SMS to a predefined phone number.
Now i've setup a program to see if i can send a SMS:
#include "Adafruit_FONA.h"
#define FONA_RX 19
#define FONA_TX 18
#define FONA_RST 2
// this is a large buffer for replies
char replybuffer[255];
// I USE HARDWARESERIAL BECAUSE SOFTWARESERIAL IS NOT WORKING
HardwareSerial *fonaSerial = &Serial1;
// Use this for FONA 800 and 808s
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
uint8_t type;
void setup() {
while (!Serial);
Serial.begin(9600);
Serial.println(F("FONA basic test"));
Serial.println(F("Initializing....(May take 3 seconds)"));
fonaSerial->begin(4800);
if (! fona.begin(*fonaSerial)) {
Serial.println(F("Couldn't find FONA"));
while (1);
}
// Unlock the SIM with a PIN code
char PIN[5] = "0000";
Serial.println(PIN);
Serial.print(F("Unlocking SIM card: "));
if (! fona.unlockSIM(PIN)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
}
int8_t smsnum;
bool do_once = false;
void loop() {
if (digitalRead(10) == true && do_once == false) {
do_once = true;
char sendto[21] = "+31612345678", message[141] = "testing..."; // NOT A REAL PHONE NUMBER
Serial.print(F("Send to #"));
Serial.println(sendto);
Serial.println(message);
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("Sent!"));
}
}
else if (digitalRead(10) == false) {
do_once = false;
}
}
The code around the FONA funcitons are all copy pasted (and slightly modified) from the FONAtest example.
I use pin10 to simulate as an error. Every time 10 is set to HIGH, a message needs to be sent.
Now what happens is The sens SMS function returns FALSE meaning it prints out FAILED.
But then a couple of seconds later i DO get the SMS on my phone!
So the message did send, but the function responds the wrong outcome.
i looked into the Adafruit_FONA.cpp. The SMS function looks like this:
bool Adafruit_FONA::sendSMS(char *smsaddr, char *smsmsg) {
if (!sendCheckReply(F("AT+CMGF=1"), ok_reply))
return false;
char sendcmd[30] = "AT+CMGS=\"";
strncpy(sendcmd + 9, smsaddr,
30 - 9 - 2); // 9 bytes beginning, 2 bytes for close quote + null
sendcmd[strlen(sendcmd)] = '\"';
if (!sendCheckReply(sendcmd, F("> ")))
return false;
DEBUG_PRINT(F("> "));
DEBUG_PRINTLN(smsmsg);
mySerial->println(smsmsg);
mySerial->println();
mySerial->write(0x1A);
DEBUG_PRINTLN("^Z");
if ((_type == FONA3G_A) || (_type == FONA3G_E)) {
// Eat two sets of CRLF
readline(200);
// DEBUG_PRINT("Line 1: "); DEBUG_PRINTLN(strlen(replybuffer));
readline(200);
// DEBUG_PRINT("Line 2: "); DEBUG_PRINTLN(strlen(replybuffer));
}
readline(10000); // read the +CMGS reply, wait up to 10 seconds!!!
// DEBUG_PRINT("Line 3: "); DEBUG_PRINTLN(strlen(replybuffer));
if (strstr(replybuffer, "+CMGS") == 0) {
return false;
}
readline(1000); // read OK
// DEBUG_PRINT("* "); DEBUG_PRINTLN(replybuffer);
if (strcmp(replybuffer, "OK") != 0) {
return false;
}
return true;
}
It seems as the code above returns false to the main sketch even though the SMS got send.
Does anyone have any idea why this is happening and how to fix it, or if i should just leave it en ignore the return of the function for now?
Kind regards,
Jan Tromp.