I am programming SPARTKFUN VROOM Thing Plus C for use with the SD card and I am struggling with how to write a int to the SD card.
The sample code works fine with characters or text, i.e “Hello” but the FS.h code uses pointers which are not my strong point.
The SD_Test code uses appendFile(SD, “/LOW_2”, “World!\n”); to send text (“World!”) but how would I sens a integer ; replacing World! with my integer(count) does not work.
I am incrementing the integer count and I want to write it to the SD card but I am stuck. Following is my complete code
/*************
4/20/2023- STARTED WITH SPARKFUN WIFI EAMPLE CODE"SPARKFUN_WIFI.ino"
ADDED SD_Test.ino, removed un-needed code and added counter code without RTC
***********/
#include <WiFi.h>
// WiFi network name and password:
const char * networkName = "xxxxxxx";
const char * networkPswd = "yyyyyyyyyy";
#include "FS.h"
#include "SD.h"
#include "SPI.h"
const int sd_cs = 5; //Thing Plus C
int targetPin = 33; //INCREMENT TARGETS COUNT, SEND TO SD CARD
int target;
int count;
int counter;
int counterPin = 32; //RESET COUNTER TO ZERO
const int LED_PIN = 13;
void setup()
{
// Initilize hardware:
Serial.begin(115200);
pinMode(LED_PIN,OUTPUT);
pinMode(targetPin,INPUT);
pinMode(counterPin,INPUT);
// Connect to the WiFi network (see function below loop)
connectToWiFi(networkName, networkPswd);
digitalWrite(LED_PIN, LOW); //BLUE LED OFF
Serial.println("SD test");
if (!SD.begin(sd_cs)) {
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
}
Serial.print("SD Card Type: ");
if (cardType == CARD_MMC) {
Serial.println("MMC");
} else if (cardType == CARD_SD) {
Serial.println("SDSC");
} else if (cardType == CARD_SDHC) {
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
/****************
listDir(SD, "/", 0);
createDir(SD, "/mydir");
listDir(SD, "/", 0);
removeDir(SD, "/mydir");
listDir(SD, "/", 2);
writeFile(SD, "/hello.txt", "Hello ");
************/
//appendFile(SD, "/LOW_2", "World!\n");
// `readFile(SD, "/hello.txt");
/*******************
deleteFile(SD, "/foo.txt");
renameFile(SD, "/hello.txt", "/foo.txt");
readFile(SD, "/foo.txt");
testFileIO(SD, "/test.txt");
Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));
*******************/
Serial.println("FILE NAME = SPARKFUN_WIFI_SD_cleaned_code");
}
void loop()
{
counter = digitalRead(counterPin);
if (counter == HIGH)
{
Serial.println("counter == HIGH");
count = 0;
}
target = digitalRead(targetPin);
if (target == HIGH)
{
Serial.println("target == HIGH");
//DateTime now = rtc.now();
count = count + 1;
Serial.print("Count = ");
Serial.println(count);
Serial.println("opening data fiLe");
appendFile(SD,"/LOW_2", "count\n");
delay(500);
}
}
void connectToWiFi(const char * ssid, const char * pwd)
{
int ledState = 0;
Serial.println("Connecting to WiFi network: " + String(ssid));
WiFi.begin(ssid, pwd);
while (WiFi.status() != WL_CONNECTED)
{
// Blink LED while we're connecting:
digitalWrite(LED_PIN, ledState);
ledState = (ledState + 1) % 2; // Flip ledState
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
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();
}