diff --git a/SD_GPS_MQTT/SD_GPS_MQTT.ino b/SD_GPS_MQTT/SD_GPS_MQTT.ino index ef9a6330762b41f3573803df830daa0a6010825a..e24d5e4b53e0304cb546f0a70ed3a29cb3b4e200 100644 --- a/SD_GPS_MQTT/SD_GPS_MQTT.ino +++ b/SD_GPS_MQTT/SD_GPS_MQTT.ino @@ -18,17 +18,19 @@ uint32_t timer = millis(); Adafruit_GPS GPS(&GPSSerial); // SD card information -File myFile; +File tmpFile; +File logFile; void setup() { /* Initialize PC serial */ Serial.begin(115200); //Initialize serial and wait for port to open: - while (!Serial); // Wait for Serial to be ready + //while (!Serial); // Wait for Serial to be ready + delay(2000); Serial.println("\nHello there! My name is Annie Tracey, the animal tracker"); /* Initialize GPS module */ GPSSerial.begin(9600); - while (!GPSSerial); + //while (!GPSSerial); GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY); // Set to RMC only GPS.sendCommand(PMTK_SET_NMEA_UPDATE_200_MILLIHERTZ); // Set the update rate GPS.sendCommand(PGCMD_ANTENNA); // Request updates on antenna status @@ -37,26 +39,12 @@ void setup() { /*Initialize SD card */ if (!SD.begin(10)) { Serial.println("SD card initialization failed!"); - while (1); - } - Serial.println("SD card initialization done!"); - SD.remove("gpslog.txt"); - myFile = SD.open("gpslog.txt", FILE_READ); - myFile.close(); - -/* - // if the file opened okay, write to it: - if (myFile) { - Serial.print("Writing to gpslog.txt..."); - myFile.println("test"); - // close the file: - myFile.close(); - Serial.println("done."); - } else { - // if the file didn't open, print an error: - Serial.println("error opening gpslog.txt"); - } -*/ + } else{ + //SD.remove("gpstmp.txt"); + tmpFile = SD.open("gpstmp.txt", FILE_READ); + tmpFile.close(); + Serial.println("SD card initialization done!"); + } /* WiFi + MQTT initialization */ setup_wifi(); // Initialize WiFi @@ -72,33 +60,32 @@ void loop() { return; // we can fail to parse a sentence in which case we should just wait for another } - // approximately every 9 seconds or so, print out the current stats - if (millis() - timer > 9000) { + // approximately every XX seconds or so, print out the current stats + if (millis() - timer > 15000) { timer = millis(); // reset the timer /* Process GPS data */ - //parseGPSdata(); + parseGPSdata(); /* Store Data in the SD card */ storeGPSdata(); /* Reconnect to WiFi */ - //mainWifiLoop(); + mainWifiLoop(); } } void storeGPSdata(){ - char sd_word[60]; - String tmp_word; - - String mqtt_timstmp, lt, ln, al; - char time_stmp[30], ant_fix[5], sat[5], qlty[5], pos_lat[10], pos_lon[10], pos_alt[10]; - char yr[5], mh[3], dy[3], hr[3], mn[3], sc[3]; + char sd_word[60], time_stmp[30], pos_lat[10], pos_lon[10], pos_alt[10]; + char yr[5], mh[3], dy[3], hr[3], mn[3], sc[3]; + String tmp_word, ln, lt, al; if (GPS.fix) { - myFile = SD.open("gpslog.txt", FILE_WRITE); - if (myFile) { - Serial.print("Writing to SD card..."); + gps_fix_flg = 1; // If there has been at least one fix, set flag + + Serial.print("\nWriting to log file..."); + logFile = SD.open("gpslog.txt", FILE_WRITE); + if (logFile) { // Compose timestamp sprintf(yr, "20%d", GPS.year); @@ -128,13 +115,22 @@ void storeGPSdata(){ // Compose final word sprintf(sd_word,"%s,%d,%d,%d,%s,%s,%s", time_stmp, GPS.fix, GPS.fixquality, GPS.satellites, pos_lon, pos_lat, pos_alt); - myFile.println(sd_word); - - // close the file: - myFile.close(); + logFile.println(sd_word); + logFile.close(); // close the file + delay(100); + Serial.println("done."); + } else { + Serial.println("error opening log"); // if the file didn't open, print an error: + } + + tmpFile = SD.open("gpstmp.txt", FILE_WRITE); + Serial.print("Writing to temp log..."); + if (tmpFile) { + tmpFile.println(sd_word); + tmpFile.close(); Serial.println("done."); } else { - Serial.println("error opening file"); // if the file didn't open, print an error: + Serial.println("error opening temp log"); // if the file didn't open, print an error: } } } diff --git a/SD_GPS_MQTT/Wifi_Fcn.cpp b/SD_GPS_MQTT/Wifi_Fcn.cpp index 9a3d120069088a12382745ed6b1a2553412398ca..9126afc80ca8ebe50958308e3342f4f1744a4bd4 100644 --- a/SD_GPS_MQTT/Wifi_Fcn.cpp +++ b/SD_GPS_MQTT/Wifi_Fcn.cpp @@ -35,14 +35,14 @@ void mainWifiLoop (){ char rcnct_ctr_lmt = 6; if (status != WL_CONNECTED) { - Serial.print("\nNot connected to WiFi\nAttempting reconnection"); + Serial.print("Not connected to WiFi\nAttempting reconnection..."); status = WiFi.begin(ssid, pass); while( (WiFi.status() != WL_CONNECTED) && ( rcnct_ctr++ < rcnct_ctr_lmt) ){ Serial.print("."); delay(500); } if (status == WL_CONNECTED){ - Serial.println("\nConnected to WiFi"); + Serial.println("Connected to WiFi"); if (!client.connected()) { mqtt_reconnect(); } @@ -56,7 +56,7 @@ void mainWifiLoop (){ } } else if (status == WL_CONNECTED){ - Serial.println("\nConnected to WiFi"); + Serial.println("Connected to WiFi"); if (!client.connected()) { mqtt_reconnect(); } @@ -114,12 +114,12 @@ void printMacAddress(byte mac[]) { } void setup_wifi(){ - char wifi_init_cnt_lmt = 10; + char wifi_init_cnt_lmt = 50; char wifi_init_ctr = 0; WiFi.setTimeout(15000); - Serial.print("\nConnecting to "); Serial.println(ssid); + Serial.print("\nConnecting to "); Serial.print(ssid); WiFi.begin(ssid, pass); while( (WiFi.status() != WL_CONNECTED) && (wifi_init_ctr++ < wifi_init_cnt_lmt) ){ @@ -128,7 +128,7 @@ void setup_wifi(){ if (WiFi.status() == WL_CONNECTED){ // Connection succeeded randomSeed(micros()); - Serial.println("\nWiFi connected"); + Serial.println("\nWiFi initialization done!"); printCurrentNet(); printWifiData(); }else if (WiFi.status() != WL_CONNECTED){ // Connection timed out @@ -148,7 +148,8 @@ void mqtt_reconnect() { if (client.connect("arduinoClient")) { // Connection succeeded. Publish info mqtt_pub(); } else { // Connection failed. Wait before reattempting - Serial.print("failed, rc="); Serial.print(client.state()); + Serial.print("failed, rc="); Serial.println(client.state()); + client.disconnect(); delay(800); } } @@ -162,19 +163,21 @@ void mqtt_pub(){ int char_ctr = 0, buf_ctr = 0; char buff[7][60]; - File dataFile = SD.open("gpslog.txt"); + File dataFile = SD.open("gpstmp.txt"); - if (dataFile) { + if (dataFile && gps_fix_flg) { while (dataFile.available()) { c = dataFile.read(); if (c != '\n'){ // Current line continues continues if (c != ','){ // Current word continues buff[buf_ctr][char_ctr++] = c; // Append newly read char to string } else { // Word finished. Move to next word + buff[buf_ctr][char_ctr] = 0; char_ctr = 0; buf_ctr++; } } else{ // Line finished. Flush to MQTT + buff[buf_ctr][char_ctr] = 0; char_ctr = 0; buf_ctr = 0; client.publish("efss/GPS/Timestamp", buff[0]); client.publish("efss/GPS/Fix", buff[1]); @@ -183,13 +186,27 @@ void mqtt_pub(){ client.publish("efss/GPS/Longitude", buff[4]); client.publish("efss/GPS/Latitude", buff[5]); client.publish("efss/GPS/Altitude", buff[6]); +/* + Serial.print("\nTime: "); + Serial.println(buff[0]); + // Print fix + Serial.print("Fix: "); Serial.print(buff[1]); + Serial.print(" quality: "); Serial.print(buff[2]); + Serial.print(" satellites: "); Serial.println(buff[3]); + + // print location + Serial.print("Location: "); Serial.print(buff[4]); + Serial.print(", "); Serial.println(buff[5]); + Serial.print("Altitude: "); Serial.println(buff[6]); + */ } } dataFile.close(); - } - // if the file isn't open, pop up an error: - else { - Serial.println("error opening datalog.txt"); - } + SD.remove("gpstmp.txt"); Serial.println("Published to MQTT"); + }else if (!dataFile){ // if the file isn't open, pop up an error: + Serial.println("error opening gpslog.txt"); + }else if ( !gps_fix_flg){ + Serial.println("No GPS fix"); + } }