Skip to content Skip to sidebar Skip to footer

Communication Between Arduino And Android Via Ethernet Shield

I'm using Android to toggle my led light with Arduino and ethernet shield. It didn't work despite having no problems during compilation neither on Android nor Arduino. I connected

Solution 1:

Program your Arduino:

#include"etherShield.h"#include"ETHER_28J60.h"int led2 = 7;
int led1 = 6;

staticuint8_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.staticuint8_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. staticuint16_t port = 80;                                    // Use port 80 - the standard for HTTP

ETHER_28J60 e;

voidsetup(){

    e.setup(mac, ip, port);

    pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);

    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
}

voidloop(){
    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();
    }
}

Program your APP

Add Permission to Your Manifest

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

MainActivity.java

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;

publicclassMainActivityextendsActivityimplementsOnClickListener{

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {

        StrictMode.ThreadPolicypolicy=newStrictMode.
        ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Viewled1on= findViewById(R.id.led_1on);
        Viewled1off= findViewById(R.id.led_1off);
        Viewled2on= findViewById(R.id.led_2on);
        Viewled2off= findViewById(R.id.led_2off);

        led1on.setOnClickListener(this);
        led1off.setOnClickListener(this);
        led2on.setOnClickListener(this);
        led2off.setOnClickListener(this);
    }

    publicvoidcommandArduino(String url){
        try {
            HttpClienthttpclient=newDefaultHttpClient();
            httpclient.execute(newHttpGet(url));      
        } catch (Exception e) {}
    }

    publicvoidonClick(View thisView) {
        switch(thisView.getId()){
            case R.id.led_1on:  commandArduino("http://192.168.0.15/?cmd=1"); break;
            case R.id.led_1off: commandArduino("http://192.168.0.15/?cmd=2"); break;
            case R.id.led_2on:  commandArduino("http://192.168.0.15/?cmd=3"); break;
            case R.id.led_2off: commandArduino("http://192.168.0.15/?cmd=4"); break;
        }
    }
}

Reference:

http://www.instructables.com/id/Arduino-Android-LED-control-Using-Ethernet-Shield/

Post a Comment for "Communication Between Arduino And Android Via Ethernet Shield"