I'm trying to use CmdMessenger to transfer data over the serial bus between 2 Arduino's, one of which is a Energy Monitor, the other is a ethernet enabled board for uploading data to Pachube. The data will flow just one way, from the Energy Monitor to the Ethernet board, and will take to form of 4,3212,4334,1212,1248; (the leading '4' is a command informing that data is forthcoming. The ',' are data separators, and the ';' is a terminator.
The sketch is here CmdMessenger2/CmdMessengerExample.pde at master · dreamcat4/CmdMessenger2 · GitHub and the processing section is highlighted.
I can get the sketch to run, but what I want to do is to attach the received values to variables in the Ethernet board, so mainPower has the value 3212, solarPower has the value of 4334, etc. but so have not managed to do so. Any help would be appreciated.
If it helps, the sketch's author has said 'The key to understanding this is to see that the function call cmdMessenger.available() points to the next field in the message. And this will loop around in the while() loop for each successive field. As delimited by your chosen field separator character (and ASCII comma ',' by default).'
I'm not sure if theres a better way but I would always just use the indexOf function and a few other String functions to parse the data and I usually just put it into an array not a bunch of independent variables. This will work fine as long as you send the same amount of data. Check out the String reference page. Once you have the various indexes of your reference points (i.e. 4 , and then you can parse it pretty easy by using the between indexes and assigning them to a variable or array. I hope this makes sense
It looks like cmdMessenger.available() is designed to step through the comma-separated payload contents using strtok(). Although it does use an uninitialized local 'temppointer' the second time through. So what you're going to do is to take each one in turn, convert it to a number, and then put it where you want it.
Based on assumptions, here's one potential solution:
void bens_msg()
{
int seq;
long value;
// Message data is any ASCII bytes (0-255 value). But can't contain the field
// separator, command separator chars you decide (eg ',' and ';')
cmdMessenger.sendCmd(kACK,"bens cmd recieved");
for (seq = 0; cmdMessenger.available(); seq ++ )
{
char buf[64] = { '\0' };
cmdMessenger.copyString(buf, 350);
if(buf[0])
cmdMessenger.sendCmd(kACK, buf);
value = strtol(buf, NULL, 10);
switch (seq) {
case 1: // the first value is the foobar variable
foobar = value;
break
:
case 3: // the third value is the frobnitz variable
frobnits = value;
break
}
}
}
gardner:
It looks like cmdMessenger.available() is designed to step through the comma-separated payload contents using strtok(). Although it does use an uninitialized local 'temppointer' the second time through. So what you're going to do is to take each one in turn, convert it to a number, and then put it where you want it.
Based on assumptions, here's one potential solution:
void bens_msg()
{
int seq;
long value;
// Message data is any ASCII bytes (0-255 value). But can't contain the field
// separator, command separator chars you decide (eg ',' and ';')
cmdMessenger.sendCmd(kACK,"bens cmd recieved");
hey, hope you'll get this / it'll be helpful to someone:
void bens_msg()
{
int seq;
long value;
// Message data is any ASCII bytes (0-255 value). But can't contain the field
// separator, command separator chars you decide (eg ',' and ';')
cmdMessenger.sendCmd(kACK,"bens cmd recieved");
for (seq = 0; cmdMessenger.available(); seq ++ )
{
char buf[64] = { '\0' };
cmdMessenger.copyString(buf, 350);
if(buf[0])
cmdMessenger.sendCmd(kACK, buf);
value = strtol(buf, NULL, 10);
switch (seq) {
case 0:
red = value;
break;
case 1:
green = value;
break;
case 2:
blue = value;
break;
}
}
}