64 bytes (not bits) and really it is 63 as the 64th must be a null termination
yup
first not everyone wants to discretely control pins from the internet ( I know I don’t) I use the strings to pass setting variables (offsets, thresholds, and the like). If you do what to discretely control pins from the internet, there are plenty of ways, just look around the libraries and tutorials and TRUST ME, you can control 16 i/o pins with 4, 64 bytes strings… actually I am pretty sure you control 16 i/o pins with one 2 byte string, but why would you when you have four 64 bytes strings! Great job spark core team!
I think so. I remember this being important, but I don’t remember why. I haven’t had my coffee yet.
M sorry.I thought I am writing 64 byte but ended up with 64 bit.
Anyways Thanks a lot for your valuable suggestions @GregF & @jerome .
@GregF
Thanks again but I actually couldn’t understand how to compress strings using bits. Could you please point me towards any reference or tutorials?
You could take a look at the Tinker firmware to get an idea of how you could control all pins using 4 functions. This is one of many ways this can be done, but it should get you started.
Assuming (hopefully) that you know about main(int argc, char * argv) where argc is the number of parameters and argv is a list of the parameters.
Where you can pass an arbitrary number of parameters in the code above:
./display this is a test
argc == 5
argv[0] == ./display
argv[1] == this
argv[2] == is
argv[3] == a
argv[4] == test
With the spark code you can only pass one parameter, the 64 character string.
By knowing in advance the types of data to expect, you can do multiple things by stepping through the string:
Byte 0 has 8 bits. these 8 bits can be broken down further:
bit 7 6 5 4 3 2 1 0
bit 0-3 (0-15) pin number
bit 4: (0) read pin (1) write pin
if read pin, read the byte and set the return value to the read value and skip to byte 1 if it exists, if not return
if write pin, write byte 1 to pin and skip to byte 2 if it exists, if not return
Note that with the above method only the last read pin value will be returned.
Here’s a code that i use for controlling a thermostat. It detects the first two letters and determines what functions to send the rest of the string to, so I have one function that can do a lot of different things.
int genericCommand(String command) {
char com = command.charAt(0);
switch (com) {
case 'S':
//schedule
if (command.charAt(1) == 'W') {
//write schedule
return writeSchedule(command.substring(2));
} else {
return readSchedule(command.substring(2));
}
break;
case 'P':
//PID
if (command.charAt(1) == 'W') {
return writePID(command.substring(2));
} else {
return readPID(command.substring(2));
}
break;
case 'T':
// Temp sensor serials
if (command.charAt(1) == 'W') {
//write serial
return writeTempSerial(command.substring(2));
} else {
return readTempSerial(command.substring(2));
}
break;
case 'C':
// find current temp setting
if (command.charAt(1) < 0 || command.charAt(1) >= NUM_OF_ZONES) return -1;
sprintf(outputString, "%0.4f", schedules[command.charAt(1)].findCurTempSetting());
return command.charAt(1);
break;
case 'R':
//Reset. Need "RR" so that one doesn't get accidentally called
if (command.charAt(1) == 'R') System.reset();
break;
}
return -20;
}
So, if you send PR1, it will read the PID values for zone 1, and print them to a String that is an exposed variable, which you can use to output data other than ints. Alternatively SW0.1:24|22;35|15 writes (W) the temperature schedule (S) for zone 0, on day 1 (Monday), setting it to 22C at time 2415 minutes (6:00am), and 15C at time 3515 (8:45 am) minutes. So, get creative!