I’m trying to use a couple of particle functions to remotely configure my device in the field.
One function would send sensor calibration parameters; the other would send environmental calibration parameters. All of the parameters are double-precision floating point.
Ideally, there would be only two functions (one sensor, one environmental) where each function sets 3 parameters. (so 6 parameters total.) Currently, the parameters are stored as retained variables (so they don’t disappear if I “sleep deep”). Eventually, the parameters would be stored using the eeprom emulation capabilities so that they would survive even power cycles.
I tentatively wanted to pass the parameters as a command-separated list, but I’m having trouble parsing that from the function command string.
The first approach that I tried was to convert the string class argument holding the comma-separated list into a character array, then parse the character array using sscanf. sscanf always returned zero (meaning it converted zero characters) and the variables that it was supposed to update were unchanged. I added debug prints to the code to confirm that the function was being called and that the string arguments were really there (both before and after the conversion to a character array). All of that looked good.
I also tried passing only one argument (no commas). Still sscanf did nothing.
What does work is to directly convert a single argument (no commas) to a float using the String class “toFloat()” method… but then I need a separate function call for each argument – so six separate functions rather than two.
I suspect I could come up with a loop to parse through the String class string, and call toFloat on multiple comma-separated substrings. This is probably the best and right way, but I figured that I’d ask and see if anyone has suggestions.
BTW, the following code does as expected if compiled and run on my Linux box. I.e., it prints 6.400000,9.200000,-2.784260 whereas I would expect to get 100.000000,101.000000,102.000000 if I tried it on my electron, based on the behavior I’ve seen so far. (Didn’t actually try this exact code on the electron.)
Thanks,
Jeff
#include "stdio.h"
static const char mystring[] = “6.4,9.2,-2.78426”;
double a = 100;
double b = 101;
double c = 102;
void main() {
sscanf( mystring, “%lf,%lf,%lf”, &a, &b, &c );
printf( “%f,%f,%f\n”, a, b, c );
}