So, I currently am building a state machine on an argon so that it has an idle mode, a warmup mode, and a measuring mode. I have got the state machine itself down, but I want the individual modes of the states to be controlled by a command typed into the CLI. For example, the default state would be idle mode, but when "warmup" is typed into the CLI the state would change to warmup mode. Is there any way to do this?
@efarquhar, when you say "typed into the CLI" are you thinking that the command would be sent via the cloud or via USB?
Hi @efarquhar!
The Particle CLI is mainly for device management, I recommend you use Serial instead. The device uses the same USB port but appears as a virtual COM port on your computer so you can send commands and even receive data or confirmation. You can learn about Serial and how to use it here.
You can also try cloud functions to expose a function and call them when requested through the cloud API. Here you can have more information. You could create a function to make the desired changes on your state machine.
Hi,
I should have said I use the Particle extension on Visual Studio for all firmware purposes. So I was thinking the command would be sent over USB.
Thanks!
Hi,
Thanks! Do you know of any examples where someone has done this before? I see where I can request info from the device using the particle commands, but I don't see any examples where the code is already running and waiting in an idle state for me to tell it to measure.
Touching on what @jalbersr said about exposing cloud functions, there is a ready made example which runs, as is, on my Argon device:
https://docs.particle.io/reference/device-os/api/cloud-functions/particle-function/
// EXAMPLE USAGE
int brewCoffee(String command);
void setup()
{
// register the cloud function
Particle.function("brew", brewCoffee);
}
void loop()
{
// this loops forever
}
// this function automagically gets called upon a matching POST request
int brewCoffee(String command)
{
// look for the matching argument "coffee" <-- max of 64 characters long
if(command == "coffee")
{
// some example functions you might have
//activateWaterHeater();
//activateWaterPump();
return 1;
}
else return -1;
}
By exposing a Particle.variable() for your mode, you could easily track mode state as it changes according to your custom Particle.function().
added:
I changed the example a bit below to show usage of a Particle.variable() and customize it for your use case. Hope this helps:
// Particle.variable() & Particle.function()
int presentMode = 0; //default
int chooseMode(String command);
void setup()
{
// register the cloud variable
Particle.variable("present_mode", presentMode);
// register the cloud function
Particle.function("choose_mode", chooseMode);
}
void loop()
{
// this loops forever
}
// this function automagically gets called upon a matching POST request
int chooseMode(String command)
{
// look for the matching arguments "idle - warmup or measuring" <-- max of 64 characters long
if(command.equalsIgnoreCase("idle"))
{
presentMode = 0;
return 1;
} else if (command.equalsIgnoreCase("warmup"))
{
presentMode = 1;
return 1;
} else if (command.equalsIgnoreCase("measuring"))
{
presentMode = 2;
return 1;
}
else return -1;
}
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.