Introduction: Arduino-Android LED Control Using Ethernet Shield

Things you need:

hardware :

  Arduino Uno
  Ethernet Shield
  LED 2 pcs.
  Resistors 2pcs.
  Breadboard(optional)
  wires(copper,ethernet cable)
  Router
  
connect 

  LED 1 --> pin 6 to ground
  LED 2 --> pin 7 to ground
software:
 
  Eclipse IDE
  Arduino IDE 1.x.x




Step 1: Program Your Arduino

// A simple web server that always just says "Hello World"

#include "etherShield.h"
#include "ETHER_28J60.h"

int led2 = 7;
int led1 = 6;


static uint8_t mac[6] = {0xAA, 0xBB, 0xCC, 0xDD, 0xBB, 0xAA};   // this just needs to be unique for your network,
                                                                // so unless you have more than one of these boards
                                                                // connected, you should be fine with this value.
                                                          
static uint8_t ip[4] = {192, 168, 0, 15};                       // the IP address for your board. Check your home hub
                                                                // to find an IP address not in use and pick that
                                                                // this or 10.0.0.15 are likely formats for an address
                                                                // that will work.
static uint16_t port = 80;                                      // Use port 80 - the standard for HTTP

ETHER_28J60 e;

void setup()
{
    e.setup(mac, ip, port);
   
    pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);
   
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);

 
}

void loop()
{
  char* params;
  if (params = e.serviceRequest())
  {
    if (strcmp(params, "?cmd=1") == 0)
    {
          digitalWrite(led1, HIGH);
    }
    if (strcmp(params, "?cmd=2") == 0)
    {
          digitalWrite(led1, LOW);
         
    }
    if (strcmp(params, "?cmd=3") == 0)
    {
          digitalWrite(led2, HIGH);
    }
    if (strcmp(params, "?cmd=4") == 0)
    {
          digitalWrite(led2, LOW);
         
    }
   
    e.respond();
  }
}

Step 2: Your App (main Activity)

package com.androidarduino;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
 
  StrictMode.ThreadPolicy policy = new StrictMode.
  ThreadPolicy.Builder().permitAll().build();
  StrictMode.setThreadPolicy(policy);

  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 
  View led1on = findViewById(R.id.led_1on);
  View led1off = findViewById(R.id.led_1off);
  View led2on = findViewById(R.id.led_2on);
  View led2off = findViewById(R.id.led_2off);
 
  led1on.setOnClickListener(this);
  led1off.setOnClickListener(this);
  led2on.setOnClickListener(this);
  led2off.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
}

public void commandArduino(String url){
   try {
      HttpClient httpclient = new DefaultHttpClient();
      httpclient.execute(new HttpGet(url));      
    } catch (Exception e) {
    }
}

public void onClick(View thisView) {
  switch(thisView.getId()){
  case R.id.led_1on: 
   commandArduino("http://192.168.0.15/?cmd=1");
   Toast.makeText(getApplicationContext(), "led_1on",Toast.LENGTH_LONG).show();
   break;
  case R.id.led_1off: 
   commandArduino("http://192.168.0.15/?cmd=2");
   Toast.makeText(getApplicationContext(), "led_1off",Toast.LENGTH_LONG).show();
   break;
  case R.id.led_2on: 
   commandArduino("http://192.168.0.15/?cmd=3");
   Toast.makeText(getApplicationContext(), "led_2on",Toast.LENGTH_LONG).show();
   break;
  case R.id.led_2off: 
   commandArduino("http://192.168.0.15/?cmd=4");
   Toast.makeText(getApplicationContext(), "led_2off",Toast.LENGTH_LONG).show();
   break;

  }
 
}

}

Step 3: Write Your Xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Arduino Simple Automation"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/led_1on"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_weight="0.39"
            android:text="LED 1 ON" />

        <Button
            android:id="@+id/led_1off"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_weight="0.39"
            android:text="LED 1 OFF" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/linearLayout1"
        android:layout_alignRight="@+id/linearLayout1"
        android:layout_below="@+id/linearLayout1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/led_2on"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_weight="0.39"
            android:text="LED 2 ON" />

        <Button
            android:id="@+id/led_2off"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_weight="0.39"
            android:text="LED 2 OFF" />
    </LinearLayout>

</RelativeLayout>

Step 4: Add Permission to Your Manifest

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />



your android manifest should look like these:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidarduino"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.androidarduino.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Comments

author
PriyanS1 (author)2016-08-15

why gateway not one network? what this coding success?

author
VigneshramT (author)2016-01-19

Hi,

I have implemented the project by following the steps given by you. I have downloaded both the ethershield and ETHER_28J60 libraries.

After loading your code I got the following error while compilation. Kindly help me to get out of this.

C:\Users\jjj\Desktop\arduino-1.6.6\libraries\ETHER_28J60/ETHER_28J60.h:h:38:15:error: 'prog_char' has not been declared

void print_P(prog_char* text)

exit status 1

Error compiling.

Thanks and Regards

Vignesh

author
DexterE4 (author)2016-01-09

Hello. We got the same components! thank you for this info. I can use this as an example. But can i ask you favor? Please help me edit the LED1,2,3,4 and change it into servo motor with one output only? I badly needed it. Please email me here alacse07@gmail.com thanks a lot bro.

author
DexterE4 (author)DexterE42016-01-10

Sir please reply. :(

author
amr.elgendy.338 (author)2014-12-28

what is the meaning of ( 'SPI' was not declared in this scope )?

aa.png
author
uzidon (author)2014-06-17

Could you tell me if it is possible to pursue the same project using official Arduino WiFi Shield instead of the Ethernet shield? What would need to be changed and I'd be grateful if you could point me in the direction of some related resources if it is possible.

author
lascencio (author)2014-03-24

hello, I gave a studied in your code, but I do not The use ethernet module 28J60, I use ethernet W5100 (Wiz), could you help me?

ola, eu dei uma estudada em seu codigo, mas eu nao utilizao o modulo de ethernet 28J60, eu utilizo o ethernet W5100 (Wiz), voce poderia me ajudar?