Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 3 months ago.
Unable to store AT command response from sim808 module!
Hello all, I am working with the STM32L031k6 microcontroller with the SIM808 GSM/GPS module. This is my hardware connection:
- SIM808_TX >> STM32 PA_15
- SIM808_RX >> STM32 PA_2
In the main.cpp, I am declaring:
Serial GPRS(PA_2, PA_15); //MCU tx,rx
Every thing is working great. I am able to make an MQTT connection and send dummy data as well.
What I want to do is to read the GPS Data, extract the latitude and Longitude and publish them.
The process to get GPS data is:
- 1) AT+CGPSPWR=1
- 2) AT+CGNSSEQ=RMC
- 3) AT+CGNSINF
the command <<code>>AT+CGNSINF <</code>>will reply with the string I want to use.
I am trying everything possible, but I am unable to save the reply of "AT+CGNSINF" command!
How can I read the response and save it in a variable? If I can get it in a variable, I can later use it to extract the latitude and longitude and publish it using MQTT.
Any help in the matter is highly appreciated!
My code is as follows:
#include "mbed.h"
#include <string.h>
const int buffer_size = 255;
char buffer[buffer_size+1];
char gpsstr[buffer_size+1];
char response[200];
Serial GPRS(PA_2, PA_15); //MCU tx,rx
Serial pc(SERIAL_TX, SERIAL_RX,9600);
void cle()
{
for(int i=0;i<buffer_size;i++)
{
buffer[i]=0;
}
}
void rec()
{
//cle();
int i=0;
while(GPRS.readable()&& i<buffer_size)
{
buffer[i]=GPRS.getc();
i++;
}
}
int main(){
pc.printf("starting code...");
wait(2);
GPRS.attach(&rec, Serial::RxIrq);
wait(1);
GPRS.printf("AT\r\n");
wait(1);
GPRS.printf("AT+CGNSPWR=1\r\n");
wait(1);
GPRS.printf("AT+CGNSSEQ=RMC\r\n");
wait(1);
cle();
GPRS.printf("AT+CGNSINF\r\n");
wait(1);
//getResponse();
GPRS.printf("data:%s",buffer);
}
1 Answer
8 years, 2 months ago.
Take a look at the ATParser library which uses a BufferedSerial object underneath. That should make it a lot easier to deal with the chip.
Example on how you use it is in esp8266-driver.