Pages: [1]   Go Down
Author Topic: Wifly UDP parsing  (Read 2105 times)
0 Members and 1 Guest are viewing this topic.
South Pasadena
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
« Last Edit: October 17, 2012, 07:59:02 pm by Briwil » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

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

South Pasadena
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something like this:

Code:
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++;
    }
}
Logged

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

South Pasadena
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: