I have a Firmata sketch, that accepts string messages, partitions them and sends its' parts back to a Firmata client program. My problem is certainly at the sketch's side. The problem is that Firmata.sendString(argument1) and Firmata.sendString(argument2) methods don't send anything. More information is found in the sketch's code. How can I get the sendString methods to work?
/*
* This sketch accepts strings in the following form:
* CMDn(arg1,arg2)
* CMDn — command name,
* arg1 и arg2 — unnecessary arguments.
* Each argument's length can be from 1 to 4 characters,
* They're saved to separate variables.
*
* This sketch sends back the parsed commands partially:
* the command name, argument #1 (if present), argument #2 (if present)
*/
#include <Firmata.h>
void stringCallback(char *received) {
//CMDn(arg1,arg2)
//CMDn(arg1)
//CMDn(1)
//CMDn()
//012345678901234
// 11111
//This scheme up there is just for
//understanding the indexes of the chars in the string
//in its various possible variations
//Let's declare all the necessary variables
char methodName[5];
char argument1[5];
char argument2[5];
byte commaLocation = 0;
//If the received string doesn't match the strings' syntax,
//send an error message
if (!(received[4] == '(' && strlen(received) <= 15)) {
Firmata.sendString("ERR;");
return;
}
//Let's take the 0-3 chars in the string and send it back.
//It's probably the command name
for (int i = 0; i < 4; i++) {
methodName[i] = received[i];
}
methodName[4] = '\0'; //does or doesn't the compiler add the terminator automatically?
Firmata.sendString(methodName); //send it
//This string is being sent without problems.
//If there is some arguments in the string,
//it is detectable if the string char 5 is not ')'/
if (received[5] != ')') {
//Lent's find out if the input string has a comma
//inside the parentheses. If it does, it means that
//there are two arguments in the string.
Firmata.sendString("46 passed");
for (int i = 6; i < (strlen(received) - 1); i++) {
//If there is a comma, save its' index and break the loop
if (received[i] == ',') {
commaLocation = i;
break;
}
}
//If a comma is not present, its location variable equals 0,
//sincerely we just collect all the chars between the parentheses
//to the 'argument1' variable and send it.
if (commaLocation == 0) {
Firmata.sendString("61 passed");
for (int i = 5; i < ; i++) {
argument1[(i - 5)] = received[i];
}
argument1[strlen(argument1)] = '\0'; //Either this is executed or not, the outcome doesn't change
Firmata.sendString(argument1); //This very command doesn't work. It just does not send anything.
//Looks like 'argument1' is empty, but I'm awfully sure that it must
//not be empty. Anyway, the 'argument1' does not show up in the client
}
//If there is a comma present, then write chars before the comma
//to 'argument1', and after the comma - to 'argument2'
if (commaLocation != 0) {
Firmata.sendString("77 passed");
//let's write what is before the comma:
for (int i = 5; i < commaLocation; i++) {
argument1[i - 5] = received[i];
Firmata.sendString("81 iterator");
}
//let's write what's after the comma;
char argument2[5];
for (int i = (commaLocation + 1); i < (strlen(received)); i++) {
argument2[(i - (commaLocation + 1))] = received[i];
Firmata.sendString("87 iterator");
}
argument1[strlen(argument1)] = '\0'; //Either this is executed or not, the outcome doesn't change
argument2[strlen(argument2)] = '\0';
Firmata.sendString(argument1); //Not working
Firmata.sendString(argument2); //Not working
}
}
}
void sysexCallback(byte command, byte argc, byte *argv) {
Firmata.sendSysex(command, argc, argv);
}
void setup() {
Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
Firmata.attach(STRING_DATA, stringCallback);
Firmata.attach(START_SYSEX, sysexCallback);
Firmata.begin(57600);
}
void loop() {
while (Firmata.available()) {
Firmata.processInput();
}
}
Expected output:
| What I send | What I receive (excluding the debug messages, ; stands for newline) |
|---|---|
DHTt() |
DHTt |
DHTt(1) |
DHTt; 1 |
DHTt(1,2) |
DHTt; 1; 2 |
Real output:
| What I send | What I receive (excluding the debug messages) |
|---|---|
DHTt() |
DHTt as long as there are no arguments, there are no problems. But the arguments are necessary for my task |
DHTt(1) |
DHTt the argument are not sent |
DHTt(1,2) |
DHTt two arguments are not sent neither |