Adding GPS Features
This code is sourced from another project.
Currently the GPS is only used for Date and Time retrieval.
gps.ino
#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;
const int sensorPin = 2;
TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);
unsigned long lastDateSentTime = 0;
const unsigned long sendDateInterval = 7000;
unsigned long previousMillis = 0;
void setup() {
pinMode(sensorPin, INPUT);
Serial.begin(9600);
ss.begin(GPSBaud);
}
void loop() {
while (ss.available() > 0)
gps.encode(ss.read());
unsigned long currentTime = millis();
if (currentTime - lastDateSentTime >= sendDateInterval) {
sendTimeAndDate();
lastDateSentTime = currentTime;
return;
}
}
void sendTimeAndDate() {
if (!gps.time.isValid() || !gps.date.isValid())
return;
StaticJsonDocument<100> jsonDoc;
JsonObject timeObject = jsonDoc.createNestedObject("time");
timeObject["h"] = gps.time.hour();
timeObject["m"] = gps.time.minute();
timeObject["s"] = gps.time.second();
JsonObject dateObject = jsonDoc.createNestedObject("date");
dateObject["d"] = gps.date.day();
dateObject["m"] = gps.date.month();
dateObject["y"] = gps.date.year();
String jsonString;
serializeJson(jsonDoc, jsonString);
Serial.println(jsonString);
}
Last updated