Escolar Documentos
Profissional Documentos
Cultura Documentos
ACEduinoEthernetShield.rar
// 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};
home hub
}
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();
}
}
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>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:ignore="HardcodedText" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/labelnim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:text="A11.2011.05909"
android:layout_centerHorizontal="true"
android:textSize="18sp" />
<TextView
android:id="@+id/labelnama"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/labelnim"
android:layout_marginTop="7dp"
android:text="Nama :"
android:textSize="18sp" />
<EditText
android:id="@+id/fieldnama"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fieldemail"
android:layout_below="@+id/labelnim"
android:inputType="text" >
</EditText>
<TextView
android:id="@+id/labelemail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/labelnama"
android:layout_marginTop="25dp"
android:text="Email :"
android:textSize="18sp" />
<EditText
android:id="@+id/fieldemail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/fieldnama"
android:layout_toRightOf="@+id/labelhobi"
android:inputType="textEmailAddress" >
</EditText>
<TextView
android:id="@+id/labeljk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/labelemail"
android:layout_marginTop="20dp"
android:text="Jenis Kelamin :"
android:textSize="18sp" />
<RadioGroup
android:id="@+id/groupjk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/labeljk"
android:orientation="horizontal"
android:padding="5dp" >
<RadioButton
android:id="@+id/radiolk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Laki-Laki"
android:textSize="18sp" />
<RadioButton
android:id="@+id/radiopr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Perempuan"
android:textSize="18sp" />
</RadioGroup>
<TextView
android:id="@+id/labelhobi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/groupjk"
android:layout_marginTop="7dp"
android:text="Hobi :"
android:textSize="18sp" />
<CheckBox
android:id="@+id/cekboxgam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/labelhobi"
android:layout_marginTop="5dp"
android:text="Gamming"
android:textSize="18sp" />
<CheckBox
android:id="@+id/cekboxmov"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cekboxgam"
android:layout_marginTop="5dp"
android:text="Movies"
android:textSize="18sp" />
<CheckBox
android:id="@+id/cekboxspo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cekboxmov"
android:layout_marginTop="5dp"
android:text="Sports"
android:textSize="18sp" />
<Button
android:id="@+id/tomboltampil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cekboxspo"
android:layout_marginTop="5dp"
android:text="CETAK"
android:textSize="18sp" />
<TextView
android:id="@+id/outputnama"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tomboltampil"
android:layout_marginTop="5dp"
android:textSize="18sp" />
<TextView
android:id="@+id/outputemail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/outputnama"
android:layout_marginTop="5dp"
android:textSize="18sp" />
<TextView
android:id="@+id/outputhobi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/outputemail"
android:layout_marginTop="5dp"
android:textSize="18sp" />
<TextView
android:id="@+id/outputjk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/outputhobi"
android:layout_marginTop="5dp"
android:textSize="18sp" />
</RelativeLayout>
</ScrollView>
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class InputbiodataActivity
OnCheckedChangeListener {
extends
Activity
EditText fieldnama,fieldemail;
Button tomboltampil;
RadioGroup groupjk;
RadioButton radiolk, radiopr;
CheckBox cekboxgam, cekboxspo, cekboxmov;
TextView outputnama, outputemail, outputjk, outputhobi;
String jk, hobi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fieldemail = (EditText)findViewById(R.id.fieldemail);
fieldnama = (EditText)findViewById(R.id.fieldnama);
tomboltampil = (Button)findViewById(R.id.tomboltampil);
groupjk = (RadioGroup)findViewById(R.id.groupjk);
cekboxgam = (CheckBox)findViewById(R.id.cekboxgam);
cekboxspo = (CheckBox)findViewById(R.id.cekboxspo);
cekboxmov = (CheckBox)findViewById(R.id.cekboxmov);
outputnama =(TextView)findViewById(R.id.outputnama);
outputemail =(TextView)findViewById(R.id.outputemail);
outputhobi = (TextView)findViewById(R.id.outputhobi);
outputjk = (TextView)findViewById(R.id.outputjk);
tomboltampil.setOnClickListener(this);
groupjk.setOnCheckedChangeListener(this);
}
@Override
public void onClick(View klik) {
// TODO Auto-generated method stub
outputnama.setText("Nama Anda : "+fieldnama.getText());
outputemail.setText("Email Anda : "+fieldemail.getText());
implements
OnClickListener,
6. Jika sudah, save semua file lalu lanjutkan dengan Running project seperti gambar berikut