Hallo Guys and Girls
I want to add the ability to my project to update a global variable using a web server.
The URL request would look something like this
http://thing:8080/variable/set?&variabl ... le_value=x
Where variable_name would refer the actual name of the variable and variable_value the value you want to set.
I know I could use a huge if statement but I am pretty sure there is a simple way of doing this.
I would prefer not to directly reference the variable but rather reference it using the argument inputted to the url.
Any ideas?
Reference A Variable Using String
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- Echamberlain
- Posts: 108
- Joined: Sun Aug 03, 2014 11:36 am
Re: Reference A Variable Using String
C/C++ doesn't natively support reflection. You are going to need to use an if statement to match strings to variables.
- Richmccarty
- Posts: 25
- Joined: Fri Jan 27, 2017 1:11 pm
Re: Reference A Variable Using String
If you can generate a linker output file (map?) you could write a program to parse it and then reference the variable by address. You could then use a pointer at the arduino end.
- wernerXventer
- Posts: 7
- Joined: Tue Apr 11, 2017 3:29 am
Re: Reference A Variable Using String
Thanks for the replies :-)
So in short if a make a 2d array with variable name in one column and pointer in the other I should be able to set values :-)
Now for the fun part figuring out these pointer things.
So in short if a make a 2d array with variable name in one column and pointer in the other I should be able to set values :-)
Now for the fun part figuring out these pointer things.
- wernerXventer
- Posts: 7
- Joined: Tue Apr 11, 2017 3:29 am
Re: Reference A Variable Using String
Might even be easier to have a just a 2d array one column with var name and one with var value
That way I can make a function to loop the array and either get or set the value.
That way I can make a function to loop the array and either get or set the value.
- wernerXventer
- Posts: 7
- Joined: Tue Apr 11, 2017 3:29 am
Re: Reference A Variable Using String
So I have been giving this some thought and came up with this, would this be correct and most efficient way to do this.
Something I am not too sure about though would one declare trace_level in the array as:
Something I am not too sure about though would one declare trace_level in the array as:
Code: Select all
char* array_thing_settings[3][2] = {
{"trace_level", trace_level},
OR
char* array_thing_settings[3][2] = {
{"trace_level", *trace_level},
Code: Select all
char* trace_level = "2";
char* thing_id = "thing - x";
char* thing_password = "this";
char* wifi_ssid = ";-)";
char* wifi_password = "that";
char* mqtt_server = "mqtt.zapto.org";
char* mqtt_port = "12353";
char* mqtt_username = "mqtt-user";
char* mqtt_password = "these";
char* mqtt_Subscribe_topic = "#";
char* mqtt_publish_topic = "";
char* esp_firmware_version = "10.5.0.1 - D";
char* esp_upgrade_user = "admin";
char* esp_upgrade_password = thing_password;
char* esp_upgrade_path = "/firmware/upgrade";
char* array_thing_settings[3][2] = {
{"trace_level", trace_level},
{"thing_id", thing_id},
{"thing_password", thing_password}
};
char* array_wfi_settings[2][2] = {
{"wifi_ssid", wifi_ssid},
{"wifi_password", wifi_password}
};
char* array_mqtt_settings[6][2] = {
{"mqtt_server", mqtt_server},
{"mqtt_port", mqtt_port},
{"mqtt_username", mqtt_username},
{"mqtt_password", mqtt_password},
{"mqtt_Subscribe_topic", mqtt_Subscribe_topic},
{"mqtt_publish_topic", mqtt_publish_topic}
};
char* array_firmware_settings[4][2] = {
{"esp_firmware_version", esp_firmware_version},
{"esp_upgrade_user", esp_upgrade_user},
{"esp_upgrade_password", esp_upgrade_password},
{"esp_upgrade_path", esp_upgrade_path}
};
- adafruit_support_mike
- Posts: 67446
- Joined: Thu Feb 11, 2010 2:51 pm
Re: Reference A Variable Using String
The general structure you're looking for is called a 'hash table'. The arbitrary string is reduced to a fixed-size integer using what's known as a 'hashing function', and the main feature of hashing functions is that they produce different output values for different input strings, at least to extremely high probability.
The hashed output is then used as the index into an array, or more often stored as one part of a linked data structure. The literal array size for a 32-bit hash table would be unnecessarily large.
The hashed output is then used as the index into an array, or more often stored as one part of a linked data structure. The literal array size for a 32-bit hash table would be unnecessarily large.
Please be positive and constructive with your questions and comments.