Go Down

Topic: Troubles comparing UDP socket received and array (Read 360 times) previous topic - next topic

ibujin5

Feb 06, 2013, 12:19 pm Last Edit: Feb 06, 2013, 12:24 pm by ibujin5 Reason: 1
Hello. I'm new here and in arduino. Hope you can help me. I'm developing an alarm program in arduino UNO, and an app for iphone to send the password by wifi (socket_datagram, UDP) and compare it with an array called: char password[4]={1,2,3,4} in the arduino. The problem is that when the arduino receives the udpSocket is properly showed in the screen, but when i compare it with the password it does anything, like it was wrong, or if change a bit the comparing method trying an other way, it works allways although the password received is wrong. I don't know how to solve the comparing method. Can someone help me? The code used in the receiving function is:

char password[4]={1,2,3,4}; //password going to be compared
char activ[2]={9,9};  // code to activate alarm

void loop()
{
...
udpReceive(); // call the function to receive udp sockets
...
}


void udpReceive()
{
 // if there's data available, read a packet
 int packetSize = Udp.parsePacket();
 if(packetSize)
 {
   Serial.print("Received packet of size ");
   Serial.println(packetSize);
   Serial.print("From ");
   IPAddress remote = Udp.remoteIP();
   for (int i =0; i < 4; i++)
   {
     Serial.print(remote, DEC);
     if (i < 3)
     {
       Serial.print(".");
     }
   }
   Serial.print(", port ");
   Serial.println(Udp.remotePort());

   // read the packet into packetBufffer
   Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
   Serial.println("Contents:");
   Serial.println(packetBuffer);
   
   for(int i=0;i<UDP_TX_PACKET_MAX_SIZE;i++)  //comparing
      {
        if(packetBuffer==password
        {
          x=0;
          code=1;  //password correct to switch off the alarm in loop() functio
          break;
        }
        if(packetBuffer==activ)
        {
          code=0;
          x=1;   // just to activate the alarm in loop() function
          break;
        }
      }
   delay(10);
 }
}

ibujin5

PD.: in the comparing method, in the function if() the arrays are properly typed but the forum text editor erases the 'i' between brackets. password, active and packetBuffer

PaulS

Quote
PD.: in the comparing method, in the function if() the arrays are properly typed but the forum text editor erases the 'i' between brackets. password, active and packetBuffer

Because you failed to post the code correctly. Modify your post. Select the code. Select the icon with the # sign. Save the changes.

Why are you breaking out of the compare loop as soon as a match is found? You should assume that the passwords match, and set a flag to false and break out when a mis-match is found, not a match.

1,2,3,5 does not match 1,2,3,4, but you never compare 2 to 2, 3 to 3, or 5, to 4, because 1 matched 1.

char arrays are to hold characters. 1, 2, 3, and 4 are numbers, not characters. uint8_t would be a better choice for the array type(s). Or, perhaps what is really needed is '1', '2', '3', and '4' in the array.


ibujin5

Thank you very much. I'm going to try both ways you have said. About breaking it, i don't know why. Just tryed with 'break;' and without it. I'll tell you if it works. Thanks !!

Go Up