1

I'm using two SIM908 shields to communicate with each other. One of the shields will be connected to a sensor and it should send values contentiously to the second SIM908 (receiver). I'm facing a problem on the receiver side, I'm only able to receive the first SMS and when sending a new SMS it displays the older one.

The code that I'm using is from (cooking-hacks.com) where I bought the shield. In the original code the instructions were in void setup function so I moved them to void loop function to keep checking for new SMS and print any received values. I also made other minor changes. Here is the code

int8_t answer;
int x;
int onModulePin= 2;
char aux_string[30];
char SMS[200];
float fSMS;
char data[20];

void setup() {
  pinMode(onModulePin, OUTPUT);
  Serial.begin(115200);
  Serial.println("Starting...");
  power_on();
}

void loop() {
  Serial.println("Setting SMS mode...");
  sendATcommand("AT+CMGF=1", "OK", 1000);
  // sets the SMS mode to text
  sendATcommand("AT+CPMS=\"SM\",\"SM\",\"SM\"", "OK", 1000);
  // selects the memory
  answer = sendATcommand("AT+CMGR=1", "+CMGR:", 2000);
  // reads the first SMS (since all other messages are deleted from previous delete statement)
  if (answer == 1) {
    answer = 0;
    while(Serial.available() == 0);
    // this loop reads the data of the SMS
    do {
      // if there are data in the UART input buffer, reads it and checks for the answer
      if(Serial.available() > 0) {
        SMS[x] = Serial.read();
        x++;
        // check if the desired answer (OK) is in the response of the module
        if (strstr(SMS, "OK") != NULL) {
          answer = 1;
        }
      }
    }while(answer == 0);
    // Waits for the answer with timeout
    SMS[x] = '\0';
    /*  strtok(SMS, ",");
    strtok(NULL, ",");
    strtok(NULL, ",");
    strtok(NULL, "\n");
    strcpy(data,strtok(NULL, ""));*/
    Serial.println(SMS);
    //Serial.println(data);
    /*    fSMS= atof (data);
    Serial.println(fSMS, 5);*/
  } else {
    Serial.print("error ");
    Serial.println(answer, DEC);
  }
  Serial.println("AT+CMGDA= \"DEL ALL\"");
  //delete all messages from SIM card
  delay(5000);
}

void power_on() {
  uint8_t answer=0;
  // checks if the module is started
  answer = sendATcommand("AT", "OK", 2000);
  if (answer == 0) {
    // power on pulse
    digitalWrite(onModulePin,HIGH);
    delay(3000);
    digitalWrite(onModulePin,LOW);
    // waits for an answer from the module
    while(answer == 0){
      // Send AT every two seconds and wait for the answer
      answer = sendATcommand("AT", "OK", 2000);
    }
  }
}

int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout) {
  uint8_t x=0,  answer=0;
  char response[100];
  unsigned long previous;
  memset(response, '\0', 100);
  // Initialice the string
  delay(100);
  while( Serial.available() > 0) Serial.read();
  // Clean the input buffer
  Serial.println(ATcommand);
  // Send the AT command
  x = 0;
  previous = millis();
  // this loop waits for the answer
  do {
    // if there are data in the UART input buffer, reads it and checks for the answer
    if(Serial.available() != 0) {
      response[x] = Serial.read();
      x++;
      // check if the desired answer is in the response of the module
      if (strstr(response, expected_answer) != NULL) {
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  } while((answer == 0) && ((millis() - previous) < timeout));
  return answer;
}
  • Did you get an answer to this? Looking to find solution to similar problem. It gets stuck at the first AT command it encounters. Is there any special library that needs to be included? Thanks – Raj Feb 14 '17 at 0:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.