3 Commits

Author SHA1 Message Date
Ano-sys b9e982bb0c Added SD card writes -> Coords are stored as coords[0..].txt 2025-08-15 13:11:58 +02:00
Ano-sys 76f96e4245 Moved .ino to .hpp - fixed dup include and faulty declaration 2025-08-10 15:39:04 +02:00
Ano-sys cd30c25956 [!C] New Class for sd writer 2025-08-10 15:35:09 +02:00
2 changed files with 92 additions and 10 deletions
+12 -10
View File
@@ -5,6 +5,7 @@
#include <Adafruit_SSD1306.h>
#include <TinyGPS++.h>
#include "SDCardHelper.hpp"
// LORA Settings
#define SYNC_WORD 0xCE
@@ -29,9 +30,13 @@
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SD_CS 13
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
TinyGPSPlus gps;
SDHelper logger;
void sendMessage(const String& msg) {
LoRa.beginPacket();
LoRa.write(SYNC_WORD);
@@ -68,16 +73,6 @@ bool getPacket(String& payload, byte& id) {
return (payload.length() == len);
}
/*
bool waitForAck(unsigned long timeout_ms = 1000) {
if (!waitForPacket(timeout_ms)) return false;
String payload;
byte snd;
if (!getPacket(payload, snd)) return false;
return (snd == RECEIVER_ID && payload == "ACK");
}
*/
void showOnOLED(const String& line1, const String& line2 = "", const String& line3 = "", const String& line4 = "") {
display.clearDisplay();
display.setTextSize(1);
@@ -119,6 +114,12 @@ void SerialPrintGPS(){
Serial.println("");
}
void storeToSD(const String& lat, const String& lng){
static int i = 1;
Serial.println("Filename: " + logger.Filename());
logger.Log(String(i++) + ": " + lat + " " + lng);
}
void setup() {
Serial.begin(115200);
Serial2.begin(_BAUDRATE_GPS, SERIAL_8N1, _RX_GPS, _TX_GPS);
@@ -178,6 +179,7 @@ void loop() {
Serial.println("Sende GPS:\n" + lat_str + "\n" + lng_str);
showOnOLED("Sende GPS Koords:", lat_str, lng_str, satellites);
storeToSD(lat_str, lng_str);
sendMessage(lat_str + "\n\n" + lng_str + "\n\n" + satellites);
}
}
+80
View File
@@ -0,0 +1,80 @@
#pragma once
#include "FS.h"
#include <SPI.h>
#include <SD.h>
class SDHelper {
private:
uint8_t _cs;
File _stream;
String _filename;
bool _available = false;
SPIClass _spi;
String getNewFilename(fs::FS& fs) {
unsigned long i = 0;
File root = fs.open("/");
if(!root){
Serial.println("Failed to open root of SD!");
return "/coords.txt";
}
File file = root.openNextFile();
while(file){
if(String(file.name()).indexOf("coords") != -1) i++;
file = root.openNextFile();
}
return "/coords" + String(i++) + ".txt";
}
void appendFile(fs::FS& fs, const char* path, const char* message) {
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file){
Serial.println("Failed to open file for appending!");
Serial.println("Trying writing insted!");
file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println("Failed to open file for writing!");
return;
}
}
if(file.print(message))Serial.println("Wrote message!");
else Serial.println("Writing failed!");
file.close();
}
public:
SDHelper() : _spi(HSPI){
_spi.begin(14, 2, 15, 13);
if(!SD.begin(13, _spi)){
_available = false;
return;
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
}
_filename = getNewFilename(SD);
}
~SDHelper(){
if(_stream) _stream.close();
}
String Filename() { return _filename; }
void Log(const String& msg) {
appendFile(SD, _filename.c_str(), (msg + "\n").c_str());
}
};