Post an integer to the spark

Hello
I have a photoresistor connected to analog input to turn on the lights (if analog input is greater than X turn on the light). I want to use the spark to change the threshold value, instead of flashing with the new value, to post the new value to the spark, something like :
in the spark code

{
If analogRead(A1) > threshold than
digitalWrite(D1, HIGH);
}

and to send the value of threshold like this

curl https://api.spark.io/v1/devices/0123456789abcdef/func1 \
  -d access_token=123412341234 \
  -d threshold=40

Did anyone did anything similar or has an idea on how to implement it ?

Thanks

Hi @coconut

I think it would help you to look at my tutorial for having a variable and function on one web page:

The code there is for a servo but it is very similar to what you need.

2 Likes

Hey @coconut,

that’s easy! :smiley:

You can use a Spark.function() for this purpose. Pass in a value and the function will update the threshold.

int threshold = 0;

void setup(){
  Spark.function("threshold", set_thres);
}

int set_thres(String value){
   threshold = value.toInt();
   return threshold;
}
2 Likes

Thank you.

Might want to load the last value from memory (EEPROM.read) at startup and then write it whenever it changes (EEPROM.Write) so your threshold value will survive restarts and power failure

1 Like