Hello, first I will introduce myself . My name is malaltet I am from here and there and I do drift to amateur mode.
Now important, I am a lover of DIY and I consider making a dataLogger to monitor certain parameters of the car. The problem is that GPS does not seem to work, I suppose it's because it does not validate if (SERIAL_GPS.available ()> 0) or if (gps.encode (SERIAL_GPS.read))
That's the bad part, now the worst part, I thought that the GPS being Chinese would not work well or it should always be at open place, right ?. So I decided to try out an example of the GPS library and voila! In less than 10 seconds it picks up the GPS signal. I have been trying to solve the problem for about a month and I do not know what it can be ... I am desperate to see that the same code works in one and not in another ... hahaha
(accept improvements and correction of errors)
// Libraries
#include <TinyGPS++.h>
#include <SD.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <LcdBarGraphX.h>
#include <SoftwareSerial.h>
//Cambiamos de nombre Serial a SERIAL_GPS
static const int RXPin = 4, TXPin = 9;
SoftwareSerial SERIAL_GPS(RXPin, TXPin);
//Digital Pins
#define LED_STATUS_PIN 7 // LED pin to show current status, helpful for debugging.
#define LED_GPS 6 //LED para saber si el GPS recibe datos
#define BRAKE_PIN 5 //Aun no he encontrado ningun sensor de freno...
#define STOP_SWITCH_PIN 8 // Pushbutton switch to stop program
//Pins SD
#define SD_PIN 10 // SD SS Pin
#define MOSI_PIN 11 // SD MOSI Pin
#define MISO_PIN 12 // SD MISO Pin
#define SCK_PIN 13 // SD SCK Pin
// Analog Pins
#define TPS_PIN A0 // Throttle Position Sensor
#define AIR_TEMP_PIN A1 //Sensor de temperatura de la entreada de aire
#define ENG_TEMP_PIN A2 //Sensor de temperatura del motor
// Initialize TinyGPS Variables
TinyGPSPlus gps;
// Initialize SD File Variable
File dataFile;
// Initialize Global Variables
int tpsValue, airTemp, engTemp; //Si brake value es un numero entre 1 y 0 se cambiara la variable para que ocupe menos espacio
byte brakeValue;
//Inicio del LCD
#define I2C_ADDR 0x27
LiquidCrystal_I2C lcd( I2C_ADDR, 2, 1, 0, 4, 5, 6, 7);
LcdBarGraphX lbg(&lcd, 8, 1, 0); //lbg(lcd, longitud del grafico, columna inicio, fila);
LcdBarGraphX lbg1(&lcd, 8, 1, 1);
LcdBarGraphX lbg2(&lcd, 6, 9, 0);
LcdBarGraphX lbg3(&lcd, 6, 9, 1);
void setup() {
Serial.begin(115200);
SERIAL_GPS.begin(9600);
setIoPins(); // Initialize IO Pins
setLCD(); //Inicia el LCD
setSerial(); // Initialize Serial ports
setSdCard(); // Initialize SD Card
createFile(); // Create File
lcd.clear();
}
void loop() {
checkStopSwitchPin();
if (SERIAL_GPS.available()) { // If TinyGPS detects parsable data from serial port
int c = SERIAL_GPS.read();
if (gps.encode(c)) {
getSensorData(); // Then get sensor data
writeToSdCard(); // Write data to SD Card
}
do
{
displayInfo();
} while (gps.encode(c));
}
}
void setIoPins() {
// Intitialize input pins
pinMode(BRAKE_PIN, INPUT);
pinMode(TPS_PIN, INPUT);
pinMode(STOP_SWITCH_PIN, INPUT_PULLUP); // SD Switch set to normally HIGH, will go to LOW when pressed
// Initialize output pins
pinMode(LED_STATUS_PIN, OUTPUT);
pinMode(LED_GPS, OUTPUT);
pinMode(SD_PIN, OUTPUT);
// Initialize HIGH pins
digitalWrite(LED_STATUS_PIN, HIGH);
digitalWrite(LED_GPS, LOW);
}
void setSerial() {
SERIAL_GPS.begin(9600);
}
void setSdCard() {
if (!SD.begin(SD_PIN)) { // If SD SS Pin is false for whatever reason then
lcd.clear();
lcd.print(F("Fallo en SD"));
flashLed(1); // Flash loop LED once and halt
}
lcd.clear();
lcd.print(F("SD Iniciada"));
delay(1500);
}
// After SD Card has been initialized, file will be created once GPS has a lock.
// This is done by setting isFileCreated boolean to true and will finally cause
// program to leave while loop.
// TODO: Reduce control flow inception (ie. too many loops within loops) for sanity purposes.
void createFile() {
boolean isFileCreated = false;
while (!isFileCreated) {
if (SERIAL_GPS.available() > 0) {
// Create a new file using character array.
// Must use '0' because if int i = 1 then i/10 = 0.1
// but since its an integer, i/10 = 0 due to rounding cutoff
// and 0 + 0 = 'null' character
// but 0 + '0' = 0 + 48 = '0' character
// Reference ASCII chart for dec to char value
char filename[] = "LOGGER00.CSV"; //Crea un array de 12 elementos, uno por cada letra del nombre del archivo
for (byte i = 0; i < 100; i++) {
filename[6] = i / 10 + '0'; //Incrementa el valor para crear archivos nuevos
filename[7] = i % 10 + '0';
if (!SD.exists(filename)) {
// only create a new file if it doesn't exist
dataFile = SD.open(filename, FILE_WRITE);
//SERIAL_DEBUG.print(F("File "));
// SERIAL_DEBUG.print(filename);
//SERIAL_DEBUG.println(F(" has been created."));
isFileCreated = true;
lcd.clear();
lcd.print(F(" CREADO "));
lcd.setCursor(1,1);
lcd.print(filename);
delay(3000);
break; // leave the for loop! This is done so only one file is created
// which is the first number that does not exist yet
}
}
}
}
if (!dataFile) { //En el caso de que no se haya podido crear el archivo...
//SERIAL_DEBUG.println("could not create file.");
lcd.clear();
lcd.print(F(" Fallo al crear "));
lcd.setCursor(0, 1);
lcd.print(F(" el archivo "));
flashLed(2); // Flash loop led twice and halt
}
String header = (F("LAT;LON;l.AGE;DATE;TIME;ALTITUDE;KM/H;TPS;BRAKE"));
dataFile.println(header);
}
void checkStopSwitchPin() {
if (digitalRead(STOP_SWITCH_PIN) == LOW) {
lcd.clear();
lcd.print(F("Cerrando..."));
dataFile.close(); // Save and close file
delay(2500);
lcd.clear();
flashLed(3); // And flash loop LED 3 times and halt
}
}
// LED sequence will blink how ever many times flash variable is set to,
// pause for 2 seconds, then start blink sequence over again indefinitely.
// Number of times LED flashes:
// 1 = SD Card failed or not present
// 2 = Could not create file
// 3 = SD card closed able to remove
void flashLed(byte flash) {
while (1) { // while(1) causes program to be stuck in loop forever
for (byte i = flash; i > 0; i--) {
digitalWrite(LED_STATUS_PIN, LOW);
delay(200);
digitalWrite(LED_STATUS_PIN, HIGH);
delay(200);
}
digitalWrite(LED_STATUS_PIN, LOW);
delay(2000);
}
}
void getSensorData() {
tpsValue = map(analogRead(TPS_PIN), 82, 900, 0 , 100);
brakeValue = map(digitalRead(BRAKE_PIN), 0, 1, 0, 100);
airTemp = map(analogRead(AIR_TEMP_PIN), 798, 143, 10, 120 );
engTemp = map(analogRead(ENG_TEMP_PIN), 811, 0, 0, 110);
}
void writeToSdCard() {
digitalWrite(LED_GPS, HIGH);
dataFile.print(gps.location.lat(), 6);
dataFile.print(";");
dataFile.print(gps.location.lng(), 6);
dataFile.print(";");
dataFile.print(gps.altitude.meters());
dataFile.print(";");
dataFile.print(gps.speed.kmph());
dataFile.print(";");
dataFile.print(tpsValue);
dataFile.print(";");
dataFile.print(brakeValue);
dataFile.print(";");
dataFile.print(airTemp);
dataFile.print(";");
dataFile.print(engTemp);
dataFile.print(";");
dataFile.print(gps.time.value());
dataFile.println();
dataFile.flush(); // Save file, but doesn't close
digitalWrite(LED_GPS, LOW);
Serial.println(gps.location.lat(), 6);
Serial.println(gps.time.value());
}
void setLCD() {
//Inicio de la pantalla LCD
lcd.begin (16, 2); // Inicializar el display con 16 caraceres 2 lineas
lcd.setBacklightPin(3, POSITIVE);
lcd.setBacklight(HIGH);
lcd.home ();
}
void displayInfo() {
lcd.setCursor(0,0);
lcd.print(F("T"));
lcd.setCursor(0,1);
lcd.print(F("B"));
lcd.setCursor(8,0);
lcd.print(F("I"));
lcd.setCursor(8,1);
lcd.print(F("W"));
lbg.drawValue(tpsValue, 100);
lbg1.drawValue(brakeValue, 100);
lbg2.drawValue(airTemp, 120);
lbg3.drawValue(engTemp, 110);
}
Where is the problem?
Thanks