Writing multiple values to text file in one write-cycle

Hi,

I have 10 beehives. Attached to each beehive I have some different sensors that read data and send data-packages (structs) by LoRa to a TTGO-hub.

The hub receives data from each beehive. I want to write the data that is received by the hub to an SD-card plugged into the hub. I have written up an arduino-program (see the attached .ino-file) that writes to a text file with some predefined formatting (see the attached .txt-file).

This works well. However, I'm opening and closing the text-file 12 times for each loop, because I have 12 different values to write in. This is unnecessary, as my hub always gets a struct with all 12 values each time, so I might as well write them all in simultaneously. However, I cannot figure out how to tune my code to do this successfully.

Could you help me tune my code so that I write all the 12 values (vekt, temp, volt etc.) in each cycle, instead of opening and closing the SD card once for each value I'm replacing :slight_smile:

Help is appreciated.

#include "SD.h"
#include "SPI.h"
SPIClass sdSPI(HSPI);

#define SDSCK 14
#define SDMOSI 15
#define SDMISO 2
#define SDCS 13

//Sensor readings
int kubeNr = 0;
float vekt = 0;
float BStempKube = 0; 
float BStempUte = 0;
float DHTtempKube = 0;
float DHTfuktKube = 0;
float battVolt = 0;
float solVolt = 0;
int alarmValue = 0;
int pakkeNr = 0;
int signalstyrke = 0;
int senderVersjonNr = 0;
int mottagerVersjon = 0;

File dataFile;

void setup() {
  uint8_t cardType;
  Serial.begin(115200);
  Serial.println();
  Serial.println();
  Serial.println("SD_Card_Mount_Check starting - SDserialv9_virker");
  sdSPI.begin(SDSCK, SDMISO, SDMOSI, SDCS);
  if (!SD.begin(SDCS, sdSPI))
  {
    Serial.println("SD Card Mount Failed");
    while (1);
  }
  Serial.println("SD Card Mount OK");
}

void loop() {
  genRandomValues(); //Generates random placeholder sensor values for all variables
  Serial.println("Random values generated for all variables");

  writeToKubeNr(3); //In this example, we'll always write to unit 3, as proof of concept
  delay(5000);
}

void genRandomValues() {
  kubeNr = random(1, 10);
  vekt = random(1, 101);
  BStempKube = random(-5, 40);
  BStempUte = random(-10, 30);
  DHTtempKube = random(-5, 40);
  DHTfuktKube = random(20, 100);
  battVolt = random(1, 5);
  solVolt = random(1, 5);
  alarmValue = random(0, 1);
  pakkeNr = random(1, 500);
  signalstyrke = random(-100, -1);
  senderVersjonNr = 999;
  mottagerVersjon = 999;
}

void writeToKubeNr(int kubeNr) {
  String vektString = "vekt" + String(kubeNr);
  String BStempKubeString = "BStempKube" + String(kubeNr);
  String BStempUteString = "BStempUte" + String(kubeNr);
  String DHTtempKubeString = "DHTtempKube" + String(kubeNr);
  String DHTfuktKubeString = "DHTfuktKube" + String(kubeNr);
  String battVoltString = "battVolt" + String(kubeNr);
  String solVoltString = "solVolt" + String(kubeNr);
  String alarmValueString = "alarmValue" + String(kubeNr);
  String pakkeNrString = "pakkeNr" + String(kubeNr);
  String signalstyrkeString = "signalstyrke" + String(kubeNr);
  String senderVersjonNrString = "senderVersjonNr" + String(kubeNr);
  String mottagerVersjonString = "mottagerVersjon" + String(kubeNr);

  replaceVariableValue(vektString, String(vekt));
  replaceVariableValue(BStempKubeString, String(BStempKube));
  replaceVariableValue(BStempUteString, String(BStempUte));
  replaceVariableValue(DHTtempKubeString, String(DHTtempKube));
  replaceVariableValue(DHTfuktKubeString, String(DHTfuktKube));
  replaceVariableValue(battVoltString, String(battVolt));
  replaceVariableValue(solVoltString, String(solVolt));
  replaceVariableValue(alarmValueString, String(alarmValue));
  replaceVariableValue(pakkeNrString, String(pakkeNr));
  replaceVariableValue(signalstyrkeString, String(signalstyrke));
  replaceVariableValue(senderVersjonNrString, String(senderVersjonNr));
  replaceVariableValue(mottagerVersjonString, String(mottagerVersjon));
}

void replaceVariableValue(const String& targetVariable, const String& newValue) {
  // Open the file in read mode
  dataFile = SD.open("/hivedata.txt", FILE_READ);

  // Check if the file is open
  if (dataFile) {
    // Create a temporary string to store each line of the file
    String line;

    // Create a temporary string to store the modified content
    String modifiedContent;

    // Read each line from the file
    while (dataFile.available()) {
      line = dataFile.readStringUntil('\n');

      // Find the position of the target variable in the line
      int variablePosition = line.indexOf(targetVariable);

      // If the target variable is found, replace its value
      if (variablePosition != -1) {
        int equalsPosition = line.indexOf('=', variablePosition);
        int semicolonPosition = line.indexOf(';', equalsPosition);

        // Extract the part before the equals sign
        String partBeforeEquals = line.substring(0, equalsPosition + 1);

        // Append the new value
        modifiedContent += partBeforeEquals + newValue;

        // Append the remaining part after the semicolon
        modifiedContent += line.substring(semicolonPosition);

        Serial.println("Variable replaced: " + targetVariable + "with value" + newValue);
      } else {
        // If the target variable is not found, keep the line as is
        modifiedContent += line;
      }
    }

    // Close the file
    dataFile.close();

    // Reopen the file in write mode to overwrite the content
    dataFile = SD.open("/hivedata.txt", FILE_WRITE);

    // Write the modified content to the file
    dataFile.println(modifiedContent);

    // Close the file
    dataFile.close();

    Serial.println("Replacement complete.");
  } else {
    // If the file couldn't be opened, print an error message
    Serial.println("Error opening /hivedata.txt");
  }
}

hivedata.txt (2.0 KB)

kubeNr=1;vekt1=0;BStempKube1=0;BStempUte1=0;DHTtempKube1=0;DHTfuktKube1=0;battVolt1=0;solVolt1=0;alarmValue1=0;pakkeNr1=0;signalstyrke1=0;senderVersjonNr1=0;mottagerVersjon1=0;
kubeNr=2;vekt2=0;BStempKube2=0;BStempUte2=0;DHTtempKube2=0;DHTfuktKube2=0;battVolt2=0;solVolt2=0;alarmValue2=0;pakkeNr2=0;signalstyrke2=0;senderVersjonNr2=0;mottagerVersjon2=0;
kubeNr=3;vekt3=10.00;BStempKube3=29.00;BStempUte3=-7.00;DHTtempKube3=3.00;DHTfuktKube3=68.00;battVolt3=2.00;solVolt3=3.00;alarmValue3=0;pakkeNr3=450;signalstyrke3=-74;senderVersjonNr3=999;mottagerVersjon3=999;
kubeNr=4;vekt4=0;BStempKube4=0;BStempUte4=0;DHTtempKube4=0;DHTfuktKube4=0;battVolt4=0;solVolt4=0;alarmValue4=0;pakkeNr4=0;signalstyrke4=0;senderVersjonNr4=0;mottagerVersjon4=0;
kubeNr=5;vekt5=0;BStempKube5=0;BStempUte5=0;DHTtempKube5=0;DHTfuktKube5=0;battVolt5=0;solVolt5=0;alarmValue5=0;pakkeNr5=0;signalstyrke5=0;senderVersjonNr5=0;mottagerVersjon5=0;
kubeNr=6;vekt6=0;BStempKube6=0;BStempUte6=0;DHTtempKube6=0;DHTfuktKube6=0;battVolt6=0;solVolt6=0;alarmValue6=0;pakkeNr6=0;signalstyrke6=0;senderVersjonNr6=0;mottagerVersjon6=0;
kubeNr=7;vekt7=0;BStempKube7=0;BStempUte7=0;DHTtempKube7=0;DHTfuktKube7=0;battVolt7=0;solVolt7=0;alarmValue7=0;pakkeNr7=0;signalstyrke7=0;senderVersjonNr7=0;mottagerVersjon7=0;
kubeNr=8;vekt8=0;BStempKube8=0;BStempUte8=0;DHTtempKube8=0;DHTfuktKube8=0;battVolt8=0;solVolt8=0;alarmValue8=0;pakkeNr8=0;signalstyrke8=0;senderVersjonNr8=0;mottagerVersjon8=0;
kubeNr=9;vekt9=0;BStempKube9=0;BStempUte9=0;DHTtempKube9=0;DHTfuktKube9=0;battVolt9=0;solVolt9=0;alarmValue9=0;pakkeNr9=0;signalstyrke9=0;senderVersjonNr9=0;mottagerVersjon9=0;
kubeNr=10;vekt10=0;BStempKube10=0;BStempUte10=0;DHTtempKube10=0;DHTfuktKube10=0;battVolt10=0;solVolt10=0;alarmValue10=0;pakkeNr10=0;signalstyrke10=0;senderVersjonNr10=0;mottagerVersjon10=0;

writeToSDTextFile.ino (4.8 KB)

Can you explain why you are reading lines from the file, replacing content in that line, then writing the modified line to the file? That is a very unusual method of logging data to a file.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.