0% found this document useful (0 votes)
6 views2 pages

Code 1

The document contains an Arduino sketch that reads GPS data and logs it to an SD card. It parses GPGGA NMEA sentences to extract time, latitude, and longitude, and calculates signal strength in dBm from an analog input. The data is logged every 30 seconds in a CSV format, including time, latitude, longitude, and signal strength.

Uploaded by

tehmatix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Code 1

The document contains an Arduino sketch that reads GPS data and logs it to an SD card. It parses GPGGA NMEA sentences to extract time, latitude, and longitude, and calculates signal strength in dBm from an analog input. The data is logged every 30 seconds in a CSV format, including time, latitude, longitude, and signal strength.

Uploaded by

tehmatix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <SPI.

h>
#include <SD.h>
#include <SoftwareSerial.h>

const int chipSelect = 10;


SoftwareSerial gpsSerial(4, 3); // RX, TX pentru modul GPS (RX pe D4)

const int analogPin = A0;


const unsigned long interval = 30000;
unsigned long previousMillis = 0;

File logFile;

float voltageToDbm(float voltage) {


return 33.636 * voltage - 139.18;
}

bool parseGPGGA(String nmea, String &timeStr, String &lat, String &lon) {


if (!nmea.startsWith("$GPGGA")) return false;
int idx = 0;
int lastIdx = 0;
int field = 0;
String fields[15];

while ((idx = nmea.indexOf(',', lastIdx)) != -1) {


fields[field++] = nmea.substring(lastIdx, idx);
lastIdx = idx + 1;
}

if (field > 5) {
timeStr = fields[1];
lat = fields[2];
lon = fields[4];
return true;
}
return false;
}

void setup() {
Serial.begin(9600);
gpsSerial.begin(4800);
pinMode(chipSelect, OUTPUT);

if (!SD.begin(chipSelect)) {
Serial.println("Card failed or not present");
while (1);
}

Serial.println("Card initialized.");
logFile = SD.open("log.csv", FILE_WRITE);
if (logFile) {
logFile.println("Time,Latitude,Longitude,Signal_dBm");
logFile.close();
}
}

void loop() {
if (millis() - previousMillis >= interval) {
previousMillis = millis();
String nmea = "";
while (gpsSerial.available()) {
char c = gpsSerial.read();
if (c == '\n') break;
nmea += c;
}

String timeStr, lat, lon;


if (parseGPGGA(nmea, timeStr, lat, lon)) {
int adcVal = analogRead(analogPin);
float voltage = adcVal * (3.0 / 1023.0);
float dbm = voltageToDbm(voltage);

logFile = SD.open("log.csv", FILE_WRITE);


if (logFile) {
logFile.print(timeStr);
logFile.print(",");
logFile.print(lat);
logFile.print(",");
logFile.print(lon);
logFile.print(",");
logFile.println(dbm, 1);
logFile.close();
}

Serial.print("Time: "); Serial.print(timeStr);


Serial.print(" Lat: "); Serial.print(lat);
Serial.print(" Lon: "); Serial.print(lon);
Serial.print(" dBm: "); Serial.println(dbm, 1);
}
}
}

You might also like