I recently got ahold of the Arduino Ethernet+SD Shield and can configure both to work however I'd like to see if there is a way to read a file off of SD and start the Ethernet module with parameters read off of it.
As an example, in the setup() function I am working on, the SD card is mounted, the file is read and the values are assigned to variables that will be used in the Ethernet module. The issue is how to go about reading the config from the SD card. Currently, I have two programs. One prompts the user over serial for the configuration data, then writes the config file and the other will be the "main" program that uses the Ethernet module. This method is cumbersome to change the IP address and I'd rather have a config file that is human readable.
Currently the config is written as so:
MMMMMMIIIINNNNGGGGRRRRXXXXXXXXXXXXXXXX
M=MAC Address = Each ASCII character represents two characters of the MAC address: (Ex: 0xDE = 222 = (unprintable character) )
I=IP address, where each octet is stored in a character (Ex: 74.110.41.122 = Jn)z )
N = Netmask, stored same as IP address
G = Gateway, stored same as IP address
R = Remote Server (to be used by the main program)
XXXX....XXX = API key (used by the main program in communication with the Remote Server)
I'd like to see if I could somehow update the code to use a better more easily written and read config:
Does any one have any code samples or recommended practices for being able to parse out the configuratuion? Ultimately I need to be able to distill the config file down so I can initialize the Ethernet address:
Ultimately I need to be able to distill the config file down so I can initialize the Ethernet address:
Ethernet.begin("$mac',"$ip", "$gw","$nm");
That is not going to happen unless you extensively rewrite the Arduino compiler and linker to support untyped variables, and rewrite the Ethernet class to take strings.
But, it is possible to read configuration data from an SD card, and set the values in the mac, ip, gateway, and network mask variables.
I'd like to see if I could somehow update the code to use a better more easily written and read config:
"The code"? What code?
Whatever code it is, the answer is yes, and it isn't even all that difficult. If the file on the SD card has appropriate end-of-record markers, it is possible (even easy) to read a complete record from the SD card into a character array.
The strtok() function can then be used to find tokens, using various delimiters. The proposed IP, NM, etc. lines are good (easy to parse). The proposed mac address line is not. The MAC address consists of 6 values. Why jam them all together?
Once you know what kind of data is on the line (mac address, IP address, network mask, etc.), you know what delimiter to use to parse the data portion. itoa() or strtol() can be used to convert the tokens ("DE" for instance) to numbers that can be stored in the appropriate (mac address, for instance) array variable.
firestorm_v1:
I recently got ahold of the Arduino Ethernet+SD Shield and can configure both to work however I'd like to see if there is a way to read a file off of SD and start the Ethernet module with parameters read off of it.
I wrote an IniFile library for this kind of thing: GitHub - stevemarple/IniFile: Arduino library to parse ini files.. It even handles IP addresses/netmasks. You'll need to use version 1.0 of the Arduino software. Make sure the buffer you provide is large enough to handle the longest line in the file.