/* Pi Pico W WiFi Station Demo picow-wifi-station.ino Use WiFi library to connect Pico W to WiFi in Station mode DroneBot Workshop 2022 https://dronebotworkshop.com */ // Include the WiFi Library #include #include // Inclusions for GPS #include "TinyGPSPlus.h" #include "SoftwareSerial.h" #include "LittleFS.h" #include "Arduino.h" SoftwareSerial serial_connection (1, 0); TinyGPSPlus gps; // GPS object #define button_pin 14 // set up logfile and memory handling File logFile; unsigned long lastWriteFlushMs = 0; unsigned long lastLoggedSecond = 99999; // Network credentials const char* ssid = "Fen House"; const char* password = "Wetlands6"; const char* serverUrl = "http://192.168.68.51:5001/api/log"; // change this // Variable to store onboard LED state String picoLEDState = "off"; // set up time tracking for GPS String isoTimeUTC() { if (gps.date.isValid() && gps.time.isValid()) { char buf[25]; // YYYY-MM-DDThh:mm:ssZ snprintf(buf, sizeof(buf), "%04d-%02d-%02dT%02d:%02d:%02dZ", (int)gps.date.year(), (int)gps.date.month(), (int)gps.date.day(), (int)gps.time.hour(), (int)gps.time.minute(), (int)gps.time.second()); return String(buf); } return String(""); // unknown yet } // set up header for CSV void writeCSVHeader() { if (!logFile) return; logFile.println(F("time_utc,lat,lon")); } // dump flash memory into CSV and print it to serial monitor void dumpFile() { Serial.println(F("\n----- BEGIN walk.csv -----")); File f = LittleFS.open("/walk.csv", "r"); if (!f) { Serial.println(F("No /walk.csv file found.")); return; } while (f.available()) { Serial.write(f.read()); } f.close(); Serial.println(F("\n----- END walk.csv -----\n")); } void setup() { // Start the Serial Monitor Serial.begin(115200); serial_connection.begin(115200); Serial.println("GPS Start"); // sketch starting // Operate in WiFi Station mode WiFi.mode(WIFI_STA); // Start WiFi with supplied parameters WiFi.begin(ssid, password); // Print ... on monitor while establishing connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); delay(500); } // Connection established Serial.println(""); Serial.print("Pico W is connected to WiFi network "); Serial.println(WiFi.SSID()); // init LED pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, HIGH); // init flash memory if (!LittleFS.begin()) { Serial.println(F("LittleFS mount failed. Did you select a Flash Size with FS? (Tools > Flash Size)")); // You can still run, but logging won't work. } // Open /walk.csv for write (overwrite each run) logFile = LittleFS.open("/walk.csv", "w"); if (!logFile) { Serial.println(F("Failed to open /walk.csv for writing")); } else { writeCSVHeader(); Serial.println(F("Logging to /walk.csv")); } } bool button_up = true; void loop() { // while connection is available, encode GPS feedback while(serial_connection.available()) { gps.encode(serial_connection.read()); } // sense if button is down, and record + print full GPS Message if ((digitalRead(button_pin) == LOW) && button_up) { digitalWrite(led_pin,HIGH); Serial.println("BUTTON DOWN_______________ RECORDING"); // if we get a valid location, print it: if (gps.location.isValid()) { double lat = gps.location.lat(); double lon = gps.location.lng(); String tiso = isoTimeUTC(); // print the location to serial Serial.println("Latitude:"); Serial.println(lat, 6); Serial.println("Longitude"); Serial.println(lon, 6); // if the log file is there, print data to the log file if (logFile) { Serial.println("writing to walk.csv"); // CSV: time_utc,lat,lon, logFile.print(tiso); logFile.print(','); logFile.print(lat, 6); logFile.print(','); logFile.print(lon, 6); logFile.print(','); logFile.println(); } else { Serial.println("did not find logfile"); } } else { // print error to serial Serial.println("GPS Location Invalid"); } button_up = false; } // when the button goes back up, flush the log file data and turn off the LED else if ((digitalRead(button_pin) == HIGH) && !button_up) { digitalWrite(led_pin,LOW); button_up = true; logFile.flush(); } // IF THERE IS WIFI, send whatever you have in walk csv if (WiFi.status() == WL_CONNECTED) { Serial.print("WiFi status: "); Serial.println(WiFi.status()); //Serial.print("Pico IP: "); //Serial.println(WiFi.localIP()); //Serial.print("Server URL: "); //Serial.println(serverUrl); HTTPClient http; http.begin(serverUrl); http.addHeader("Content-Type", "application/json"); File f = LittleFS.open("/walk.csv", "r"); if (!f) { Serial.println(F("No /walk.csv file found.")); return; } while (f.available()) { String line = f.readStringUntil('\n'); line.trim(); if (line.length() == 0) continue; // parse CSV: lat, long, time int i1 = line.indexOf(','); int i2 = line.indexOf(',', i1 + 1); int i3 = line.indexOf(',', i2 + 1); if (i1 < 0 || i2 < 0 || i3 < 0) { Serial.print("Bad line: "); Serial.println(line); continue; } String lat_string = line.substring(0, i1); String lon_string = line.substring(i1 + 1, i2); String t_string = line.substring(i2 + 1, i3); // build JSON payload String payload = "{"; payload += "\"timestamp\":" + t_string; payload += ",\"lat\":" + lat_string; payload += ",\"lon\":" + lon_string; payload += "}"; Serial.print("Sending: "); Serial.println(payload); int httpCode = http.POST(payload); Serial.printf("POST code: %d\n", httpCode); String response = http.getString(); Serial.println(response); http.end(); } // String payload = "{\"temperature\": 23.5, \"humidity\": 45}"; } delay(10000); // send every 10 seconds }