Comments (1)
I'm not entirely clear what the issue is here.
myString is declared inside a handler, and is local to that handler - i.e. it isn't visible or useable to anything outside it.
If you declare it at the start of the sketch then you are making it a global variable, visible to everything, so you can't also declare the same variable in the handler -- If you want to use it outside the handler then comment out the declaration inside the handler otherwise the compiler sees two variable with the same name existing in the same 'name space' and it gets confused.
In the code you posted, the main loop repeatedly checks the variable myString, and it will contain whatever was in the last 'set strings' command.
If you want to access the payload that is in commander from the last command (not just the set strings command) then your code in loop should look like this:
String lastCommand=cmd.getPayloadString();
if(lastCommand=="stop"){
Serial.println("Succes string");
}
else if(lastCommand=="go") {
Serial.println("Succes go");
}
cmd.update();
But this isn't a good way to do things because if either of those strings are in the payload then it will print out the message over and over again as fast as it can until you send a command that doesn't have either of those words in the payload - Remember: the payload from the last command stays there in memory until it is overwritten when a new command arrives.
If your intention is to have an actual command called 'stop' and 'go' - so all you have to do is type stop or go - then make a new pair of handlers and commands. If you want to do something with the result in the main loop, use global flags to communicate the fact that a command has been handled:
First, add these lines to the command list:
{"stop", stopHandler, "Stop"},
{"go", goHandler, "Go"},
Next, add these two handlers:
bool stopHandler(Commander &Cmdr){
Cmdr.println("STOP Command");
stopCommand = true;
return 0;
}
bool goHandler(Commander &Cmdr){
Cmdr.println("GO Command");
goCommand = true;
return 0;
}
Now add these two lines above the setup() function (These are global variables so they are visible to everything in your whole sketch):
bool stopCommand = false;
bool goCommand = false;
Finally use this main loop (and put whatever else you need in there):
void loop() {
cmd.update();
if(stopCommand){
stopCommand = false; //reset the flag so this only happens once for every command event
Serial.println("Succes stop");
//do other things that should happen when the stop command comes in ...
}
if(goCommand){
goCommand = false; //reset the flag so this only happens once for every command event
Serial.println("Succes go");
//do other things that should happen when the go command comes in ...
}
}
Remember, you can also put all the things you want to happen on a stop or go command inside the handler itself, and also call other functions from within the handlers.
from commander.
Related Issues (11)
- typo in Commander/src/Commander.h => not compiling HOT 2
- [RFC] help command not executed HOT 10
- Commander seems to be missing a way to handle CR+LF as a valid line terminator HOT 1
- Arduino Library Manager latest release doesn't compile HOT 2
- endOfLineChar(CR) should also disable stripCR()? HOT 4
- Idea to change sketch structure to avoid Arduino-specific magic and externs HOT 25
- startStreaming() using Stream class HOT 14
- Compile Errors with current Arduino ESP8266 on PlatformIO HOT 9
- README copy/paste error
- Unable to use library with Putty. HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from commander.