getenv/setenv - Arduino equivalent

Hi,

I'm currently looking at how I can SET and GET variables over ethernet. I'm unsure of what function I need to call. So, Here's the aim:

int VARIABLE_NAME = 10;

/getVariable?variable=VARIABLE_NAME

So when I call this, I will need to do something as follows:

server.print(variable);

I'd assume in C++, I could of used server.print(getenv(variable)) but getenv is not declared, and nor is setenv

xerof:
I'd assume in C++, I could of used server.print(getenv(variable)) but getenv is not declared, and nor is setenv

You'd be wrong. 'C' and C++ do not define any API to do that - it is an operating-system-specific operation. Since Arduino does not have anything remotely resembling a POSIX operating system, it does not provide any support to set or get environment variables - because it has no concept of environments and processes.

If you want to get/set persistent variables within the Arduino, you could write your own code to store them in EEPROM.

If you want to send data from or to the Arduino over ethernet, you can use a web server or client. Check out this tutorial: http://arduino.cc/en/Tutorial/WebServer

Right, so I'm using Webduino, which sets the variables (name/value), so name would contain the variable name, and value would contain the value I want the variable to be, but how do I set the Variable.

in PHP, I would do as such (Even though this isn't PHP, PHP is my strong point) ${$name} = $value; (So, This would take the variable name, and set the variable within the variable name (make sense? ))

I know I could do it long winded for each variable using a case on the name variable so example:

int variable_A = 0;
int variable_B = 1;

if (name == "variable_A") { variable_A = value; }
else if (name == "variable_B") { variable_B = value; }

Where really, I want to be doing name=value, but name being what ever the contents of name being (so either variable_A or variable B)

Just found this from an old post:

C and C++, being compiled rather than interpreted languages, don't have the ability for the application to access the symbol namespace used at compile time, so they don't do this sort of thing. By the time the binary code is loaded into the AVR, all your variables have become nothing more than memory references, and the names of the variables are no longer even present.

Looks like I'll have to do my if else case loop then. Will have to use the Mega for this project then, 32KB is just not enough with all the checking etc etc.

xerof:
Right, so I'm using Webduino, which sets the variables (name/value), so name would contain the variable name, and value would contain the value I want the variable to be, but how do I set the Variable.

in PHP, I would do as such (Even though this isn't PHP, PHP is my strong point) ${$name} = $value; (So, This would take the variable name, and set the variable within the variable name (make sense? ))

I know I could do it long winded for each variable using a case on the name variable so example:

int variable_A = 0;
int variable_B = 1;

if (name == "variable_A") { variable_A = value; }
else if (name == "variable_B") { variable_B = value; }

Where really, I want to be doing name=value, but name being what ever the contents of name being (so either variable_A or variable B)

So in other words, you want to convert a string received over ethernet into a variable name?

If you're intent on NOT doing it with if/else statements, then you can use a lookup table and to put your variables into an array. Example:

int myVars[2] = { myVar1, myVar2 };
char hashTable[2][8] = { "myVar1", "myVar2" };

You then would loop through your lookup table, using strcmp() to see if your string matches the indexed value. If it does, take the index and use that for your myVars array.

It isn't really worth doing for only a couple variables, but the more variables you add, the more memory you use. Since we're working with a limited amount of memory, there are rarely any circumstances that warrant using this on a micro.