Reading more than one var from URL/curl

Hi,

Probably doing something incredibly dumb but trying to debug why overall something works but a voltage reading seems way out of whack and want to call and see two variables at once. Code below:

int temp = 0;
double voltage = 0.0;
void setup ()
{
Spark.variable(“temp”, &temp, INT);
Spark.variable(“voltage”, &voltage, DOUBLE);
pinMode(A0, INPUT);
}
void loop()
{
int readVal = analogRead(A0);
voltage = (readVal * 3.3) / 4095;
temp = (voltage - 0.5) * 100;
}

The voltage is coming back with as very very small (-4e294) yet the temp is looking correct? Now my c++ is rusty but I really can’t see what’s going on here. Really appreciate where I’m going stupidly wrong.

Thanks

Chris

Hi @Blacksheep, I’m not sure if DOUBLE is working yet for spark variables. See this thread.

Ah, that’s a bit of a pain - I know long wasn’t working but would have thought if it compiles it should work.

But on the first q, if I did want to read two vars at once would I just separate with an ampersand on the URL?

Now that the limit of 9 characters has been lifted, and the compiler is parsing strings correctly, you can do something like this:

double t = 22.69;
double h = 35.67;
bool s = 0;
char output[32];

void setup() {
  Spark.variable("read", &output, STRING);
  pinMode(D7,OUTPUT);
}

void loop() {
  t += 0.01;
  h += 0.01;
  
  // Create fake JSON
  // Parse on receiving end
  // var obj = JSON.parse(receivedString);
  sprintf(output, "{\"temp\":%.2f,\"volt\":%.2f}", t, h);
  
  delay(1000);
  s = !s;
  digitalWrite(D7,s);
}

Parses nicely:

1 Like

I like this idea, thanks @BDub. May try using it on my weather station. Should be more fast & efficient than 10 separate variable reads.

Ah thanks - one way round I guess. Anything else I guess I can create more specific functions for.

Thanks again.