Ivo Hora
Ivo Hora

Reputation: 49

Zephyr RTOS Send SMS via SIMCOM SIM800L

I am trying to send SMS from my code. I assembled AT commands according to SIM800L datasheet and internet guidance, but I cannot send any. No matter what I do, it seems like I cannot register to GSM network. I tried 2 identical modems, I tried 2 different working sim cards (Vodafone operator and Kaktus operator), but seems like both are not willing to register.

If I start the MCU it prints out: [00:00:06.813,751] modem_sim800l: sim800l_rx: Thread started
[00:00:06.813,812] modem_sim800l: sim800l_send_at_cmd: OUT: [AT+COPS=0]
[00:00:06.843,688] modem_sim800l: sim800l_input_append: Modem response: AT+COPS=0
[00:00:06.848,846] modem_sim800l: sim800l_input_append: Modem response: OK
[00:00:08.528,442] modem_sim800l: sim800l_input_append: Modem response: Call Ready
[00:00:11.848,968] modem_sim800l: sim800l_send_at_cmd: OUT: [AT+CREG?]
Since I failed with manual registration with command AT+COPS=1,2,"23003". I tried automatic registration with command AT+COPS=0 to select network automatically, but when requesting the network status with AT+CREG?, the MCU hangs with no buffer to be filled in after modem response therefore I cannot see whether the modem is connected to network. Does anyone know how to connect to GSM network properly or what the problem might be? Command AT+COPS=? shows 2 networks and one is Vodafone 23003, but I cannot register to the network. The modem is blinking once per second which should be sign of network connection, but If I remove SIM card, it's blinking the same way. Moreover the command responses sometimes hangs and I have to restart the MCU, I don't know why, maybe it is an issue related to UART, but with Arduino, the same installations works without any problems. I am enclosing code snippets how things are done.

Thank you for you response

   #define AT_CMD_AT               "AT"
   #define AT_CMD_ATH              "ATH"
   #define AT_CMD_CREG             "AT+CREG?"
   #define AT_CMD_CPAS             "AT+CPAS"
   #define AT_CMD_CMGF             "AT+CMGF=1"
   #define AT_CMD_CMG_DEST "AT+CMGS=\"+420734300543\""
   #define AT_CMD_CMG_TEXT             "hello\x1A"
   #define AT_CMD_CMG_END "\x1A"
   //#define AT_CMD_CMGS_TEXT             "\rhello\032"
   #define AT_CMD_COPS             "AT+COPS=?"
   #define AT_CMD_COPN             "AT+COPN"
  //#define AT_CMD_COPS_REGISTER "AT+COPS=0,2,\"23003\""
 //#define AT_CMD_COPS_REGISTER "AT+COPS=1,2,\"23003\""
  #define AT_CMD_COPS_REGISTER "AT+COPS=0"
 //#define AT_CMD_COPS_REGISTER "AT+COPS=0"
 //#define AT_CMD_COPS_REGISTER             "AT+COPS=1"


static void sim800l_input_append(uint8_t *buf, size_t size_buf) {
static bool end_of_line_appeared;

for (size_t i = 0; i < size_buf; i++) {
    if (buf[i] != '\r') {
        if (buf[i] != '\n') {
            end_of_line_appeared = false;
            string_buf[string_index++] = buf[i];
        }
    } else if (!end_of_line_appeared) {
        string_buf[string_index] = '\0';
        //LOG_DBG("Modem response: %s", log_strdup(string_buf));
        LOG_DBG("Modem response: %s", string_buf);
        sim800l_input_parse(string_buf);
        string_index = 0;
        end_of_line_appeared = true;
    }
}
//LOG_DBG("string_buf: %s", string_buf);
if (0 == strcmp(string_buf, buffer)) {
    LOG_DBG("prompt ready");
    k_sem_give(&resp_ready);
}

}

static void sim800l_rx(void) {
uint8_t uart_buffer[128];
size_t bytes_read;
int ret;
LOG_DBG("Thread started");

while (true) {
    /* wait for incoming data */
    //LOG_DBG("Thread waiting");
    (void) k_sem_take(&ictx.rx_sem, K_FOREVER);
    //LOG_DBG("Thread released");

    ret = mdm_receiver_recv(&ictx, uart_buffer, sizeof(uart_buffer),
            &bytes_read);
    /*
     for (int i = 0; i < bytes_read; i++){
     LOG_DBG("Buffer content: %d", uart_buffer[i]);
     }
     */
    if (ret < 0 || bytes_read == 0) {
        /* mdm_receiver buffer is empty */
        continue;
    }

    sim800l_input_append(uart_buffer, bytes_read);
}

}

void sim800l_send_at_cmd(const char *at_cmd, size_t size_at_cmd) {
//LOG_DBG("OUT: [%s]", log_strdup(at_cmd));
LOG_DBG("OUT: [%s]", at_cmd);
mdm_receiver_send(&ictx, at_cmd, size_at_cmd);
mdm_receiver_send(&ictx, "\r\n", 2);
(void) k_sem_take(&resp_ready, K_FOREVER);
//LOG_DBG("Thread released");

}

int sim800l_init(void) {
sprintf(buffer, "%s", AT_CMD_CMG_DEST);
//buffer[0] = 13;
//buffer[1] = 10;
buffer[0] = 62;
buffer[1] = 32;
int ret = 0;
k_sleep(K_MSEC(3000));

ret = mdm_receiver_register(&ictx, MDM_UART_DEV, mdm_recv_buf,
        sizeof(mdm_recv_buf));

if (ret < 0) {
    LOG_ERR("Error registering modem receiver (%d)!", ret);
    LOG_DBG("Error registering modem receiver (%d)!", ret);
    return ret;
}

/* start RX thread */
k_thread_name_set(
        k_thread_create(&sim800l_rx_thread, sim800l_rx_stack,
                K_THREAD_STACK_SIZEOF(sim800l_rx_stack),
                (k_thread_entry_t) sim800l_rx, NULL, NULL, NULL,
                RX_THREAD_PRIORITY, 0, K_NO_WAIT), "sim800l rx");

//sim800l_send_at_cmd(AT_CMD_AT, sizeof(AT_CMD_AT) - 1);
//sim800l_send_at_cmd(AT_CMD_COPN, sizeof(AT_CMD_COPN) - 1);
// sim800l_send_at_cmd(AT_CMD_COPS, sizeof(AT_CMD_COPS) - 1);
sim800l_send_at_cmd(AT_CMD_COPS_REGISTER, sizeof(AT_CMD_COPS_REGISTER) - 1);
k_sleep(K_MSEC(5000));
sim800l_check_connection();
/*
 k_sleep(K_MSEC(1000));
 //k_sleep(K_MSEC(1000));
 k_sleep(K_MSEC(1000));
 sim800l_send_at_cmd(AT_CMD_CMGF, sizeof(AT_CMD_CMGF) - 1);
 k_sleep(K_MSEC(3000));
 //k_sleep(K_MSEC(5000));
 //sim800l_send_at_cmd(AT_CMD_AT, sizeof(AT_CMD_AT) - 1);
 while (SMS_READY == true) {
 */
//sim800l_send_at_cmd(AT_CMD_CMGS_DEST, sizeof(AT_CMD_CMGS_DEST) - 1);
//k_sleep(K_MSEC(1000));
/*
 (void)k_sem_take(&sms_ready, K_FOREVER);
 sim800l_send_at_cmd(AT_CMD_CMGF, sizeof(AT_CMD_CMGF) - 1);
 sim800l_send_at_cmd(AT_CMD_CMG_DEST, sizeof(AT_CMD_CMG_DEST) - 1);
 sim800l_send_at_cmd(AT_CMD_CMG_TEXT, sizeof(AT_CMD_CMG_TEXT) - 1);
 */

return ret;

}

Upvotes: 1

Views: 330

Answers (0)

Related Questions