I have an Arduino project that uploads data to a server and retrieves data from the same server. The upload and download are two separate functions. My problem is when I run the code for the first time it completes the upload function successfully the completes the download function successfully as well but then at the second iteration of the loop I get the "ERROR" response when sending the "AT+HTTPACTION=1" command to the sim800l module. After this, it will successfully complete the download function but fail the upload function. This is the code below:
void setup() {
Serial.begin(9600);
//15 second delay to allow GSM to register on network
delay(15000);
pinMode(LED_BUILTIN, OUTPUT);
sim800l.begin(9600);
delay(2000);
gsm_config_gprs(); //sets up the GPRS parameters for my carrier
delay(5000);
}
void loop() {
get_vehicle_status(); //the function that downloads from the server
gsm_http_post(postData); //the function that uploads data to the server
String postData;
postData = "temp=23.4&hum=60";
}
void gsm_http_post(String postData) {
Serial.println(" --- Start GPRS & HTTP --- ");
gsm_send_serial("AT+SAPBR=1,1", 1000);
gsm_send_serial("AT+SAPBR=2,1", 3000);
gsm_send_serial("AT+HTTPINIT", 1000);
gsm_send_serial("AT+HTTPPARA=CID,1", 1000);
gsm_send_serial("AT+HTTPPARA=URL," + url, 500);
gsm_send_serial("AT+HTTPPARA=CONTENT,application/x-www-form-urlencoded", 1000);
gsm_send_serial("AT+HTTPDATA=" + postLength + ",5000", 1000);
gsm_send_serial(postData, 1000);
gsm_send_serial("AT+HTTPACTION=1", 5000);
gsm_send_serial("AT+HTTPTERM", 1000);
gsm_send_serial("AT+SAPBR=0,1", 3000);
}
void get_vehicle_status() {
Serial.println("Fetching vehicle status... ");
gsm_send_serial("AT+SAPBR=1,1", 1000);
gsm_send_serial("AT+SAPBR=2,1", 3000);
gsm_send_serial("AT+HTTPINIT", 1000);
gsm_send_serial("AT+HTTPPARA=CID,1", 1000);
gsm_send_serial("AT+HTTPPARA=URL," + url_1, 1000);
gsm_send_serial("AT+HTTPACTION=1", 5000);
gsm_send_serial_read("AT+HTTPREAD", 500);
gsm_send_serial("AT+HTTPTERM", 1000);
gsm_send_serial("AT+SAPBR=0,1", 3000);
}
void gsm_config_gprs() {
Serial.println(" --- CONFIG GPRS --- ");
gsm_send_serial("AT+SAPBR=3,1,Contype,GPRS", 1000);
gsm_send_serial("AT+SAPBR=3,1,APN," + apn, 2000);
if (apn_u != "") {
gsm_send_serial("AT+SAPBR=3,1,USER," + apn_u, 3000);
}
if (apn_p != "") {
gsm_send_serial("AT+SAPBR=3,1,PWD," + apn_p, 3000);
}
}