Skip to content

Commit

Permalink
Fix GPS & Refactor timekeeping (#173)
Browse files Browse the repository at this point in the history
* Refactor timekeeping to use system functions

* Fix GPS module communication
  • Loading branch information
JosephHewitt authored Sep 1, 2024
1 parent 0607a32 commit da208b6
Showing 1 changed file with 53 additions and 40 deletions.
93 changes: 53 additions & 40 deletions A/A.ino
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ unsigned int wigle_history_cursor = 0;
unsigned long lcd_last_updated;

#define YEAR_2020 1577836800 //epoch value for 1st Jan 2020; dates older than this are considered wrong (this code was written after 2020).
unsigned long epoch;
unsigned long epoch_updated_at;
const char* ntpServer = "pool.ntp.org";

TaskHandle_t primary_scan_loop_handle;
Expand Down Expand Up @@ -250,6 +248,21 @@ struct wigle_file get_wigle_file(int fid, unsigned long fsize){
return wigle_file_reference;
}

unsigned long get_epoch(boolean await_valid=false) {
//Return epoch from system clock.

time_t now;
struct tm timeinfo;
if (await_valid){
//This seems to just loop for ~5sec or until the date is valid. Possibly required for NTP?
if (!getLocalTime(&timeinfo)) {
return(0);
}
}
time(&now);
return now;
}

void wigle_load_history(){
wigle_history_cursor = 0;

Expand Down Expand Up @@ -1514,10 +1527,9 @@ void boot_config(){
display.display();
Serial.println("Connected, getting the time");
configTime(0, 0, ntpServer);
epoch = getTime();
epoch_updated_at = millis();

Serial.print("Time is now set to ");
Serial.println(epoch);
Serial.println(get_epoch(true));
Serial.println("Continuing..");
String ota_test = ota_get_url("/");
Serial.println(ota_test);
Expand Down Expand Up @@ -2017,15 +2029,14 @@ void boot_config(){
int endpos = buff.indexOf(" ",startpos);
String newtime_str = buff.substring(startpos,endpos);
unsigned long newtime = atol(newtime_str.c_str());
if (epoch < YEAR_2020){
if (get_epoch() < YEAR_2020){
//if the epoch value is set to something before 2020, we can be quite sure it is inaccurate.
if (newtime > YEAR_2020){
//A very basic validity test for the datetime value issued by the client.
Serial.println("Current clock is inaccurate, using time from client");
epoch = newtime;
set_sys_clock(newtime);
Serial.print("epoch is now ");
Serial.println(epoch);
epoch_updated_at = millis();
Serial.println(get_epoch());
}
}
}
Expand Down Expand Up @@ -2373,15 +2384,15 @@ void setup() {
b_side_hash.concat(b_side_hash_full.charAt(x));
}

Serial2.begin(gps_baud_rate);
Serial2.begin(gps_baud_rate,SERIAL_8N1,16,17);

Serial.print("This device: ");
Serial.println(device_type_string());

filewriter.print(", bc=");
filewriter.print(bootcount);
filewriter.print(", ep=");
filewriter.print(epoch);
filewriter.print(get_epoch());
filewriter.print(", bsh=");
filewriter.print(b_side_hash);
filewriter.flush();
Expand Down Expand Up @@ -2579,13 +2590,11 @@ void lcd_show_stats(){

void loop(){
//The main loop for the second core; handles GPS, "Side B" communication, and LCD refreshes.
update_epoch();
while (Serial2.available()){
char c = Serial2.read();
if (nmea.process(c)){
if (nmea.isValid()){
lastgps = millis();
update_epoch();
}
}
}
Expand Down Expand Up @@ -2621,6 +2630,7 @@ void loop(){
disp_gsm_count = gsm_count;
}
if (lcd_last_updated == 0 || millis() - lcd_last_updated > 1000){
gps_time_sync();
lcd_show_stats();
lcd_last_updated = millis();
}
Expand Down Expand Up @@ -2871,7 +2881,7 @@ String parse_bside_line(String buff){
testfilewriter.print(",blc=");
testfilewriter.print(ble_count);
testfilewriter.print(",ep=");
testfilewriter.println(epoch);
testfilewriter.println(get_epoch());
testfilewriter.close();
b_working = false;
side_b_reset_millis = millis();
Expand Down Expand Up @@ -3051,7 +3061,7 @@ String parse_bside_line(String buff){

String dt_string(){
//Return a datetime String using local timekeeping and GPS data.
time_t now = epoch;
time_t now = get_epoch();
struct tm ts;
char buf[80];

Expand Down Expand Up @@ -3242,38 +3252,41 @@ String get_latest_datetime(String filename, boolean date_only){
return "";
}

void update_epoch(){
//Update the global epoch variable using the GPS time source.
boolean set_sys_clock(unsigned long new_epoch){
//Wrapper function to set sys clock to epoch value using standard POSIX functions

struct timeval val;
int ret;

val.tv_sec = new_epoch;
val.tv_usec = 0;
ret = settimeofday(&val, NULL);

if (ret == 0){
return true;
} else {
return false;
}
}

void gps_time_sync(){
//Sync the time with GPS if we have a lock.
if (!nmea.isValid()){
return;
}

String gps_dt = dt_string_from_gps();
if (!nmea.isValid() || lastgps == 0 || gps_dt.length() < 5){
unsigned int tdiff_sec = (millis()-epoch_updated_at)/1000;
if (tdiff_sec < 1){
return;
}
epoch += tdiff_sec;
epoch_updated_at = millis();
if (gps_dt.length() < 5){
return;
}

struct tm tm;
unsigned long this_epoch = 0;

strptime(gps_dt.c_str(), "%Y-%m-%d %H:%M:%S", &tm );
epoch = mktime(&tm);
epoch_updated_at = millis();
}
this_epoch = (unsigned long)mktime(&tm);

unsigned long getTime() {
//Use NTP to get the current epoch value.

time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
return(0);
}
time(&now);
Serial.print("Got time from NTP: ");
Serial.println(now);
return now;
set_sys_clock(this_epoch);
}

struct coordinates get_cell_pos(String wigle_key){
Expand Down

0 comments on commit da208b6

Please sign in to comment.