Author Topic: UDP wifi connection with android smartphone problem sending data  (Read 5215 times)

matpuz81

  • Newbie
  • *
  • Posts: 3
Hi,
First of all, I hope that this question hasn't already been answered often, if so I'm sorry but I searched everywhere but haven't found a solution.

I have a problem with my DigiX when trying do send data via UDP to my Galaxy S3. In the other way it works perfectly.
Do i have to set a IP or does ot send a broadcast on the specified port?

Regards
-matpuz81

tastewar

  • Jr. Member
  • **
  • Posts: 50
Re: UDP wifi connection with android smartphone problem sending data
« Reply #1 on: May 10, 2014, 05:19:11 pm »
I haven't personally coded anything that uses UDP on the DigiX, but it's usually a good idea to post the code you're using, as it will help those who have some experience spot any potential issues.

matpuz81

  • Newbie
  • *
  • Posts: 3
Re: UDP wifi connection with android smartphone problem sending data
« Reply #2 on: May 11, 2014, 10:28:19 pm »
Here I got the decodeMsg function which decodes the recieved string.

Code: [Select]
void decodeMsg(String msg)  //example of msg: "[p:h:93]"
{
  if (msg.indexOf('[') > -1 && msg.indexOf(']') > -1)      // check if message is complete, including start and end delimiter
  {
    hasConnection = true; //used for status led

    msg = msg.substring(msg.indexOf('[') + 1, msg.indexOf(']'));     //cut the delimiters

    if (msg[0] == 'p') //p -> percent      percentage is sent
    {
      msg = msg.substring(2) + '\0';

      char type = msg[0];

      msg = msg.substring(2) + '\0';
      int lenght = msg.length();
      char strVal[lenght];
      msg.toCharArray(strVal, lenght);
     
      if (type == 'h' && motorH.isDrivingToPos() == false) //h -> horizontal      percentage for horizontal motor
        motorH.setPercent(atoi(strVal));        //send converted int percentage value to motor thread
      else if (type == 'v' && motorV.isDrivingToPos() == false) //v -> vertical    percentage for vertical motor
        motorV.setPercent(atoi(strVal));        //send converted int percentage value to motor thread


    }
   
    if (msg[0] == 'r') //r -> raw percent      raw percentage is sent
    {
      msg = msg.substring(2) + '\0';

      char type = msg[0];

      msg = msg.substring(2) + '\0';
      int lenght = msg.length();
      char strVal[lenght];
      msg.toCharArray(strVal, lenght);

      if (type == 'h') //h -> horizontal      percentage for horizontal motor
        motorH.setRawPercent(atoi(strVal));        //send converted int percentage value to motor thread
      else if (type == 'v') //v -> vertical    percentage for vertical motor
        motorV.setRawPercent(atoi(strVal));        //send converted int percentage value to motor thread


    }

    else if (msg[0] == 'm') //m -> message is send (information exchange)
    {
      msg = msg.substring(2) + '\0';

      //Here I would have the Udp send function to respond
      //something like Serial1.write("test");
     
    }

    ticksSinceLastMsg = 0;      //set tickssincelastmsg to 0, so that it is known that a msg has just been recieved
  }

  else return;
}

I hope this helps and that it is somehow useful  :D

tastewar

  • Jr. Member
  • **
  • Posts: 50
Re: UDP wifi connection with android smartphone problem sending data
« Reply #3 on: May 12, 2014, 10:45:53 am »
I'd start out, if possible, by printing *all* the raw received data, ASAP after it's received, to the serial console on  your computer so that you are certain of what you are receiving.

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: UDP wifi connection with android smartphone problem sending data
« Reply #4 on: May 12, 2014, 09:39:58 pm »
Check out the NTP example - that is the best working example of UDP - and to answer the direct question - it sends a broadcast to the port

matpuz81

  • Newbie
  • *
  • Posts: 3
Re: UDP wifi connection with android smartphone problem sending data
« Reply #5 on: May 21, 2014, 11:02:22 am »
Thanks for your help! :) The solution was pretty simple in the end, for some reason i thought that Udp-Server on the WiFi config page meant the IP of the DigiX but it is the IP where it sends the broadcast to (obviously...).

Thanks again for the help :D