Hello,
I have a very simple and short C++ for Arduino targeting AT328.
It uses serial to handle SIM800L comms, just to get a small string for setup an IoT device.
We will upgrade to 4G soon, but will use the same approach.
We have some times that the string appears truncated, like lines 202 and 1549 in tha attached log.
I post my full code.
Is a small snippet of 150 lines.
I have played with different delays and baud rates without any significant improvement.
Thanks a lot,
And kind regards
Horacio
// Change collect routine to char c
// Put answers in char array
// Includes all minimum functions
#include <SoftwareSerial.h>
char caReceived[80];
char caDevice[]="BC94"; // device whose setup is read
SoftwareSerial SWserial(8, 9); // RX, TX
void setup(){
Serial.begin(9600);
Serial.println("SIM800 AT CMD Test");
Serial.println("AlemanOrig328_HBB_Base_EM_V4");
SWserial.begin(9600);
delay(15000);
Serial.println("Initial modem answer: ");
while (SWserial.available()) {
Serial.write(SWserial.read());
}
delay(2000);
gsm_config_gprs();
}
void loop() {
gsm_http_post();
delay(30000);
}
void gsm_http_post() {
Serial.println(" --- Start GPRS & HTTP --- ");
gsm_send_serial(F("AT+SAPBR=1,1"));
gsm_send_serial(F("AT+SAPBR=2,1"));
gsm_send_serial(F("AT+HTTPINIT"));
gsm_send_serial(F("AT+HTTPPARA=\"CID\",1"));
gsm_send_serial(F("AT+HTTPPARA=\"URL\",\"http://nutritronix.com/selectV201W.php\""));
gsm_send_serial(F("AT+HTTPPARA=\"CONTENT\",\"application/x-www-form-urlencoded\""));
gsm_send_serial(F("AT+HTTPDATA=34,10000"));
gsm_send_serial(F("api_key=tPmAT5Ab3j7F9&device#="), caDevice);
gsm_send_serial(F("AT+HTTPACTION=1"));
gsm_send_serial(F("AT+HTTPREAD"));
gsm_send_serial(F("AT+HTTPTERM"));
gsm_send_serial(F("AT+SAPBR=0,1"));
}
void gsm_config_gprs() {
Serial.println(" --- CONFIG GPRS --- ");
gsm_send_serial(F("AT"));
gsm_send_serial(F("AT"));
gsm_send_serial(F("AT+IPR=9600"));
gsm_send_serial(F("AT+IPR?"));
gsm_send_serial(F("AT+CFUN=1"));
gsm_send_serial(F("AT+CFUN?"));
gsm_send_serial(F("AT+CSQ"));
gsm_send_serial(F("AT+CREG=1"));
gsm_send_serial(F("AT+CREG?"));
gsm_send_serial(F("AT+GMR"));
gsm_send_serial(F("AT+CCID"));
gsm_send_serial(F("AT+SAPBR=3,1,\"Contype\",\"GPRS\""));
gsm_send_serial(F("AT+SAPBR=3,1,\"APN\",\"em\""));
gsm_send_serial(F("AT+HTTPTERM"));
gsm_send_serial(F("AT+SAPBR=0,1"));
Serial.println(" --- END CONFIG GPRS --- ");
}
// OVERLOAD 1 argument is F("....")
int gsm_send_serial(const __FlashStringHelper *text) { // overload 1 accepts F(Macro) call
static char command[80];
int i=0;
strncpy_P(command, (const char*)text, 80); // _P is the version to read from program space
Serial.print("Send ->: ");
SWserial.println(command);
delay(500);
long wtimer = millis();
while (wtimer + 3000 > millis()) {
while (SWserial.available()>0) {
char c;
c = SWserial.read();
delay(50);
caReceived[i] = c;
//
// Serial.print("\n");
// Serial.print(i);
// Serial.print(" char: ");
// Serial.print(caReceived[i]);
// Serial.print("\n hex: ");
// Serial.println(caReceived[i],HEX);
// delay(50);
i++;
}//endWhile inner
} //endWhile outer
Serial.println();
Serial.print("fnSendRead i Chars read -> i value: ");
Serial.println(i);
caReceived[i]='\0'; //Terminate c string
Serial.print("Ending fnSendRead, caReceived: ");
Serial.println(caReceived);
Serial.println("fnSendRead END ->: ================ \r\n");
return 1;
}
// OVERLOAD 2 argument is F("....") + a variable to be appended
int gsm_send_serial(const __FlashStringHelper *text, const char* append) {
static char command[80];
int i=0;
strncpy_P(command, (const char*)text, 80); // _P is the version to read from program space
strcat(command, (const char*)append); // _P is the version to read from program space
Serial.print(F("En overload2 Send ->: "));
Serial.println(command);
/* for (int k=0; k<40; k++){
Serial.print("\n");
Serial.print(k);
Serial.print(" char: ");
Serial.print(command[k]);
Serial.print("\n hex: ");
Serial.println(command[k],HEX);
Serial.print("End command chars: ");
*/
SWserial.println(command);
delay(500);
long wtimer = millis();
while (wtimer + 3000 > millis()) {
while (SWserial.available()>0) {
char c;
c = SWserial.read();
delay(50);
caReceived[i] = c;
//
// Serial.print("\n");
// Serial.print(i);
// Serial.print(" char: ");
// Serial.print(caReceived[i]);
// Serial.print("\n hex: ");
// Serial.println(caReceived[i],HEX);
// delay(50);
i++;
}//endWhile inner
} //endWhile outer
Serial.println();
Serial.print("fnSendRead i Chars read -> i value: ");
Serial.println(i);
caReceived[i]='\0'; //Terminate c string
delay(800);
Serial.print("Ending fnSendRead, caReceived: ");
Serial.println(caReceived);
Serial.println("fnSendRead END ->: ================ \r\n");
return 1;
}
Here an extract of my logfileshowing inconsistencies:
Search "20,8" (51 hits in 1 file of 1 searched)
D:\AsusCloud\Electronica\Arduino\Development\ResDev\Work\Aleman\AlemanOrig328_HBB_Base_EM_V5\Com5Log-1.log (51 hits)
Line 203: 20,800,20 // a truncated response
Line 329: 20,800,2030,10,2023-10-04 11:54:31 // a good one
Line 451: 20,800,2030,10,2023-10-04 11:54:31
Line 573: 20,800,2030,10,2023-10-04 11:54:31
Line 695: 20,800,2030,10,2023-10-04 11:54:31
Line 817: 20,800,2030,10,2023-10-04 11:54:31
Line 939: 20,800,2030,10,2023-10-04 11:54:31
Line 1061: 20,800,2030,10,2023-10-04 11:54:31
Line 1183: 20,800,2030,10,2023-10-04 11:54:31
Line 1305: 20,800,2030,10,2023-10-04 11:54:31
Line 1427: 20,800,2030,10,2023-10-04 11:54:31
Line 1549: 20,800,20
Line 1671: 20,800,2030,10,2023-10-04 11:54:31
Line 1793: 20,800,2030,10,2023-10-04 11:54:31
Line 1915: 20,800,2030,10,2023-10-04 11:54:31
Line 2037: 20,800,2030,10,2023-10-04 11:54:31
Line 2159: 20,800,2030,10,2023-10-04 11:54:31
Line 2281: 20,800,2030,10,2023-10-04 11:54:31
Line 2403: 20,800,2030,10,2023-10-04 11:54:31
Line 2525: 20,800,2030,10,2023-10-04 11:54:31
Line 2647: 20,800,2030,10,2023-10-04 11:54:31
Line 2769: 20,800,2030,10,2023-10-04 11:54:31
Line 2891: 20,800,2030,10,2023-10-04 11:54:31
Line 3013: 20,800,2030,10,2023-10-04 11:54:31
Line 3135: 20,800,2030,10,2023-10-04 11:54:31
Line 3257: 20,800,20
Line 3379: 20,800,2030,10,2023-10-04 11:54:31
Line 3501: 20,800,2030,10,2023-10-04 11:54:31
Line 3623: 20,800,2030,10,2023-10-04 11:54:31
Line 3745: 20,800,2030,10,2023-10-04 11:54:31
Line 3867: 20,800,2030,10,2023-10-04 11:54:31
Line 3989: 20,800,2030,10,2023-10-04 11:54:31
Line 4111: 20,800,2030,10,2023-10-04 11:54:31
Line 4233: 20,800,2030,10,2023-10-04 11:54:31
Line 4355: 20,800,2030,10,2023-10-04 11:54:31
Line 4477: 20,800,2030,10,2023-10-04 11:54:31
Line 4599: 20,800,2030,10,2023-10-04 11:54:31
Line 4721: 20,800,2030,10,2023-10-04 11:54:31
Line 4843: 20,800,2030,10,2023-10-04 11:54:31
Line 4965: 20,800,2030,10,2023-10-04 11:54:31
Line 5087: 20,800,2030,10,2023-10-04 11:54:31
Line 5209: 20,800,2030,10,2023-10-04 11:54:31
Line 5331: 20,800,2030,10,2023-10-04 11:54:31
Line 5453: 20,800,2030,10,2023-10-04 11:54:31
Line 5575: 20,800,2030,10,2023-10-04 11:54:31
Line 5697: 20,800,2030,10,2023-10-04 11:54:31
Line 5819: 20,800,2030,10,2023-10-04 11:54:31
Line 5941: 20,800,2030,10,2023-10-04 11:54:31
Line 6063: 20,800,2030,10,2023-10-04 11:54:31
Line 6185: 20,800,2030,10,2023-10-04 11:54:31
Line 6307: 20,800,2030,10,2023-10-04 11:54:31