Arm Mbed OS support forum

Sending AT Commands

Hi,

i’m trying to send commands to my SHIELD following the AT protocol.
I have the following hardware.

  • DISCO-L475VG-IoT-01A
  • Shiratech IoT Shield with the quectel BG96

My simplified code looks like this.

#include "mbed.h"

#include "ATCmdParser.h" 

#include "UnbufferedSerial.h"

ATCmdParser *at;

UnbufferedSerial *us;

int main()

{

    printf("\nAT Commando's \n\n");

    us = new UnbufferedSerial(D1, D0);

    at = new ATCmdParser(us, "\r\n");

    char buffer[1024];

    // Enable debugging.

    at->debug_on(1);

    // Set to default timeout of the quectel. 

    at->set_timeout(300);

    // Send this line to the AT.

    at->send("AT+GMI=?");

    // Received ok? 

    if (at->recv("OK")){

        printf("[AT+GMI=?] Returned result %s", buffer);

    } else {

        printf("[AT+GMI=?] Request failed");

    }

}

And this is the result im getting:

afbeelding

(NOTE: it does not print my result).

Please help me :slight_smile:

Hello Koen,

I’m not sure why (most likely because the serial console used for debugging and the UnbufferedSerial used for communication with the modem share the same serial line) but appending \r\n as below could help:

printf("[AT+GMI=?] Request failed\r\n");

Few additional comments:

  • According to the user manual the communication with the shield is based on the UART protocol using a baud rate of 115200 baud. So after creating an UnbufferedSerial object change the bit rate:
us = new UnbufferedSerial(D1, D0);
us->baud(115200);
  • I’m not sure how the ATCmdParser's debug feature works but because the DISCO’s serial console is connected to same pins (serial line) as the Shiratech shield UART you’d probably need to adjust it’s speed too using an mbed_app.json configuration file. For example:
{
    "target_overrides": {
        "*": {
            "platform.stdio-baud-rate": 115200
        }
    }
}

Best regards, Zoltan

Hey Zoltan,

Thanks!
I now do indeed get a return, but sadly it is an Request Failed.
afbeelding
On your comment on the baudrate. that should be all the same now.
Do you have any idea how I can make it work?

Best regards,

Koen

Hello Koen,

Since the serial console shares the serial line with the shield you can try to send AT commands to the shield manually from the serial terminal running on your PC without having the ST MCU on the DISCO board involved:

  • Disconnect the shield from the DISCO board.

  • Connect the shield to the DISCO board with wires as follows:

------------------
| DISCO | Shield |
------------------
| D0    | D1     |
| D1    | D0     |
| GND   | GND    |
| +5V   | +5V    |
| +3.3V | +3.3V  |
------------------
  • Program your DISCO board to do nothing. For example:
#include "mbed.h"

int main() {}
  • Set the serial terminal speed to 115200 and configure it to append a “carriage return + new line” to the message to be sent.

  • Send an AT command manually by typing it to your serial monitor. For example:

AT

Hit ENTER or click on the “Send” button (depends on the used serial terminal).

  • The shield’s response, if any, should be then sent back to the serial terminal.

  • If it works remove the wires and plug the shield to the DISCO board.

  • Try your program again but remove/comment out:

at->set-timeout(300);

Best regards, Zoltan

Hey Zoltan,

The commant above has not been working out as expected.
I connected the wires like u told me to, it seems to power on, but not communicate.
I’ve tried to send commands via the terminal, but is doesn’t pick them up. with the shield placed normally and following your suggestion.
Also i’ve tried my other program that should be able to connect to the BG96, but also there inline terminal command’s are not received.

Do you have any other suggestions, because i cannot figure this out.

Best regards,
Koen

– update –
Thanks to a colleague I now have an ATHandler, this should be able to send a command to the modem.
I can see it appear in the modem logs, but it still doesn’t respond to it.

Hello Koen,

If you have an Arduino Uno try to follow the steps suggested in section “4. Using the Shield for the First Time” of the user manual.

Best regards, Zoltan

Hey Zoltan,

I finally cracked the code!

mbed_trace_init();
CellularInterface  *net = CellularInterface::get_default_instance();
CellularDevice * dev = CellularDevice::get_default_instance();
ATHandler * devat = dev->get_at_handler();
devat->cmd_start("ATI");
devat->cmd_stop_read_resp();

This solved my problem, I now can communicate with the modem,
Thanks for thinking with me!