It's been trying for a long time to run my SIM808 module, I have problems when reading the data, execute the power-on and parameterization functions correctly, the problem is when reading the data, since it does not return anything and the program stops and stops running, as shown in the image, and you also noticed that the program is rebooted, that is, it goes through the void setup (); Thank you very much
#include <SoftwareSerial.h>
SoftwareSerial SIM808(7, 8);
int led = 13;
int ledStatus = LOW;
String campo[13];
void setup() {
//
SIM808.begin(19200);
Serial.begin(9600);
pinMode(led, OUTPUT);
Serial.println("Fin de setup");
enviarAT("AT+GPSPWR=1","OK",1000);
delay(500);
}
void decodeRespuesta(String r) {
int ant = 0;
int pos = 0;
for (int i = 0; i < 13; i++)
{
pos = r.indexOf(",", ant + 1);
campo[i] = r.substring(ant, pos);
ant = pos + 1;
}
}
double DecodeNMEA(String l, String h) {
int a = int(l.toFloat()) / 100;
double b = (l.toFloat() - (a * 100)) / 60;
if (h.equals("W") || h.equals("S")) {
return (a + b) * -1;
} else {
return a + b;
}
// a=0;
// b=0;
}
int enviarAT(String ATcommand, char* resp_correcta, unsigned int tiempo)
{
int x = 0;
bool correcto = 0;
char respuesta[100];
unsigned long anterior;
memset(respuesta, '#include <SoftwareSerial.h>
SoftwareSerial SIM808(7, 8);
int led = 13;
int ledStatus = LOW;
String campo[13];
void setup() {
//
SIM808.begin(19200);
Serial.begin(9600);
pinMode(led, OUTPUT);
Serial.println("Fin de setup");
enviarAT("AT+GPSPWR=1","OK",1000);
delay(500);
}
void decodeRespuesta(String r) {
int ant = 0;
int pos = 0;
for (int i = 0; i < 13; i++)
{
pos = r.indexOf(",", ant + 1);
campo[i] = r.substring(ant, pos);
ant = pos + 1;
}
}
double DecodeNMEA(String l, String h) {
int a = int(l.toFloat()) / 100;
double b = (l.toFloat() - (a * 100)) / 60;
if (h.equals("W") || h.equals("S")) {
return (a + b) * -1;
} else {
return a + b;
}
// a=0;
// b=0;
}
int enviarAT(String ATcommand, char* resp_correcta, unsigned int tiempo)
{
int x = 0;
bool correcto = 0;
char respuesta[100];
unsigned long anterior;
memset(respuesta, '%pre%', 100); // Inicializa el string
delay(100);
while ( SIM808.available() > 0) SIM808.read(); // Limpia el buffer de entrada
Serial.println(ATcommand);
SIM808.println(ATcommand); // Envia el comando AT //Write
//SIM808.flush();
x = 0;
anterior = millis();
// Espera una respuesta
do {
// si hay datos el buffer de entrada del UART lee y comprueba la respuesta
if (SIM808.available() != 0)
{
respuesta[x] = SIM808.read();
x++;
// Comprueba si la respuesta es correcta
if (strstr(respuesta, resp_correcta) != NULL)
{
correcto = 1;
}
}
}
// Espera hasta tener una respuesta
while ((correcto == 0) && ((millis() - anterior) < tiempo));
//Serial.print("Respuesta: ");
Serial.println(respuesta);
return correcto;
}
String PushCoordenadas(){
enviarAT("AT+CGPSOUT=32","OK",500);
Serial.println("GPS Activado");
delay(1000);
//Serial.println(Serial.read());
char lido = 0;
String respuesta = "";
while ((lido = SIM808.read()) != 10) {
if (lido > 0) {
respuesta += lido;
}
Serial.println("Leyendo: ");
Serial.println(respuesta);
}
enviarAT("AT+CGPSOUT=1","OK",500);
Serial.println("GPS Desactivado");
delay(50);
if (!respuesta.equals(" ")) {
if (respuesta.indexOf("RMC") > 0)
{
Serial.println("RMC Detectado==========================================");
enviarAT("AT+CGPSOUT=1","OK",3000);
Serial.println("GPS Desactivado");
delay(100);
if (respuesta.charAt(18/*43*/) == 'A')
{
Serial.println("Dato util =A ");
decodeRespuesta(respuesta);
Serial.println(respuesta);
for(int i=0;i<13;i++)Serial.println(campo[i]);
Serial.print("Latitud: ");
Serial.println(DecodeNMEA(campo[3], campo[4]), 10);
Serial.print("Longitud: ");
Serial.println(DecodeNMEA(campo[5], campo[6]), 10);
ledStatus = HIGH;
enviarAT("AT+CGPSOUT=1","OK",500);
delay(600);
Serial.println("GPS Desactivado");
while (SIM808.read() <= 1);
//SIM808.flush();
}
}
}
}
void loop() {
PushCoordenadas();
/* enviarAT("AT+CGPSOUT=32","OK",1000);
delay(500);
if(Serial.available()){
Serial.print("Datos: ");
Serial.println(SIM808.read());
}
enviarAT("AT+CGPSOUT=1","OK",3000);
delay(1000);*/
}
', 100); // Inicializa el string
delay(100);
while ( SIM808.available() > 0) SIM808.read(); // Limpia el buffer de entrada
Serial.println(ATcommand);
SIM808.println(ATcommand); // Envia el comando AT //Write
//SIM808.flush();
x = 0;
anterior = millis();
// Espera una respuesta
do {
// si hay datos el buffer de entrada del UART lee y comprueba la respuesta
if (SIM808.available() != 0)
{
respuesta[x] = SIM808.read();
x++;
// Comprueba si la respuesta es correcta
if (strstr(respuesta, resp_correcta) != NULL)
{
correcto = 1;
}
}
}
// Espera hasta tener una respuesta
while ((correcto == 0) && ((millis() - anterior) < tiempo));
//Serial.print("Respuesta: ");
Serial.println(respuesta);
return correcto;
}
String PushCoordenadas(){
enviarAT("AT+CGPSOUT=32","OK",500);
Serial.println("GPS Activado");
delay(1000);
//Serial.println(Serial.read());
char lido = 0;
String respuesta = "";
while ((lido = SIM808.read()) != 10) {
if (lido > 0) {
respuesta += lido;
}
Serial.println("Leyendo: ");
Serial.println(respuesta);
}
enviarAT("AT+CGPSOUT=1","OK",500);
Serial.println("GPS Desactivado");
delay(50);
if (!respuesta.equals(" ")) {
if (respuesta.indexOf("RMC") > 0)
{
Serial.println("RMC Detectado==========================================");
enviarAT("AT+CGPSOUT=1","OK",3000);
Serial.println("GPS Desactivado");
delay(100);
if (respuesta.charAt(18/*43*/) == 'A')
{
Serial.println("Dato util =A ");
decodeRespuesta(respuesta);
Serial.println(respuesta);
for(int i=0;i<13;i++)Serial.println(campo[i]);
Serial.print("Latitud: ");
Serial.println(DecodeNMEA(campo[3], campo[4]), 10);
Serial.print("Longitud: ");
Serial.println(DecodeNMEA(campo[5], campo[6]), 10);
ledStatus = HIGH;
enviarAT("AT+CGPSOUT=1","OK",500);
delay(600);
Serial.println("GPS Desactivado");
while (SIM808.read() <= 1);
//SIM808.flush();
}
}
}
}
void loop() {
PushCoordenadas();
/* enviarAT("AT+CGPSOUT=32","OK",1000);
delay(500);
if(Serial.available()){
Serial.print("Datos: ");
Serial.println(SIM808.read());
}
enviarAT("AT+CGPSOUT=1","OK",3000);
delay(1000);*/
}