gps differentiates between case 0 and case 1

This commit is contained in:
unknown
2023-08-08 21:59:37 -04:00
parent 252ea4d3cd
commit ea82ba13be
+95 -54
View File
@@ -1,11 +1,10 @@
// =================================================================== // ===================================================================
// Auth: alex // Auth: alex
// File: gps_conn_static.ino // File: gps_conn_static.ino
// Revn: 08-01-2023 2.0 // Revn: 08-08-2023 3.0
// Func: connect GPS module to Golang's net.Conn via ESP8266 // Func: connect GPS module to Golang's net.Conn via ESP8266
// //
// TODO: first loop static bool to distinguish between case 0 and 1 // TODO: store data in EEPROM, retrieve it after power failure
// store data in EEPROM, retrieve it after power failure
// =================================================================== // ===================================================================
// CHANGE LOG // CHANGE LOG
// ------------------------------------------------------------------- // -------------------------------------------------------------------
@@ -19,6 +18,10 @@
// (very broken) // (very broken)
//*08-01-2023: ac removed spin loop, added isUpdated() condition to //*08-01-2023: ac removed spin loop, added isUpdated() condition to
// client sending block // client sending block
// 08-07-2023: ac rough bones for separating case 0 from case 1
//*08-08-2023: ac fleshed out the rough bones
// moved smart delay and updated/valid prints out of
// mode if/else blocks
// //
// =================================================================== // ===================================================================
@@ -33,6 +36,7 @@
// wifi credentials // wifi credentials
#ifndef STASSID #ifndef STASSID
//#define STASSID "kaer fdsafdsafdsa"
#define STASSID "kaer morhen" #define STASSID "kaer morhen"
#define STAPSK "project:sentinel" #define STAPSK "project:sentinel"
#endif #endif
@@ -57,6 +61,8 @@ const uint16_t port = 1202;
// literally keep track of how many messages have been sent // literally keep track of how many messages have been sent
int count = 0; int count = 0;
int attempts = 0; // keep track of connection attempts
bool mode = false; // keep track of connection status
// Create an Instance of the TinyGPS++ object called gps // Create an Instance of the TinyGPS++ object called gps
TinyGPSPlus gps; TinyGPSPlus gps;
@@ -79,36 +85,30 @@ void setup() {
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
// wait for connection to be established // wait for connection to be established
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED && attempts < 10) {
delay(500); delay(500);
Serial.print("."); Serial.print(".");
attempts++;
} }
// print connection details // keep track of whether connected or not
mode = WiFi.status() != WL_CONNECTED;
Serial.println(""); Serial.println("");
Serial.println("WiFi connected"); if( mode ) { // attempt to connect to wifi
Serial.print("IP address: "); // print connection details
Serial.println(WiFi.localIP()); Serial.println("WiFi NOT connected");
} else {
// print connection details
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
} }
void loop() { void loop() {
// print connecty things every loop
// it disconnects every loop. Don't love this tbh
Serial.print("connecting to ");
//Serial.print(host);
Serial.print(':');
Serial.println(port);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("connection failed");
delay(5000);
//return;
}
Serial.print( "isUpdated: " ); Serial.print( "isUpdated: " );
Serial.println( gps.location.isUpdated() ); Serial.println( gps.location.isUpdated() );
Serial.print( "isValid: " ); Serial.print( "isValid: " );
@@ -116,46 +116,87 @@ void loop() {
delay( 1000 * 30 ); delay( 1000 * 30 );
// 1000 milliseconds in one second // 1000 milliseconds in one second
// 30 seconds in one-half minute // 30 seconds in one-half minute
// }
// This will send gps info // XXX This is case 0, where the GPS does not connect to WiFi XXX
Serial.println("sending gps data"); if( mode ) { // mode is true when WiFi is not connected
// only send message if client is connected and location is valid // only send message if client is connected and location is valid
if (client.connected() && gps.location.isValid()) { if (gps.location.isValid()) {
switch( count ) { // This will only show gps info
case 0: // on the first message Serial.println("showing gps data");
// send start message switch( count ) {
client.println("ot"); case 0: // on the first message
Serial.println("start/ot"); Serial.println("start/ot");
count++; count++;
break; break;
case 15: // on the last message case 15: // on the last message
// send finale message Serial.println("fin/za");
client.println("za"); count = 0; // restart count
Serial.println("fin/za"); break;
count = 0; // restart count default: // data messages
break; // print data to serial monitor
default: // data messages Serial.print(gps.location.lat(), 6); // 6 decimals of precision
// send data to server Serial.print(",");
client.print(gps.location.lat(), 6); // 6 decimals of precision Serial.println(gps.location.lng(), 6);
client.print(","); count++;
client.println(gps.location.lng(), 6); break;
// print data to serial monitor }
Serial.print(gps.location.lat(), 6); // 6 decimals of precision }
Serial.print(","); // XXX This is case 0, where the GPS connects to WiFi XXX
Serial.println(gps.location.lng(), 6); } else { // mode is false when WiFi is connected
count++; // print connecty things every loop
break; // it disconnects every loop. Don't love this tbh
Serial.print("connecting to ");
//Serial.print(host);
Serial.print(':');
Serial.println(port);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("connection failed");
delay(5000);
//return;
}
// only send message if client is connected and location is valid
if (client.connected() && gps.location.isValid()) {
// This will send and show gps info
Serial.println("sending gps data");
switch( count ) {
case 0: // on the first message
// send start message
client.println("ot");
Serial.println("start/ot");
count++;
break;
case 15: // on the last message
// send finale message
client.println("za");
Serial.println("fin/za");
count = 0; // restart count
break;
default: // data messages
// send data to server
client.print(gps.location.lat(), 6); // 6 decimals of precision
client.print(",");
client.println(gps.location.lng(), 6);
// print data to serial monitor
Serial.print(gps.location.lat(), 6); // 6 decimals of precision
Serial.print(",");
Serial.println(gps.location.lng(), 6);
count++;
break;
}
} }
} }
smartDelay(500); // Run Procedure smartDelay smartDelay(500); // Run Procedure smartDelay
// if GPS is not connected correctly // if GPS is not connected correctly
if (millis() > 5000 && gps.charsProcessed() < 10) if (millis() > 5000 && gps.charsProcessed() < 10)
Serial.println(F("No GPS data received: check wiring")); Serial.println(F("No GPS data received: check wiring"));
}
}
// Custom version of delay() ensures gps object is being "fed" // Custom version of delay() ensures gps object is being "fed"
static void smartDelay(unsigned long ms) { static void smartDelay(unsigned long ms) {