Search code examples
androidtcparduinowireless

How to control an Arduino and WiFly shield with an Android device


I'm trying to control an Arduino Uno R3 board + WiFly RN-131C shield with the use of my Android phone (that is, something similar to what is shown here, although I think in that case the Arduino connects to the network through an Ethernet shield).

The Arduino can connect to my local network through the WiFly shield and the Android phone is also connected to the same network.

I have been trying to look for some tutorial that does something similar (unfortunately the link for the tutorial given in the video above is broken), and I have found some, but I am having trouble with the step on how to send the commands from the Android phone to the Arduino. Many tutorials show how an Android phone and Arduino can communicate via Bluetooth or using two Android devices, but I'm looking for communication over the Wi-Fi local network.

For example, I found this nice instructable on a 4x4 Remote Control car, and the guy was kind enough to provide the source code for his Android application. However, I do not see in the code the place where the actual communication takes place (that is, where commands are sent).

From what I have read, I have to create a socket over a TCP connection, but I have no idea of how that is done in Android... :-/ I am new to Arduino, but I have some experience with Android programming.. except for working with web sockets and TCP!

What is a concrete sample code on how an Android phone can communicate through TCP with an Arduino?

PS: Another nice tutorial that does something similar can be found in Experimenting with Android and Arduino, but I don't know Python and cannot completely follow the instructions :(


Solution

  • I did something similar with UDP. TCP would require a slightly different implementation, but hopefully this provides a good shove in the right direction.

    Take a look at my Android code Here

    UdpClient.java:

    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    
    import android.util.Log;
    
    public class UdpClient {
             String messageStr;
             DatagramSocket s;;
             int server_port;
             InetAddress local;
             int msg_length;
             byte[] message;
    
            public UdpClient (String ipAddress, int port){
                      server_port = port;
                      try {
                            local = InetAddress.getByName(ipAddress);
                            s = new DatagramSocket();
                    } catch (Exception e) {
                            e.printStackTrace();
                            Log.d("Powerwheelino",e.getStackTrace() + "error");
    //                      DatagramPacket rPacket = new DatagramPacket()
    //                      s.receive(rPacket);
    
                    }               
            }
    
            public boolean sendData(byte drive, byte steering){
                    byte drvByte = (byte) (drive & 0xFF);
                    byte steerByte = (byte) (steering & 0xFF);
                    message = new byte[2];
                    message[0] = drvByte;
                    message[1] = steerByte;
                    msg_length = message.length;
                    //message = messageStr.getBytes();
                    try {   
                         DatagramPacket p = new DatagramPacket(message, msg_length,local,server_port);
                         s.send(p);
                    } catch (Exception e) {
                            Log.d("Powerwheelino", e.getStackTrace() +"ERROR ");
                            e.printStackTrace();
                            return false;
                    }
                    return true;
            }
    
            public boolean isConnected (){
                    return s.isConnected();
            }
    
    }