Convert Arduino Code to Mbed code

Nope, that is not in communication. It send AT and OK is the positive response, so communication is OK.
I do not know what you are sending but that not seem to be from my code.

BR, Jan

I send HELLO from my phone

the process that youhave create is to send AT , get OK
then put AT+CMGF=%d , get OK

My example is not able to receive any SMS, at the moment it can only be sent.

    if(modulInit()){ // if  your SIM in A9G is locked by a pin, then fill your pin code as parameter, for example modulInit(1234)
        if(sendSMS("+123123456789", "Testerino")){ // here as first parameter fill your phone number where the SMS is to be sent 
            printf("SMS sended\n");
        }
    }
  • The function modulInit(int simPin) sends AT command for communication test, tries to unlock SIM card with AT+CPIN (when simPin parameter is not zero, otherwise skip) and then is waiting for Network registration via +CREG (that is peridically sended by module and informs about Network status).
    When all is done then returns true or otherwise false.
  • When modulInit was successful, then the function sendSMS(string phoneNum, string text) set module to text mode via AT+CMGF and the send the content of variable text to number from variable phoneNum via AT+CMGS

BR, Jan

I Still have the same error
123

  • AT+CPIN command returns error because incorrect format probably. The command should look like this AT+CPIN=1234 where the number 1234 is a fake Pin code of a SIM card.
  • also AT+CMGF command returns error because incorrect format probably. The command should look like this AT+CMGF=1, where number 1 is a text format mode for SMS.

Below you can see my complete report as an output of my code

Description of Parser’s debug messages:

  • AT> - Mbed send
  • AT? - Mbed is waiting for a specific response until timeout
  • AT< - Mbed received a response
  • AT= - Mbed received the expected response
  • AT(Timeout)
Mbed A9G test
AT> AT
AT? OK%n
AT< AT
AT= OK
Module is up!
AT> AT+CPIN=1234
AT? OK%n
AT< 
AT< AT+CPIN=1234
AT= OK
Sim unlocked
Connecting to network...
AT? +CREG: %*d%n
AT< 
AT(Timeout)
State: timeout
AT? +CREG: %*d%n
AT< +CIEV: service,  0
AT< +CIEV: roam, 0
AT= +CREG: 2
State: not registered, but is searching...
AT? +CREG: %*d%n
AT< 
AT< +CIEV: service,  1
AT< +CIEV: roam, 0
AT= +CREG: 1
Connected
AT> AT+CMGF=1
AT? OK%n
AT< 
AT< AT+CMGF=1
AT= OK
Sms mode was set to 1
AT> AT+CMGS="+123123456789"
AT? >%n
AT< 
AT< AT+CMGS="+123123456789"
AT= >
Sms mode was set to 1
AT> Testerino
[1A]
AT? +CMGS: %*d%n
AT<  Testerino
AT< [1A]
AT= +CMGS: 4
Sms was send to +123123456789 under id: 1 
SMS sended

BR, Jan

even if i put this it still The same error i dont now what is the reason of probleme

Do you have another board (Nucleo ideal)? Or an USB to Serial adapter like on this picture (usb ttl converter)?

BR, Jan

Yes i have usb TTL

I want to know what is the mbed programmer you use ?
for me i use the ST LINK utility THEN i open my serial monitor and i select the Com . i dont receive “Mbed A9G test”

hello sir
I want to know which programmer you are using
For me I use st link utility then I open the terminal serie then I start put my commands AT
I don’t know if I’m not following the right things

I use standard ST-link V2 what is included on Nucleo board. So I only drag & drop binary file to a Flash memory in Windows manager, but it does not matter.

Yes, you do not receive “Mbed A9G test” because you are not connected to STDO console. So take your USB TTL and connect its TX pin to PA_3 pin of BluePhill and RX pin to PA_2 of BluePhil. Then connect to its COM port and you will see it.

BR, Jan

For that you not need Mbed for that is exactly good USB TTL, you can connect it directly to A9G module and comunicate directly with it from PC Serial terminal.
I usually did it like that before I start to make an app with Mbed’s ATCmdParser. Because I want to understand how commands works and so on.

BR, Jan

Wow, what a great sharing!

Hello sir hope your doing well so the code works for me i can communicate with A9g module .
and now i try to create library basing on this code but i m facing a lot of problems when i create class A9G .

Hello again,

I’m glad to hear you’ve moved on :slight_smile:
Good description is first step to a solution. So, what problems do you have?

BR, Jan

A9G.h
#ifndef A9G_H
#define A9G_H
#define DEBUG 1
#include <stdio.h>
#include “mbed.h”

#define DEFAULT_TIMEOUT 5
#define SMS_MAX_LENGTH 16
class A9G
{
public:
/** Create GSM instance
* @param tx uart transmit pin to communicate with GSM module
* @param rx uart receive pin to communicate with GSM module
* @param baudRate baud rate of uart communication
* @param number default phone number during mobile communication
*/
A9G(PinName A , PinName D, int baudRate,char *number) ;

int init(void);

/** Check SIM card' Status
 *  @returns
 *      0 on success,
 *      -1 on error
 */
 int powerCheck(void);
/** init GSM module including SIM card check & signal strength & network check
 *  @returns
 *      0 on success,
 *      -1 on error
 */

int checkSIMStatus(void);

/** Check signal strength
 *  @returns
 *      signal strength in number(ex 3,4,5,6,7,8...) on success,
 *      -1 on error
 */
int checkSignalStrength(void);

/** Set SMS format and processing mode
 *  @returns
 *      0 on success,
 *      -1 on error
 */
int settingSMS(void);

/** Send text SMS
 *  @param  *number    phone number which SMS will be send to
 *  @param  *data   message that will be send to
 *  @returns
 *      0 on success,
 *      -1 on error
 */
int sendSMS(char *number, char *data);

/** Read SMS by index
 *  @param  *message   buffer used to get SMS message
 *  @param  index    which SMS message to read
 *  @returns
 *      0 on success,
 *      -1 on error
 */
int readSMS(char *message, int index);

/** Delete SMS message on SIM card
 *  @param  *index    the index number which SMS message will be delete
 *  @returns
 *      0 on success,
 *      -1 on error
 */
int deleteSMS(int index);

/** Read SMS when coming a message,it will be store in messageBuffer.
 *  @param message  buffer used to get SMS message
 */
int getSMS(char* message);

/** Call someone
 *  @param  *number    the phone number which you want to call
 *  @returns
 *      0 on success,
 *      -1 on error
 */
 #endif

A9G.cpp
#include “mbed.h”
#include
#define DEBUG 1
#include"A9G.h"
/* !!!Change pins according to your connection!!! */

int A9G::A9G(PinName A , PinName D, int baudRate) :gprsSerial (tx,rx, baudRate)
{
int i = 0;
timeCnt.start(); // start timer
while(1) {
while (gprsSerial.readable()) {
char c = gprsSerial.getc();
if (c == ‘\r’ || c == ‘\n’) c = ‘$’;
buffer[i++] = c;
if(i > count)break;
}
if(i > count)break;
if(timeCnt.read() > DEFAULT_TIMEOUT) {
timeCnt.stop();
timeCnt.reset();
break;
}
}
wait(0.5);
while(gprsSerial.readable()) { // display the other thing…
char c = gprsSerial.getc();
}
return 0;
}

void cleanBuffer(char *buffer, int count)
{
for(int i=0; i < count; i++) {
buffer[i] = ‘\0’;
}
}

}

bool modulInit(int simPin);
bool sendSMS(string phoneNum, string text);

// setup
int main() {
printf(“Mbed A9G test\n”);
module.debug_on(true);
module.set_timeout(1000);

if(modulInit(0)){ // replace 1234 with your Pin code of your SIM card or replace it with 0, when your SIM have no Pin code.
    if(sendSMS("+212605686054", "Testerino")){ // replace +123123456789 with destination Phone number
        printf("SMS sended\n");
    }
}

// loop
while(true)
{
    //do something in loop
    thread_sleep_for(1000);
}

}

bool modulInit(int simPin){
bool result = true;
// power on
for(int i=0;i<5;i++){
module.send(“AT”);
thread_sleep_for(500);
if(module.recv(“OK”)){
debug_if(DEBUG, “Module is up!\n”);
break;
} else {
debug_if(DEBUG, “No communication with module!\n”);
return false;
}
}
// SIM autentication
if(simPin != 0){
module.send(“AT+CPIN=%d”, simPin);
if(module.recv(“OK”)){
debug_if(DEBUG, “Sim unlocked\n”);
} else {
debug_if(DEBUG, “SIM locked!\n”);
return false;
}
}

// Wait for network registration
string network_status[] = { "not registered, not searching", 
                            "registered, home network", 
                            "not registered, but is searching", 
                            "registration denied", 
                            "unknown", 
                            "registered, roaming"};
int state = 0;
debug_if(DEBUG, "Connecting to network...\n");
module.set_timeout(10000);
for(int i=0;i<5;i++){
    if(module.recv("+CREG: %d",&state)){
        if(state == 1)break;
        debug_if(DEBUG, "State: %s...\n",network_status[state].c_str());
    } else {
        debug_if(DEBUG, "State: timeout\n");
    }
    thread_sleep_for(500);
}  
if(state == 1){
    debug_if(DEBUG, "Connected\n");
}else{
    debug_if(DEBUG, "Connection failed!\n");
    return false;
}
thread_sleep_for(1000);
module.set_timeout(1000);    
return true;

}

bool sendSMS(string phoneNum, string text){
bool result = true;
// set mode to text mode
int mode = 1;
module.send(“AT+CMGF=%d”, mode);
if(module.recv(“OK”)){
debug_if(DEBUG, “Sms mode was set to %d\n”, mode);
} else {
debug_if(DEBUG, “Set mode failed!\n”);
}

module.set_timeout(10000);
module.send("AT+CMGS=\"%s\"", phoneNum.c_str());
if(module.recv(">")){
    debug_if(DEBUG, "Sms mode was set to %d\n", mode); 
    module.send("%s\n%c", text.c_str(), 0x1A); 
    int id = 0;
    if(module.recv("+CMGS: %d", &id)){
        debug_if(DEBUG, "Sms was send to %s under id: %d \n",phoneNum.c_str(), id); 
    } else {
        debug_if(DEBUG, "Sms send failed\n"); 
        result = false;
    }
} else {
    debug_if(DEBUG, "Sms send failed\n");
    result = false;
}
module.set_timeout(1000);  
return result;

}

i don’t know how to define this in library
BufferedSerial uart(D2,D8, 115200); // UART TX, RX
ATCmdParser module(&uart,“\r\n”);

class A9G{
public:
   A9G(PinName tx, PinName rx, bool atDebug, int baud);

// rest of public members
...
private:
   BufferedSerial _uart;
   ATCmdParser _module;
// rest of private members
...
}

// both inicialize in con
A9G::A9G(PinName tx, PinName rx, bool atDebug, int baud):_uart(tx,rx,baud), _module(&_uart){}

I reworked my example code from above to an example with a library - Mbed_A9G_test - Test | Mbed

BR, Jan