Hello everyone, my code runs perfectly when the USB connects my esp32, but nothing is saved when I connect Vin with the battery.
#include <Arduino.h>
#include <math.h>
#include <SPIFFS.h>
//Definição dos pinos UARTs GPS NEO6M
#define UART_RX_PIN 4 // GPIO2
#define UART_TX_PIN 2 // GPIO4
//Definição dos pinos de saída do ADXL335
const int x_out = 13;
const int y_out = 12;
const int z_out = 14;
//Flag para indicar se o dispositivo está no plano XY
bool dispositivoNoPlanoXY = false;
const double pitch_tolerance = abs(5.0); // Tolerância em graus para considerar o dispositivo paralelo ao plano XY
//Botão para aquisição de dados
#define BUTTON_PIN 21
String sentence = ""; //Sentença recebida pelo módulo GPS
//Conversão de Graus, Minutos Decimais para Graus Decimais
double ddm_to_dd(float ddm) {
double degrees = floor(ddm / 100.0);
double minutes = ddm - degrees * 100.0;
double dd = degrees + minutes / 60.0;
return dd;
}
//Função para salvar dados de tempo, latitude e longitude na memória Flash
void salvarCoordenadasNoArquivo(String timeStr, float latitude, float longitude) {
File file = SPIFFS.open("/coordenadas.txt", "a");
if (file) {
file.printf("%s,%.5f,%.5f\n", timeStr.c_str(), latitude, longitude);
file.close();
//Serial.println("Coordenadas salvas no arquivo.");
} else {
//Serial.println("Erro ao abrir o arquivo.");
}
}
//Função para ler arquivo e imprimir a lista no terminal
void lerArquivo() {
File file = SPIFFS.open("/coordenadas.txt", "r");
if (file) {
Serial.println("Conteúdo do arquivo:");
while (file.available()) {
Serial.write(file.read());
}
file.close();
} else {
Serial.println("Erro ao abrir o arquivo.");
}
}
void verificarInclinacaoPlanoXY(double pitch, double roll) {
// Verificar se o dispositivo está no plano XY com base nos ângulos de pitch e roll
if (fabs(pitch) > 350 || fabs(pitch) < 205 || fabs(roll) > 220 || fabs(roll) < 100) {
// O dispositivo não está paralelo ao plano XY
dispositivoNoPlanoXY = false;
} else {
// O dispositivo está no plano XY
dispositivoNoPlanoXY = true;
}
}
void imprime_coordenadas(char NMEA_format){
sentence += NMEA_format;
if(NMEA_format == '\n'){
int index = sentence.indexOf("$GPGGA");
if (index != -1) {
int comma0 = sentence.indexOf(",", index);
int comma1 = sentence.indexOf(",", index + 7);
int comma2 = sentence.indexOf(",", comma1 + 1);
int comma3 = sentence.indexOf(",", comma2 + 1);
int comma4 = sentence.indexOf(",", comma3 + 1);
int comma5 = sentence.indexOf(",", comma4 + 1);
if (comma0 != -1 && comma1 != -1 && comma2 != -1 && comma3 != -1 && comma3 != -1 && comma4 != -1 && comma5 != -1) {
//Variáveis para salvar apenas latitude em ddmm.mmmm
String timeStr = sentence.substring(comma0 +1, comma1);
String latitudeStr = sentence.substring(comma1 + 1, comma2);
String orlatitudeStr = sentence.substring(comma2 + 1, comma3);
float latitude = latitudeStr.toFloat();
latitude= ddm_to_dd(latitude);
//Serial.print("Sentença: ");
//Serial.println(sentence);
//Serial.print("Tempo: ");
//Serial.println(timeStr);
//Serial.print("Latitude: ");
//Serial.print(latitude, 5);
//Serial.println(orlatitudeStr);
//Variáveis para salvar apenas longitude em dddmm.mmmm
String longitudeStr = sentence.substring(comma3 + 1, comma4);
String orlongitudeStr = sentence.substring(comma4 + 1, comma5);
float longitude = longitudeStr.toFloat();
longitude = ddm_to_dd(longitude);
//Serial.print("Longitude: ");
//Serial.print(longitude, 5);
//Serial.println(orlongitudeStr);
// Chame a função para salvar as coordenadas no arquivo
salvarCoordenadasNoArquivo(timeStr, latitude, longitude);
}
}
sentence = "";
}
}
void setup() {
Serial.begin(9600); //Para comunicação USB
Serial2.begin(9600, SERIAL_8N1,
UART_RX_PIN,
UART_TX_PIN); //Para comunicação com o módulo
pinMode(BUTTON_PIN, INPUT);
if (!SPIFFS.begin(true)) {
Serial.println("Falha ao montar o sistema de arquivos SPIFFS!");
return;
}
}
void loop() {
if (digitalRead(BUTTON_PIN) == HIGH) {
int x_adc_value, y_adc_value, z_adc_value;
double x_g_value, y_g_value, z_g_value;
double roll, pitch;
x_adc_value = analogRead(x_out); // Digital value of voltage on x_out pin
y_adc_value = analogRead(y_out); // Digital value of voltage on y_out pin
z_adc_value = analogRead(z_out); // Digital value of voltage on z_out pin
x_g_value = ( ( ( (double)(x_adc_value * 3.3)/4096) - 1.65 ) / 0.330 ); // Acceleration in x-direction in g units
y_g_value = ( ( ( (double)(y_adc_value * 3.3)/4096) - 1.65 ) / 0.330 ); // Acceleration in y-direction in g units
z_g_value = ( ( ( (double)(z_adc_value * 3.3)/4096) - 1.80 ) / 0.330 ); // Acceleration in z-direction in g units
roll = ( ( (atan2(y_g_value,z_g_value) * 180) / 3.14 ) + 180 ); // Formula for roll
pitch = ( ( (atan2(z_g_value,x_g_value) * 180) / 3.14 ) + 180 ); // Formula for pitch
// Verificar a inclinação em relação ao plano XY
verificarInclinacaoPlanoXY(pitch, roll);
if (dispositivoNoPlanoXY) {
//Copy byte incoming via PC serial
while (Serial.available() > 0) {
Serial2.write(Serial.read());
}
//Copy bytes incoming via UART serial
while (Serial2.available() > 0) {
char c = Serial2.read();
imprime_coordenadas(c);
}
}
}
delay(1000);
}