Hi All,
Would someone be willing to provide an example of how to read/populate a variable from a file on an SDCard?
Currently all of the variables are written directly into code such as...
byte ip = {192,168,1,1};
int StationID = 1;
etc...
My goal is to have a .txt or .ini file on the SDCard with this info so a user can provide the changes without having to recompile and upload to the device.
I don't really care what the file is called and i am not too worried about the structure.
Something like...
StationID = 1
ArduinoIP = 192,168,1,1
Would be fantastic and reasonably self-explanatory to the user.
My current hardware configuration is the Arduino UNO with the Ethernet/SD Shield purchased from Adafruit.
I have been accessing/working thru the examples on the SDCard with the SDFat lib.
Sorry but I only have a week or so of experience with Arduino/C and learn mostly by example and doing.
I just haven't found anything yet i can get my head around yet.
Reading data from a file is the inverse of writing data to the file. So, the first thing you need to define is the EXACT format that the data in the file will have.
The next thing you need to do is to be able to read a record from the file. Precisely how to do that depends on exactly which library you are using to read from the SD card, and exactly how the data is written to the SD card.
Finally, you need to parse the data in that record. You could use strtok() to extract the tokens, and atof(), atoi(), etc. to convert the tokens to numbers, or you could use sscanf to extract the data from the record.
The token approach is better if the record format changes a lot (one value, then 5 values, etc.). The sscanf approach is easier if the records are all alike (and don't contain floats).
Making progress but looking to understand what I am doing wrong with setting the variable.
Any advice welcome...
So... I have taken the advice from PaulS and nailed down the File structure.
The format of the text file is below:
1
0xDE,0xAD,0xBE,0xEF,0xFE,0xED
192,168,3,195
216,139,202,28
99
32
80
1
# ---------------------------------------------------------
# The file structure below must be followed exactly as the Arduino reads
# the first 8 lines of the file
# Line 1 = StationID ie: 1
# Line 2 = Arduino MAC Address
# Line 3 = Arduino IP Address ie: 192,168,3,195
# Line 4 = Mail Server IP Address ie: *,*,202,28
# Line 5 = Maximum Farenheit Temperature ie: 99
# Line 6 = Min Farenheit Temperature ie: 32
# Line 7 = Maximum Humidity ie: 80
# Line 8 = Min Humidity ie: 1
# Any questions regarding editing this file? - See Shane
Next I am reading the file line by line using the full code below.
But I cannot figure out how to set the var .... I continue to get 55: error: invalid array assignment
This is the section of code where i am trying to set it...
if(line_number == 1)
{
Serial.print("Output in the if statement for line 1 is ");
Serial.println(buffer);
SensorID = buffer;
}
Full Code
// Ported to SdFat from the native Arduino SD library example by Bill Greiman
// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
const int chipSelect = 4;
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe
modified by Bill Greiman 11 Apr 2011
This example code is in the public domain.
*/
#include <SdFat.h>
SdFat sd;
void setup() {
Serial.begin(9600);
// Initialize SdFat or print a detailed error message and halt
// Use half speed like the native library.
// change to SPI_FULL_SPEED for more performance.
if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt();
const int line_buffer_size = 50;
char buffer[line_buffer_size];
char SensorID[50];
ifstream sdin("CONFIG.TXT");
int line_number = 0;
Serial.println("... inside the Loop");
while (sdin.getline(buffer, line_buffer_size, '\n') || sdin.gcount())
{
int count = sdin.gcount();
count--; // Don’t include newline in count
++line_number;
if(line_number == 1)
{
Serial.print("Output in the if statement for line 1 is ");
Serial.println(buffer);
SensorID = buffer;
}
if(line_number == 2)
{
Serial.print("Output in the if statement for line 2 is ");
Serial.println(buffer);
}
} // end of while
Serial.println("did the sensor ID print below ?");
Serial.println(SensorID);
}
void loop() {
// nothing happens after setup
}
Okay, there is the code for a class that does what you need. I'll paste a sample config file below.
You can change the type of value you are reading easily. Use atoi() for an int, use atof() for a float. A character string is even easier. With this, you need to decide what your parameters are ahead of time, which is pretty normal.
Then change the getters (methods that return your values) accordingly.
Let me know what questions you have.
; this is a comment
;
DAMP_LEVEL_TBS=3
;
LONG_PRESS=10
;
BOATLENGTH=30
;
GPS_ANTENNA_LOCATION = 30
;
WIND_XDUCER_HEIGHT=43