Go Down

Topic: Wifly UDP parsing (Read 2417 times) previous topic - next topic

Briwil

Oct 18, 2012, 12:42 am Last Edit: Oct 18, 2012, 02:59 am by Briwil Reason: 1
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.

Code: [Select]
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?

johnwasser


Code: [Select]
void loop(){
   char d=wifly.read();
   char parameter = strtok (d, " ,");

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.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Briwil

Quote
The "strtok" function works on a string of characters, not on a single character.

Well I guess I'm confused, because I thought my 'char d' was a string of Characters, the r128 g004 b023 that I gave in the example.

The iPhone app could include a closing character, but Im not sure how I would put it in an array like you say. Could you show me a code example?

johnwasser

Something like this:

Code: [Select]

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++;
    }
}

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Briwil

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
Code: [Select]
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++;
    }
}

johnwasser

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).
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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.

Thank you,
Butcher

Go Up