Arduino Android bidirectional communication using the Ethernetshield

Bidirectional TCP Communication between Android Smartphone and Arduino using the Ethernetshield

After my first connection test between an Android Phone and an Arduino using the Ethernetshield, I spend a little more time on the communication to make it bidirectional. In Java this would normally be done by using an endless thread. But on Android there is a more elegant way by using Tasks.

Have a look at the result:

 

AppLayout

The UI is similar to my previous app:

 

 

using tasks on the Androidplattform

The Socket connection is now handled in a Task. In Java this would normally be done using Therads, but on Android devices tasks are much more common, because they can run in background.
A Task is structured like this:

  • onPreExecute is called before the task is started
  • doInBackground is the thread which can run “endlessly” in the background
  • onProgressUpdate can be called from the task to update the UI or values
  • onCancelled is called when the task is cancelled
  • onPostExecute is called after the task has executed

In my case the class looks like this:

The doInBackground Method opens a socket and keeps the connection alive. When a new String is recieved the onProgressUpdate Method is called to update the UI.
The Method sendDataToNetwork can be called from my main class to send a String over the socket connection.

The full sourcecode of the Android app is this:

the Arduino Server

The Arduino Server is quite similar to the one I used before. I Added a Method to send Strings over the ethernet.

The while loop runs as long as a client is connected. The Poti is read and the Value is braodcasted to all connected Clients. At the moment the Ethernetlibrary can only handle one Client connection at  time (it shloud be four, but at the moment only one is working correctly). Please keep that in mind.

I’m using a very cheap poti, with the result that my curPotiValue is allways jumping between values (for example:1021..1022…1021…1020…). In addition to this you don’t want to send data over the socket when nothing has changed (Eventlistening). That’s why I added a little script to solve that.

I’m storing the old value in a variable and compare it to the new one. If the difference is bigger than two, the new value is transmitted.

The whole sourcecode of the server now looks like this:

Downloads

Download the files below, set the ipadress and port you want to use (on the client- and serverside) and have fun playing with it.

 

 

 

8 thoughts on “Bidirectional TCP Communication between Android Smartphone and Arduino using the Ethernetshield

  1. miguel

    Hello
    I found your Project very interesting.
    I test your project and all the time I get the “SendDataToNetwork: Message send failed. Caught an exception” message, the only way I could send data was in the doInBackground function, in which I created the socket.
    Finally I continued to try the connection like your project, and recently made it with operative system Android 1.6.
    It’s a shame that this method can’t be used in superior versions of android, because of the need of threads and also that you need to send the message in the same function you create the socket connection.
    I was wondering if you know a better way to send and receive information, in superior versions of android operative system. Because if you do it like I did it, you can’t get information from the server, because I already used the doInBackground function to send data.
    Thank you for your time and sorry for my English.

    Reply
    1. Laurid Post author

      sorry, I don’t have any experiences with Android 1.6. At the moment I don’t have an idea. Good Luck anyway!

      Reply
      1. miguel

        Mm ok, I believe it’s the minSdkVersion, because on android 2.3 version by default set you in 10, and in your app its set as 8, so I changed it and start to work.
        In conclusion I believe that this method can work if the minSdkVersion its equals or lower than 8, but can’t work if its higher. Thanks for the help

        Reply
  2. Szilamer

    Hi,
    i’m trying to use your coding as a source for my project. What i need from it is the bidirectional communication. It works just fine on local network (Arduino and Android connected to the same router), but what i need is to send and recieve data to/from the Android device connected to the internet over the provider’s internet connection(3G). In this case it will not connect to the Arduino.
    I suppose the IP address is the problem…
    Do you have any idea how to fix this?
    Thanks a lot!

    Reply
  3. Sohib

    Hi. I like this project and I’m working on something like it .. but,I’m using Wi-Fi proto with Arduino and I wanna control some lights via my android app ..
    any help with the code that can connect the android app with the Wi-Fi over a wireless network (Router) .. and how to send the command to the Arduino ?
    :)

    Reply
  4. nat

    hi. I’m after a modified version of this project. As I’m really stuck would you please help to modify code so that when you press ‘connect’ the position of the potentiometer appears.

    any feedback is really appreciated.

    nat

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *


+ five = 12

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">