here is my code:
/*PARTIE GENERIQUE*/
#include "StringSplitter.h"
StringSplitter *splitter;
unsigned long lastTime = 0;
unsigned long timerDelay = 5000;
int indexFichierSonCourant=-1;
String listeMP3EtCoords[100];
/*FIN DE LA PARTIE GENERIQUE*/
/* toute la partie propre au son*/
#include <SPI.h>
#include <SD.h>
#include "AudioTools.h"
#include "AudioLibs/AudioBoardStream.h"
#include "AudioCodecs/CodecMP3Helix.h"
const int chipSelect=PIN_AUDIO_KIT_SD_CARD_CS;
AudioBoardStream kit(AudioKitEs8388V1); // final output of decoded stream
EncodedAudioStream decoder(&kit, new MP3DecoderHelix()); // Decoding stream
StreamCopy copier;
File audioFile;
File audioFileLoop;
void volUp(bool, int, void*) {
Serial.println("Volument actuel="+String(kit.volume()));
kit.setVolume(kit.volume()+float(0.1));
Serial.println("Volument actuel="+String(kit.volume()));
}
void volDown(bool, int, void*) {
Serial.println("Volument actuel="+String(kit.volume()));
kit.setVolume(kit.volume()-float(0.1));
Serial.println("Volument actuel="+String(kit.volume()));
}
/*void actionKeyOff(bool active, int pin, void* ptr){
Serial.println("KeyOff");
}*/
/* fin toute la partie propre au son*/
/* toute la partie propre à la SD */
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if(!root){
Serial.println("Failed to open directory");
return;
}
if(!root.isDirectory()){
Serial.println("Not a directory");
return;
}
File file = root.openNextFile();
while(file){
if(file.isDirectory()){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}
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");
return;
}
if(file.print(message)){
//Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}
void readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if(!file){
Serial.println("Failed to open file for reading");
return;
}
Serial.print("Read from file: ");
while(file.available()){
Serial.write(file.read());
}
file.close();
}
void loadFile(fs::FS &fs, const char * path, String (&theArrayToLoad)[100]){
Serial.printf("Reading file: %s\n", path);
String res="";
byte line=0;
File file = fs.open(path);
if(!file){
Serial.println("Failed to open file for reading");
return;
}
while(file.available()){
char c = file.read();
if (isPrintable(c))
{
res.concat(c);
}
else if (c == '\n')
{
theArrayToLoad[line] = res;
res = "";
line++;
}
//Serial.write(file.read());
//res+=file.read();
}
file.close();
}
/* fin de toute la partie propre à la SD*/
/* toute la partie propre au GPS*/
#include <TinyGPS++.h>
#define GPS_BAUDRATE 9600 // The default baudrate of NEO-6M is 9600
#include <SoftwareSerial.h>
long rayonKm=1;
TinyGPSPlus gps;
bool positionFound;
const int rxPin = 21;
const int txPin = 22;
SoftwareSerial SerialGPS(rxPin, txPin);
int findClosePoint(double currLat, double currLong, String (&theArrayToLoop)[100]) {
for (int line = 0; line < 100; line++)
{
if(theArrayToLoop[line]!=NULL && theArrayToLoop[line].length()>0 ) {
//Serial.println(theArrayToLoop[line]);
splitter = new StringSplitter(theArrayToLoop[line], ',', 3); // new StringSplitter(string_to_split, delimiter, limit)
unsigned long distanceKm = TinyGPSPlus::distanceBetween(currLat, currLong, splitter->getItemAtIndex(1).toDouble(), splitter->getItemAtIndex(2).toDouble()) / 1000;
if(distanceKm<rayonKm) {
return line;
}
}
else
line=100;
}
return -1;
}
/* fin de toute la partie propre au GPS*/
void setup() {
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Info);
// setup output
auto config = kit.defaultConfig(TX_MODE);
config.sd_active = true;
kit.begin(config);
// setup file
SD.begin(chipSelect);
audioFile = SD.open("/217007.mp3");
// setup I2S based on sampling rate provided by decoder
decoder.begin();
copier.setCheckAvailableForWrite(false);
copier.begin(decoder, audioFile);
// setup additional buttons
auto act_low = AudioActions::ActiveLow;
kit.addAction(kit.getKey(1), kit.actionStartStop);
kit.addAction(kit.getKey(5), volDown);
kit.addAction(kit.getKey(6), volUp);
//gestion de la carte:
uint8_t cardType = SD.cardType();
String contentFile="Setup\n";
if(cardType == CARD_NONE){
Serial.println("No SD card attached");
return;
} else {
Serial.print("SD Card Type: ");
contentFile+="SD Card Type: ";
if(cardType == CARD_MMC){
Serial.println("MMC");
contentFile+="MMC\n";
} else if(cardType == CARD_SD){
Serial.println("SDSC");
contentFile+="SDSC\n";
} else if(cardType == CARD_SDHC){
Serial.println("SDHC");
contentFile+="SDHC\n";
} else {
Serial.println("UNKNOWN");
contentFile+="UNKNOWN\n";
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
contentFile+="SD Card Size: ";
contentFile+=String(cardSize);
contentFile+="MB\n";
listDir(SD, "/", 0);
appendFile(SD, "/log.txt", contentFile.c_str());
loadFile(SD, "/tablo.txt", listeMP3EtCoords);
Serial.println("Resultat du load de variables:");
for (int line = 0; line < 100; line++)
{
if(listeMP3EtCoords[line]!=NULL && listeMP3EtCoords[line].length()>0 )
Serial.println(listeMP3EtCoords[line]);
else
line=100;
}
}
//GPS
SerialGPS.begin(GPS_BAUDRATE); //default mode
positionFound=false;
}
void loop() {
//player.copy();
kit.processActions();
copier.copy();
long mill=millis();
String contentFile="New Loop "+String(mill)+"\n";
contentFile+="millis()="+String(mill)+", lastTime="+String(lastTime)+", delta="+String(mill - lastTime)+", delay="+String(timerDelay)+"\n";
if (SerialGPS.available() > 0) {
gps.encode(SerialGPS.read());
if ((mill - lastTime) > timerDelay) {
if(gps.location.isValid()) {
if(!positionFound) {
positionFound=true;
audioFile = SD.open("/position.mp3");
contentFile+="Fichier son demarré: /position.mp3\n";
copier.begin(decoder, audioFile);
}
contentFile+="- location: \n";
contentFile+="- latitude: ";
contentFile+=String(gps.location.lat(),6);
contentFile+="\n";
contentFile+="- longitude: ";
contentFile+=String(gps.location.lng(),6);
contentFile+="\n";
contentFile+="- altitude: ";
if (gps.altitude.isValid()) {
contentFile+=String(gps.altitude.meters());
contentFile+="\n";
} else {
contentFile+="INVALID\n";
}
int indexNew=findClosePoint(gps.location.lat(), gps.location.lng(),listeMP3EtCoords);
if(indexFichierSonCourant!=indexNew) {
indexFichierSonCourant=indexNew;
splitter=new StringSplitter(listeMP3EtCoords[indexFichierSonCourant], ',', 3);
String tmp=splitter->getItemAtIndex(0);
audioFile = SD.open("/"+tmp);
copier.begin(decoder, audioFile);
contentFile+="Fichier son demarré: /"+tmp+"\n";
}
} else {
audioFile = SD.open("/noposition.mp3");
copier.begin(decoder, audioFile);
positionFound=false;
contentFile+="- location INVALID: \n";
contentFile+="- latitude: ";
contentFile+=String(gps.location.lat(),6);
contentFile+="\n";
contentFile+="- longitude: ";
contentFile+=String(gps.location.lng(),6);
contentFile+="\n";
}
if (gps.speed.isValid()) {
contentFile+="- speed: ";
contentFile+=String(gps.speed.kmph());
contentFile+=" km/h\n";
} else {
//contentFile+="INVALID\n";
}
if (gps.date.isValid() && gps.time.isValid()) {
contentFile+="- GPS date&time: ";
contentFile+=String(gps.date.year());
contentFile+="-";
contentFile+=String(gps.date.month());
contentFile+="-";
contentFile+=String(gps.date.day());
contentFile+=" ";
contentFile+=String(gps.time.hour());
contentFile+=":";
contentFile+=String(gps.time.minute());
contentFile+=":";
contentFile+=String(gps.time.second());
contentFile+="\n";
} else {
//contentFile+="INVALID\n";
}
contentFile+="\n";
Serial.print(contentFile);
appendFile(SD, "/log.txt", contentFile.c_str());
lastTime = millis();
} else {
//Serial.print("x");
}
} else {
//Serial.print(".");
}
}
the aim is to play a specific music when I come to as specific point, as it's difficult to test in my office, I do some tests in my car and I log everything I want on the SD card... and the lasts tests give me some strange results
1/ here you can see that when I acquire the position, the date is wrong and the data are not refreshing for a long time:
New Loop 1224015
millis()=1224015, lastTime=1219007, delta=5008, delay=5000
- location INVALID:
- latitude: 0.000000
- longitude: 0.000000
- GPS date&time: 2024-8-29 7:10:51
New Loop 1229055
millis()=1229055, lastTime=1224045, delta=5010, delay=5000
Fichier son demarré: /position.mp3
- location:
- latitude: 43.626562
- longitude: 7.052498
- altitude: INVALID
- speed: 28.17 km/h
- GPS date&time: 2090-2-0 7:10:54
New Loop 1234104
millis()=1234104, lastTime=1229103, delta=5001, delay=5000
- location:
- latitude: 43.626562
- longitude: 7.052498
- altitude: INVALID
- speed: 28.17 km/h
- GPS date&time: 2090-2-0 7:10:54
New Loop 1239137
millis()=1239137, lastTime=1234127, delta=5010, delay=5000
- location:
- latitude: 43.626562
- longitude: 7.052498
- altitude: INVALID
- speed: 28.17 km/h
- GPS date&time: 2090-2-0 7:10:54
New Loop 1244169
millis()=1244169, lastTime=1239160, delta=5009, delay=5000
- location:
- latitude: 43.626562
- longitude: 7.052498
- altitude: INVALID
- speed: 28.17 km/h
- GPS date&time: 2090-2-0 7:10:54
New Loop 1249203
millis()=1249203, lastTime=1244193, delta=5010, delay=5000
- location:
- latitude: 43.626562
- longitude: 7.052498
- altitude: INVALID
- speed: 28.17 km/h
- GPS date&time: 2090-2-0 7:10:54
2/here the date is refreshing correctly but the "good" location is not so good:
New Loop 60461
millis()=60461, lastTime=55451, delta=5010, delay=5000
- location INVALID:
- latitude: 0.000000
- longitude: 0.000000
- GPS date&time: 2000-0-0 0:0:9
New Loop 65501
millis()=65501, lastTime=60492, delta=5009, delay=5000
- location INVALID:
- latitude: 0.000000
- longitude: 0.000000
- GPS date&time: 2024-8-29 16:28:24
New Loop 70541
millis()=70541, lastTime=65531, delta=5010, delay=5000
- location INVALID:
- latitude: 0.000000
- longitude: 0.000000
- GPS date&time: 2024-8-29 16:28:29
New Loop 75581
millis()=75581, lastTime=70574, delta=5007, delay=5000
Fichier son demarré: /position.mp3
- location:
- latitude: 0.000000
- longitude: 3.200000
- altitude: 2.00
- GPS date&time: 2024-8-29 16:28:34
New Loop 80621
millis()=80621, lastTime=75615, delta=5006, delay=5000
- location:
- latitude: 0.000000
- longitude: 3.200000
- altitude: 2.00
- GPS date&time: 2024-8-29 16:28:39
3/and the last one, the position is correct, the time is correct, the date is incorrect, but my car is not so nervous , but it could be explained by the fact that the 1st location is an old one, not refreshed and the second a correct one, so the library or the module thinks I went from 1st to 2nd (maybe 2 kms) in 5 secs
New Loop 377511
millis()=377511, lastTime=372501, delta=5010, delay=5000
- location:
- latitude: 43.578677
- longitude: 6.993166
- altitude: INVALID
- speed: 32.67 km/h
- GPS date&time: 2000-0-0 16:48:32
New Loop 382543
millis()=382543, lastTime=377533, delta=5010, delay=5000
- location:
- latitude: 43.564665
- longitude: 6.977619
- altitude: INVALID
Fichier son demarré: /son1.mp3
- speed: 262.98 km/h
- GPS date&time: 2003-0-0 16:52:8
New Loop 387578
millis()=387578, lastTime=382577, delta=5001, delay=5000
- location:
- latitude: 43.564665
- longitude: 6.977619
- altitude: INVALID
- speed: 262.98 km/h
- GPS date&time: 2003-0-0 16:52:8