Convert Arduino Code to Mbed code

A9G Low Power GSM/GPRS+GPS Module (Seeduino arch pro )

Code Arduino :
#include<stdio.h>
#include<string.h>

#define DEBUG true

int PWR_KEY=9;
int RST_KEY=6;
int LOW_PWR_KEY=5;

int LED_R = 2; //D2 RED
int LED_G = 3; //D3 GREEN
int LED_B = 4; //D4 BLUE

String msg = String(“”);
int SmsContentFlag = 0;

void setup()
{

  pinMode(PWR_KEY, OUTPUT);
  pinMode(RST_KEY, OUTPUT);
  pinMode(LOW_PWR_KEY, OUTPUT);
  digitalWrite(RST_KEY, LOW);
  digitalWrite(LOW_PWR_KEY, HIGH);   
  digitalWrite(PWR_KEY, HIGH);

  pinMode(LED_R, OUTPUT); 
  pinMode(LED_G, OUTPUT);
  pinMode(LED_B, OUTPUT);
  digitalWrite(LED_R, HIGH);
  digitalWrite(LED_G, HIGH);
  digitalWrite(LED_B, HIGH);

// String msg = String(“”);
// int SmsContentFlag = 0;

  SerialUSB.begin(115200);
    while (!SerialUSB) {
    ; // wait for Arduino serial port to connect
  }
  Serial1.begin(115200);
    modulePowerOn();
  GprsTextModeSMS();

}

void loop()
{
char SerialInByte;
if(SerialUSB.available())
{
Serial1.print((unsigned char) SerialUSB.read());
}
else if(Serial1.available())
{
//char SerialInByte;
SerialInByte = (unsigned char)Serial1.read();
if( SerialInByte == 13 )//0x0D
{
ProcessGprsMsg();
}
if( SerialInByte == 10 )
{
}
else
{
//EN: store the current character in the message string buffer
msg += String(SerialInByte);
}
}
}

void modulePowerOn()
{
int i=0;
boolean result = false;
digitalWrite(PWR_KEY, LOW);
delay(3000);
digitalWrite(PWR_KEY, HIGH);
delay(15000);

    result =  Serial1.find("OK");
    if(result)
        SerialUSB.println( "Please send sms to control your device!" );
else
    {
            for(i=0;i<10;i++)
            {
                    Serial1.println("AT");
                    delay(500);
                    result =  Serial1.find("OK");
                    if(result)
                    {
                            SerialUSB.println( "Please send sms to control your device!" );
                            return;
                    }
     }
    }

}

void ProcessSms( String sms )
{
SerialUSB.print( “ProcessSms for [” );
SerialUSB.print( sms );
SerialUSB.println( “]” );
if( sms.indexOf(“onr”) >= 0)
{
digitalWrite(LED_R, LOW);
SerialUSB.println( “LED Red ON” );
//return;
}
if( sms.indexOf(“ong”) >= 0)
{
digitalWrite( LED_G, LOW);
SerialUSB.println( “LED Green ON”);
//return;
}
if( sms.indexOf(“onb”) >= 0 )
{
digitalWrite(LED_B, LOW);
SerialUSB.println( “LED Blue ON” );
//return;
}
if( sms.indexOf(“offr”) >= 0 )
{
digitalWrite(LED_R,HIGH);
SerialUSB.println( “LED Red OFF” );
//return;
}
if(sms.indexOf(“offg”) >= 0 )
{
digitalWrite(LED_G, HIGH );
SerialUSB.println( “LED Green OFF” );
//return;
}
if( sms.indexOf(“offb”) >= 0 )
{
digitalWrite(LED_B, HIGH );
SerialUSB.println( “LED Blue OFF” );
//return;
}
}

// EN: Request Text Mode for SMS messaging
void GprsTextModeSMS()
{
Serial1.println( “AT+CMGF=1” );
}

void GprsReadSmsStore( String SmsStorePos )
{
// Serial.print( "GprsReadSmsStore for storePos " );
// Serial.println( SmsStorePos );
Serial1.print( “AT+CMGR=” );
Serial1.println( SmsStorePos );
}

// EN: Clear the GPRS shield message buffer
void ClearGprsMsg()
{
msg = “”;
}

// EN: interpret the GPRS shield message and act appropiately
void ProcessGprsMsg()
{
SerialUSB.println(“”);
// Serial.print( “GPRS Message: [” );
SerialUSB.print( msg );
// Serial.println( “]” );
if( msg.indexOf( “Call Ready” ) >= 0 )
{
SerialUSB.println( “*** GPRS Shield registered on Mobile Network ***” );
GprsTextModeSMS();
}

    //EN: unsolicited message received when getting a SMS message
    if( msg.indexOf( "+CIEV" ) >= 0 )
    {
            SerialUSB.println( "*** SMS Received ***" );
    }

    //EN: SMS store readed through UART (result of GprsReadSmsStore request)  
    if( msg.indexOf( "+CMT:" ) >= 0 )
    {
            // EN: Next message will contains the BODY of SMS
            SmsContentFlag = 1;
            // EN: Following lines are essentiel to not clear the flag!
            ClearGprsMsg();
            return;
    }

    // EN: +CMGR message just before indicate that the following GRPS Shield message 
    //     (this message) will contains the SMS body
    if( SmsContentFlag == 1 )
    {
            SerialUSB.println( "*** SMS MESSAGE CONTENT ***" );
            SerialUSB.println( msg );
            SerialUSB.println( "*** END OF SMS MESSAGE ***" );
            ProcessSms( msg );
    }

    ClearGprsMsg();
    //EN: Always clear the flag
    SmsContentFlag = 0; 

}
hhhhhhh.PNG

Hello,

welcome here, but I don’t think this would be a suitable form of request for help.

You can visit Mbed website and look what you need for your project. When you stuck somewhere do not be hesitate to ask.

I do not see Seeeduino-Arch-Pro | Mbed on your picture, it seems to be a Maduino Zero A9G GPRS/GPS and that is probably not compatible with Mbed.

BR, Jan

Firstly thanks for your response .
Yes the maduinoo Zero not compatible with Mbed so I use Nucleo STm32F103RBT6 Board and i have to connect it with GPRS/GPS A9G module , Idont have ideas how to define this in mbed .

I have to make the A9G GPRS/GPS module compatible with Mbed

How exactly do you connect the F103 directly to the A9G (when it is on board with a SAM MCU)?
Here is a starting point, but until you see the message Module is up! there is no point in continuing.

Piece of code
#include "mbed.h"

/* !!!Change pins according to your connection!!! */
BufferedSerial uart(D10,D2, 115200); // UART TX, RX
ATCmdParser module(&uart,"\r\n");


// setup
int main() {
    printf("Mbed A9G test\n");
    module.debug_on(true);
    bool result = false;
    for(int i=0;i<5;i++){
        module.send("AT");
        thread_sleep_for(500);
        result =  module.recv("OK");
        if(result){
            break;
        }
    }
    if(result){
        printf("Module is up!\n");  
    } else {
        printf("No communication with module!\n");  
    }

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

The module uses AT commands for communication with rest of world, for that is good to use - ATCmdParser - API references and tutorials | Mbed OS 6 Documentation

BR, Jan

Thanks for your response ,Now i’m trying to create lbriairies for A9G (GPRS/GPS) module to make it compatible with Mbed .
I don’t have any ideas how to create libriairies
please help

I am trying to help you whole time.

Did you test the code what I posted above? It is under the Piece of code, it is hiden content.
I had also a question in my previous post

Truly when you not understand basics, it not make sense to making any library. Just start with code what I posted above and continue to make it like the arduino code what you posted above. Once you have hard coded something funcional in the main then you can start to make a library from that. This is logical flow in development from my point of view.

BR, Jan

Yes i tested the code but it had a error of buffered serial not defined
Error: This declaration has no storage class or type specifier in “main.cpp”, Line: 16, Col: 2

I forgot the maaduino zero board and now i connect the STM32F103RB with the A9G module

Ok, that probably happened because you use old Mbed OS version, probably older than 6 where the BufferedSerial not exist. You can update MbedOS to latest or change the BufferedSerial object to Serial.

BTW, what a tool do you use? MbedStudio, KeilStudio or old OnlineCompiler?

BR, Jan

I use the online compiler

If you have mbed-os library included in your project then do right click and chose update. Then try it again.
mbed-os_update

BR,Jan

ok understood but the code that you sent to me is based on Maaduino zero board

The code I posted is based on your arduino code and it is able to run on every Mbed enabled board (with correct pins).
Maduino zero A9G is board where is SAM MCU connected via uart to A9G module aka Ai-Thinker A9 GSM+GPRS Modul. So no matter what is connected to the module, the communication will be the same.
So where you see a problem?

BR, Jan

Ok which board i will select ?
I have to use nucleo F103RB

The probleme is how can i use the module A9G in mbed with STM32 because the module not compatible with mbed

  • Simply connect crosswise UART TX (D10) and RX(D2) pins of your NUCLEO-F103RB | Mbed crossed UART RX and TX pins of A9G module (Nucleo TX to A9G RX an so on). Connect the Power Key pin if it necessary it is only optional.
  • Change pins in program to correct ones
  • Compile the code for your Nucleo board and upload it
  • Run some PC serial terminal like Putty for example, and see the output

BR, Jan

Error: Symbol __ARM_use_no_argv multiply defined (by …/…/build/main.NUCLEO_F103RB.o and …/…/build/A9GPRS/main.NUCLEO_F103RB.o).

I do not know from where this come from.
Please try to import this project - Mbed_A9G_test - Test | Mbed

BR, Jan

Ok, I found one piece of this/similar hardware in our cabinet…

Here you can see the output of my code
termite
So the communication was established. Now it is ready for additional commands.

I simplified the code because I found the PWR_KEY pin is only optional.

Let me know your progress.

BR, Jan