Search code examples
arduinoconnectiongsm

How to run Ai Thinker A9G gps module using arduino uno & Arduino IDE?


I couldn't find any blog or solution for connecting and run A9G Ai thinker GSM/GPRS GPS module with Arduino Uno using Arduino IDE. Found only one video but that show the connection of A9G using USB to TTL converter. But i need the correct connection details for Arduino uno and A9G. How to connect them? (let me inform that i connect A9G rx with arduino tx, tx with arduino rx and GRnd and Vcc (5v+) and use the SMAD port but didn't work) which board and port have to use for IDE to use A9G? How to put AT commands in IDE using arduino code. Thank you in advance.


Solution

  • after a research I successfully implemented A9G with Arduino Uno here is the code:

    #include <SoftwareSerial.h> // Library for using serial communication
    SoftwareSerial SIM900(7, 8); // Pins 7, 8 are used as used as software serial pins
    
    String incomingData;   // for storing incoming serial data
    //String message = "";   // A String for storing the message
    //int relay_pin = 2;    // Initialized a pin for relay module
    char msg;
    char call;
    void setup()
    {
      Serial.begin(115200); // baudrate for serial monitor
      SIM900.begin(115200); // baudrate for GSM shield
      
    
      Serial.println("GSM SIM900A BEGIN");
      Serial.println("Enter character for control option:");
      Serial.println("h : to disconnect a call");
      Serial.println("i : to receive a call");
      Serial.println("s : to send message");
      Serial.println("c : to make a call");  
      Serial.println("e : to redial");
      Serial.println("l : to read location");
      Serial.println();
      delay(100);
    
      
      /*SIM900.println("AT+GPS=1");
      delay(100);
      SIM900.println("AT+GPSRD=5");
      delay(5000);*/
      
      // set SMS mode to text mode
      SIM900.print("AT+CMGF=1\r");  
      delay(100);
      
      // set gsm module to tp show the output on serial out
      SIM900.print("AT+CNMI=2,2,0,0,0\r"); 
      delay(100);
    }
    
    void loop()
    {
      
      receive_message();
    
    
    if (Serial.available()>0)
       switch(Serial.read())
      {
        case 's':
          SendMessage();
          break;
        case 'c':
          MakeCall();
          break;
        case 'h':
          HangupCall();
          break;
        case 'e':
          RedialCall();
          break;
        case 'i':
          ReceiveCall();
          break;
        case 'l':
          ReadLocation();
          break;
      }
           
    }
    
    void receive_message()
    {
      if (SIM900.available() > 0)
      {
        incomingData = SIM900.readString(); // Get the data from the serial port.
        Serial.print(incomingData); 
        delay(10); 
      }
    }
    
    void SendMessage()
    {
      SIM900.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
      delay(1000);  // Delay of 1000 milli seconds or 1 second
      SIM900.println("AT+CMGS=\"x\"\r"); // Replace x with mobile number
      delay(1000);
      SIM900.println("sim900a sms");// The SMS text you want to send
      delay(100);
       SIM900.println((char)26);// ASCII code of CTRL+Z
      delay(1000);
    }
    
    void ReceiveMessage()
    {
      SIM900.println("AT+CNMI=2,2,0,0,0"); // AT Command to recieve a live SMS
      delay(1000);
      if (SIM900.available()>0)
      {
        msg=SIM900.read();
        Serial.print(msg);
      }
    }
    
    void MakeCall()
    {
      SIM900.println("ATD+201020516469;"); // ATDxxxxxxxxxx; -- watch out here for semicolon at the end, replace your number here!!
      Serial.println("Calling  "); // print response over serial port
      delay(1000);
    }
    
    
    void HangupCall()
    {
      SIM900.println("ATH");
      Serial.println("Hangup Call");
      delay(1000);
    }
    
    void ReceiveCall()
    {
      SIM900.println("ATA");
      delay(1000);
      {
        call=SIM900.read();
        Serial.print(call);
      }
    }
    
    void RedialCall()
    {
      SIM900.println("ATDL");
      Serial.println("Redialing");
      delay(1000);
    }
    void ReadLocation(){
    
      SIM900.println("AT+GPS=1");
      delay(1000);
      SIM900.println("AT+GPSRD=5");
      delay(1000);
      
      }
    

    set the serial monitor baud rate to 115200 and replace your number in MakeCall function

    Edit: I suffered from noise in serial monitor when using baud rate 11500 by replacing it to 9600 the noise have gone.

    Edit: Important note the gps will give proper reading if the antenna is in open area and the module is connecting with external power source otherwise it will give you old reading.