I am using a MKR Zero with an SD card loaded, I have a timer that wakes up the MKR every 10 seconds to take readings, and store them on the SD card, then cut power to the MKR.
Every 5 minutes I plan on sending these values from the SD card over radio (UART).
My main roadblock right now is reading the values from the .txt file on the SD and converting them to a suitable format for sending over UART to the radio.
I originally was using String
but have since read about all the dangers of using String
due to memory fragmentation (Evils of Strings
So for example, if I have 3 .txt files that contain this text:
ch1.txt = 100,100,100,100,
ch2.txt = 200,200,200,200,
ch3.txt = 300,300,300,300,
I want to read each line, combine them, then send over UART like this:
Serial1.print("AT+SEND=1:12: 100,100,100,100,200,200,200,200,300,300,300,300,300,")
Where Serial1
is the radio UART port.
This code will read the .txt file and print easily:
#include <SPI.h>
#include <SD.h>
const int chipSelect = SDCARD_SS_PIN;
File ch1_file;
void setup() {
Serial.begin(9600);
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("SD initialization failed.");
while (true);
}
Serial.println("initialization done.");
}
void readData()
{
ch1_file = SD.open("ch1.txt");
while (ch1_file.available())
{
char c = ch1_file.read();
Serial.print(c);
}
ch1_file.close();
}
void loop() {
delay(5000);
readData();
}
This outputs in the console 100,100,100,100,
as expected.
How can I read all three .txt files, append them into one object, then send without converting anything to a String
.
I tried to create arrays like this:
#include <SPI.h>
#include <SD.h>
const int chipSelect = SDCARD_SS_PIN;
File ch1_file;
File ch2_file;
File ch3_file;
char arr1[13];
char arr2[13];
char arr3[13];
char arrCombined[39];
void setup() {
Serial.begin(9600);
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("SD initialization failed.");
while (true);
}
Serial.println("initialization done.");
}
void readData()
{
int j=0;
ch1_file = SD.open("ch1.txt");
while (ch1_file.available())
{
char c = ch1_file.read();
arr1[j] = c;
j += 1;
}
ch1_file.close();
ch2_file = SD.open("ch2.txt");
while (ch2_file.available())
{
char c = ch2_file.read();
arr2[j] = c;
j += 1;
}
ch2_file.close();
ch3_file = SD.open("ch3.txt");
while (ch3_file.available())
{
char c = ch3_file.read();
arr3[j] = c;
j += 1;
}
ch3_file.close();
strcat(arrCombined, arr1);
strcat(arrCombined, arr2);
strcat(arrCombined, arr3);
Serial.println(arrCombined);
}
void loop() {
delay(5000);
readData();
}
This printed nothing to the console.
Even if this did work and printed 100,100,100,100,200,200,200,200,300,300,300,300,300,
, I am unsure how to create the required String
(assuming that is a string?) to send the radio command.
I just wish I could use Strings!