Can someone help me figure out what I'm doing wrong? I'm receiving a UDP message from my iphone, but I can't figure out how to parse it correctly.
The message being received is something like 'r128 g004 b023' (a three digit value for each of the three rgb colors).
Like I said, everything is working for the receiving part, but once I get it, I'm stuck.
void loop(){
while (wifly.available() > 0) {
char d=wifly.read();
char parameter = strtok (d, " ,");
//parameter = strtok (NULL, " ,");
while (*parameter != NULL) {
if (parameter[0] == "r") {
int value = atoi(parameter+1);
analogWrite(9, value);
}
else if (parameter[0] == "g")) {
int value = atoi(parameter+1);
analogWrite(10, value);
}
else if (parameter[0] == "b")) {
int value = atoi(parameter+1);
analogWrite(11, value);
}
}
}
}
But I'm getting "invalid conversion from 'char' to 'char*'" errors.
Could anyone help point me in the right direction for how to correctly parse this?
But I'm getting "invalid conversion from 'char' to 'char*'" errors.
The "strtok" function works on a string of characters, not on a single character.
Does your phone send a character at the end of the three value to let the Arduino know that all characters have been received? If so, you can put the characters in a character array and use that array as a string once all the characters have been received.
Alternatively you can parse each character as it arrives (but not with strtok). When you get a letter (r, g, or b) you keep track of which letter was last received and set the value for that color to 0. As each digit arrives you multiply the value for the last color letter received by 10 and add the new digit. If you receive any other character you can ignore it.
void loop() {
static char buffer[30];
static int bufferIndex = 0;
while (wifly.available()) {
buffer[bufferIndex++] = wifly.read();
if (buffer[bufferIndex-1] == '\n') {
// End Of Line character received
buffer[bufferIndex++] = '\0'; // Add a terminating null character
processString(buffer); // You write this function to process the message
bufferIndex = 0;
}
if (bufferIndex > 29) // Buffer Overflow
bufferIndex = 0;
}
void processString(char *parameter){
while (*parameter != NULL) {
if (parameter[0] == "r") {
int value = atoi(parameter+1);
analogWrite(9, value);
}
else if (parameter[0] == "g")) {
int value = atoi(parameter+1);
analogWrite(10, value);
}
else if (parameter[0] == "b")) {
int value = atoi(parameter+1);
analogWrite(11, value);
}
parameter++;
}
}
Thanks for writing, but this isn't working either, I still get the error "ISO C++ forbids comparison between pointer and integer"
which is coming from
void processString(char *parameter){
while (*parameter != NULL) {
if (parameter[0] == "r") {
int value = atoi(parameter+1);
analogWrite(9, value);
}
else if (parameter[0] == "g")) {
int value = atoi(parameter+1);
analogWrite(10, value);
}
else if (parameter[0] == "b")) {
int value = atoi(parameter+1);
analogWrite(11, value);
}
parameter++;
}
}
That's because you are using string constants "r", "g" and "b" when you want character constants 'r', 'g', and 'b'. Since parameter[0] is a character you can't compare it to a string (character pointer).
Hello Briwil, could I ask you the whole source code or at least the setup part of this program? Because I can't figure out how to setup my arduino with wifly to receive udp packets.