Hello, I have a question in a project I'm doing with Arduino UNO. The project is to detect accidents by MPU6050 and with the NEO6M read the location and upload it to ThingSpeak with sim800l.
The problem is in this part of the code:
void GPS(){
// Obtener la información del GPS
serialGPS.listen();
latitude=0;
longitude=0;
while(serialGPS.available()){
int c = serialGPS.read();
//Serial.println("GPS1");
Serial.print(".");
if(gps.encode(c)){
Serial.println("GPS2");
gps.f_get_position(&latitude, &longitude);
// Construir la URL para enviar los datos a ThingSpeak
thingspeakURL = "GET https://api.thingspeak.com/update?api_key=" + String(THINGSPEAK_API_KEY);
thingspeakURL += "&field1=" + String(latitude, 6);
thingspeakURL += "&field2=" + String(longitude, 6);
i=0;
a=true;
////Serial.println(thingspeakURL);
}
}
}
That to get the location I can't "waste time" in the while(serialGPS.available()), do you have any idea how I can avoid going into a while.
I mean that it can be all that time without getting a location when I change the while to an if.
During all this time it is getting data from the MPU6050 but nbt the location.
Hello, I have made tests and in static it behaves well, it takes about 5/10 seconds to give the reading of the coordinates. But in motion it takes much longer even 3 minutes. Any idea?
Attached is the new code:
#include <SoftwareSerial.h>//incluimos SoftwareSerial
#include <TinyGPS.h>//incluimos TinyGPS
#include <Wire.h>
#include <MPU6050.h>
// Configuración de la comunicación serial
#define GPS_RX_PIN 3
#define GPS_TX_PIN 4
SoftwareSerial serialGPS(GPS_TX_PIN, GPS_RX_PIN);
#define SIM_TX_PIN 10
#define SIM_RX_PIN 11
SoftwareSerial serialSIM800L(SIM_TX_PIN, SIM_RX_PIN); // Configuración de la interrupción
// Objeto TinyGPS++
TinyGPS gps;
MPU6050 mpu;
// Configuración de la red GSM y ThingSpeak
#define APN "TM" // Reemplaza con el nombre de tu APN
#define THINGSPEAK_API_KEY "API" // Reemplaza con tu API Write Key
String thingspeakURL = "GET https://api.thingspeak.com/update?api_key=" + String(THINGSPEAK_API_KEY);
//Declaramos la variables para la obtención de datos
unsigned long tiempo1 = 0;
unsigned long tiempo2 = 0;
int i=10;
bool a=false;
int16_t ax, ay, az;
float acc_x, acc_y, acc_z;
float latitude, longitude;
void setup() {
// Inicialización de la comunicación serial
Serial.begin(115200);
serialGPS.begin(9600);
serialSIM800L.begin(4800);
tiempo1 = millis();
delay(5000);
//Comunicación acelerometro
Wire.begin();
mpu.initialize();
mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_16);
}
void loop() {
if(i==10 and a==false){//MIRA EL GPS CADA 25 SEGUNDOS (TIEMPO QUE TARDA EN SUBIR DATOS)
GPS();
Wire.begin();
}
ACELEROMETRO();//MIENTRAS TANTO SIEMPRE SE LEE EL ACELEROMETRO
if(a){
SUBIR_UBICACION();
}
}
void ACELEROMETRO(){
//LECTURA DE ACELEROMETRO
mpu.getAcceleration(&ax, &ay, &az);
acc_x = ((float)ax / 2048);
acc_y = ((float)ay / 2048);
acc_z = ((float)az / 2048);
//Serial.println(acc_z);
//CONDICION EN CASO DE ACCIDENTE
//GPS();
//SMS_ACCIDENTE("+telf");
}
void GPS(){
// Obtener la información del GPS
serialGPS.listen();
latitude=0;
longitude=0;
Serial.print(".");
if(serialGPS.available()){
int c = serialGPS.read();
Serial.print("A");
if(gps.encode(c)){
Serial.println("GPS2");
gps.f_get_position(&latitude, &longitude);
// Construir la URL para enviar los datos a ThingSpeak
thingspeakURL = "GET https://api.thingspeak.com/update?api_key=" + String(THINGSPEAK_API_KEY);
thingspeakURL += "&field1=" + String(latitude, 6);
thingspeakURL += "&field2=" + String(longitude, 6);
i=0;
a=true;
}
}
}
void SUBIR_UBICACION() {
static unsigned long tiempo_anterior = 0; //Variable estática para mantener el tiempo anterior
if (i == 0) {
serialSIM800L.listen();
serialSIM800L.println("AT+CFUN=1");
delay(2000);
serialSIM800L.println("AT+CIPSTATUS");//Consultar el estado actual de la conexión
i++;
Serial.println(i);
mostrarDatosSeriales();
tiempo_anterior = millis();
}
else if (i == 1 && millis() - tiempo_anterior > 2000) {
serialSIM800L.println("AT+CIPMUX=0"); // Configurar la conexión TCP/IP para una sola conexión
i++;
Serial.println(i);
mostrarDatosSeriales();
tiempo_anterior = millis();
}
else if (i == 2 && millis() - tiempo_anterior > 2000) {
serialSIM800L.println("AT+CSTT=\"" + String(APN) + "\""); // Configurar el APN
i++;
Serial.println(i);
mostrarDatosSeriales();
tiempo_anterior = millis();
}
else if (i == 3 && millis() - tiempo_anterior > 2000) {
serialSIM800L.println("AT+CIICR"); // Establecer la conexión GPRS
i++;
Serial.println(i);
mostrarDatosSeriales();
tiempo_anterior = millis();
}
else if (i == 4 && millis() - tiempo_anterior > 2000) {
serialSIM800L.println("AT+CIFSR"); // Obtener la dirección IP
i++;
Serial.println(i);
mostrarDatosSeriales();
tiempo_anterior = millis();
}
else if (i == 5 && millis() - tiempo_anterior > 4000) {
String ip = serialSIM800L.readStringUntil('\n');
serialSIM800L.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\""); // Iniciar la conexión TCP/IP con ThingSpeak
i++;
Serial.println(i);
mostrarDatosSeriales();
tiempo_anterior = millis();
}
else if (i == 6 && millis() - tiempo_anterior > 5000) {
serialSIM800L.println("AT+CIPSEND"); // Iniciar el envío de datos
i++;
Serial.println(i);
mostrarDatosSeriales();
tiempo_anterior = millis();
}
else if (i == 7 && millis() - tiempo_anterior > 5000) {
serialSIM800L.println(thingspeakURL); // Enviar los datos a ThingSpeak
i++;
Serial.println(i);
Serial.println(thingspeakURL);
mostrarDatosSeriales();
tiempo_anterior = millis();
}
else if (i == 8 && millis() - tiempo_anterior > 6000) {
serialSIM800L.write((byte)26); // Finalizar el envío de datos
i++;
Serial.println(i);
mostrarDatosSeriales();
tiempo_anterior = millis();
}
else if (i == 9 && millis() - tiempo_anterior > 7000) {
serialSIM800L.println();
serialSIM800L.println("AT+CIPSHUT");//Cierra la conexión(Desactiva el contexto GPRS PDP)
i++;
Serial.println(i);
mostrarDatosSeriales();
tiempo_anterior = millis();
//Serial.println("END");
a=false;
}
}
void mostrarDatosSeriales()//Muestra los datos que va entregando el sim900
{
while(serialSIM800L.available()!=0)
Serial.write(serialSIM800L.read());
}
void SMS_ACCIDENTE(String PHONE_NUMBER) {
// Configura el módulo SIM800L para enviar mensajes SMS
serialSIM800L.println("AT+CMGF=1"); // Configura el modo SMS a texto
serialSIM800L.println("AT+CNMI=2,2,0,0,0"); // Configura la notificación SMS
// Envía un mensaje SMS al número de teléfono especificado
serialSIM800L.print("AT+CMGS=\"");
serialSIM800L.print(PHONE_NUMBER);
serialSIM800L.println("\"");
delay(1000);
serialSIM800L.print("Accidente de moto en: Latitud: " + String(latitude, 6) + " Longitud: " + String(longitude, 6));
delay(7000);
serialSIM800L.write(26);
delay(7000);
while(1){} //BUCLE INFINITO
}