Blinking Led Using Timer Freezes During Communication with GSM Modem

Hi PaulRB. Apologies for the delay. I was stuck with my regular job and did not have time to play with my Arduino.

I am posting the simplified (relevant) code below. When the uploadGprs() function is not running, my led blinks perfectly. As soon as this function starts, the led blinking stops, because the arduino is busy running the uploadGprs code, and does not get a chance to update the LED.

The uploadGprs uses the ArduinoSIM800L library to communicate with the GPRS model via the UART using simple AT commands. However, it does wait for about 2 seconds between commands giving the modem time to respond. The entire function takes about 15 seconds to complete, during which time the LED does not blink.

I will appreciate any suggestions to get round this. Thanks!

#include <Http.h> // ArduinoSIM800L library

// typedef for tast scheduling
struct t  {
    unsigned long tStart;
    unsigned long tTimeout;
};

// define pins
int pinLedGprs = 5;
int pinGprsRx = 16;
int pinGprsTx = 17;
int pinGprsRst = 23;

// define led states
int stateLedGprs = LOW;

// led blink rates
unsigned long blinkFast = 100;
unsigned long blinkSlow = 500;
unsigned long blinkOff = 0;

// task timers
t t_blinkLedGprs = {0, blinkFast};
t t_gprsUpload = {0, 60000}; // run every 60 seconds

// sim800
HTTP* sim800;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(pinLedGprs, OUTPUT);
  sim800 = new HTTP(9600, pinGprsRx, pinGprsTx, pinGprsRst);
  Serial.println("=== STARTING APPLICATION ===");
}

void loop() {

  // task to upload the gprs data
  if (tCheck(&t_gprsUpload)) {
    Serial.println("*** Starting GPRS UPLOAD function ***");
    gprsUpload();
    tRun(&t_gprsUpload);
  }
  
  
  // task. set led state
  if ( t_blinkLedGprs.tTimeout > 0) {
    if (tCheck(&t_blinkLedGprs)) {
      stateLedGprs = !stateLedGprs;
      tRun(&t_blinkLedGprs);
    } 
  } else {
    stateLedGprs = HIGH;
  }

  // update the led state
  digitalWrite(pinLedGprs, stateLedGprs);
}

void gprsUpload(void) {

  const char* jsonData = "{\"test\"}"; 
  
  char response[2048];
  Result result;

  sim800->configureBearer("airtelgprs.com");
  result = sim800->connect();

  result = sim800->post("http://my.web.api/", jsonData, response);
  if (result == SUCCESS) {
    // Set the status led.
    t_blinkLedGprs.tTimeout = blinkOff;
    Serial.println("Successfully posted to api.");
    Serial.println(response);
  } else {
    Serial.println("Error in posting to api.");
    t_blinkLedGprs.tTimeout = blinkSlow;
    Serial.println(response);
  } 
  sim800->disconnect();
}

// used to periodically check task run timer status.
bool tCheck (struct t *t ) {
  
  if (millis() > t->tStart + t->tTimeout) {
    return true;
  } else {
    return false;
  }
 
}

// used to set when the task was last run.
void tRun (struct t *t) {
    t->tStart = millis();
}