Need help to send a variable to electron

I wish to have a fleet of electrons that has the exact same firmware. And I want to send to each of them a different threshold value so they can compare the sensor reading with this value. I am not very sure how I am able to achieve this, can someone point me to the write direction?

Have you looked at Particle.function()?

Are you implementing that fleet as a PRODUCT?

I may have messed this up, but it should get you close.

static int  pressureAlarm = 60;  // psi

void setup() {
  // Register Particle Function to allow user to remotely Change the Pressure Alarm Setpoint.
  Particle.function("setPressure", setPressure);
}

void loop() {

}  

// Particle Function to allow user to remotely Change the Pressure Alarm Setpoint.
int setPressure(String command) {
  pressureAlarm = (command.toInt());
  Particle.publish("Pressure Alarm  set to ", String((pressureAlarm), 1), PRIVATE);

}


[edit] changed first line to “static int”

To combine the previous answers and add some suplemental info for the fuller picture:

It is perfecly possible to have all your devices run the same firmware, either by flashing all of them one-by-one with the same binary or more comfortably by organising your devices as a PRODUCT (as mentioned by @BulldogLowell) where you just upload the desired binary to the product console and the devices will be updated OTA via the cloud when they next come and stay online for at least a minute.

Once you have the firmware in place, you can provide your threshold either via a Particle.function() or via an event to which your firmware subscribes via Particle.subscribe() - the latter also allows to set multiple devices in parallel.

And in order to only have to set the value once and not for each restart over and over again, you may want to store the value in EEPROM.


@Rftop, your setPressure() should return an int and if you do it like return pressureAlarm; you can even drop the Particle.publish() as the return value already reports back what was set (or an error code when found).
And you probably also want to have some sanity check to not set bad values.

2 Likes

Thanks for all the reply, it does solve a big part of my problem. But I also want to know is there a way to set different threshold value for each device?

So... Have you looked at Particle.function() already?

2 Likes

Hi Moors, I guess the problem for me is I do not know I can do “command.toInt()”. I tried to use atoi but that is not the right way to do it.

Add your Code to what I posted (with the suggestions from ScruffR) and flash.

Then Select the device in your Particle Console:
https://console.particle.io/devices
The Function will appear on the Right Side of the Webpage.

image

Enter whatever value you want and press “CALL”

2 Likes

Hi ScruffR,

I tried to use my electron to set this value via Particle.subscribe(). I tried to atoi(data).The code complied OK,but I don’t think it change the value works. May I ask why? https://go.particle.io/shared_apps/5ac2b30240a286057e002092

One thing I see is that you are using atoi() (ASCII to Integer) but assign this to a float is this intended?

The next - most important - thing is that atoi() does not extract numbers from strings, but only tries to convert a string into a number. Meaning the numeric part of the string must be at the beginning of the string.

And third, you are publishing "number received" but subscribe to "thres", so your published event will not trigger the subscription handler.

Or are you sending the event from another device?
If so, how does that happen?