目次:
必要なもの
ハードウェア
Arduino Uno
イーサネットシールド
LED 2個
抵抗器2個
ブレッドボード(オプション)
ワイヤー(銅、イーサネットケーブル)
ルーター
接続する
LED 1 - > 6番ピンからアース
LED 2 - > 7番ピンからアース
ソフトウェア:
Eclipse IDE
Arduino IDE 1.x.x
用品:
ステップ1:Arduinoをプログラムする
//常に "Hello World"と表示される単純なWebサーバー
#include "etherShield.h"
#include "ETHER_28J60.h"
int led2 = 7。
int led1 = 6。
静的uint8_t mac 6 = {0xAA、0xBB、0xCC、0xDD、0xBB、0xAA}。 //これはあなたのネットワークに固有のものである必要があります。
//これらのボードが複数ある場合を除き
//接続したら、この値で問題ないはずです。
静的uint8_t ip 4 = {192、168、0、15}。 //ボードのIPアドレスあなたのホームハブをチェック
//使用されていないIPアドレスを見つけて選択する
// thisまたは10.0.0.15はおそらく住所の形式です
//それはうまくいくでしょう。
静的uint16_t port = 80。 //ポート80を使用 -
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();
}
}
ステップ2:あなたのアプリ(主な活動)
パッケージcom.androidarduino;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
android.app.Activityをインポートします。
android.os.Bundleをインポートします。
android.os.StrictModeをインポートします。
import android.view.Menu;
android.view.Viewをインポートします。
import android.view.View.OnClickListener;
android.widget.Toastをインポートします。
パブリッククラスMainActivityはアクティビティを実装していますOnClickListener {
@オーバーライド
protected void onCreate(Bundle savedInstanceState){
StrictMode.ThreadPolicy policy = new StrictMode
ThreadPolicy.Builder()。permitAll()。build();
StrictMode.setThreadPolicy(ポリシー);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View led1on = findViewById(R.id.led_1on);
ビューled1off = findViewById(R.id.led_1off);
View 2on = findViewById(R.id.led_2on);
ビューled2off = findViewById(R.id.led_2off);
led1on.setOnClickListener(this);
led1off.setOnClickListener(this);
led2on.setOnClickListener(this);
led2off.setOnClickListener(this);
}
@オーバーライド
public boolean onCreateOptionsMenu(メニューメニュー){
//メニューを膨らませる。アクションバーがある場合は、これによってアクションバーに項目が追加されます。
getMenuInflater()。inflate(R.menu.main、メニュー);
trueを返します。
}
public void commandArduino(String url){
試してください{
HttpClient httpclient = new DefaultHttpClient();
catch(例外e){
}
}
public void onClick(このビューを表示){
switch(thisView.getId()){
ケースR.id.led_1on:
commandArduino( "http://192.168.0.15/?cmd=1");
Toast.makeText(getApplicationContext()、 "led_1on"、Toast.LENGTH_LONG).show();
ブレーク;
ケースR.id.led_1off:
commandArduino( "http://192.168.0.15/?cmd=2");
Toast.makeText(getApplicationContext()、 "led_1off"、Toast.LENGTH_LONG).show();
ブレーク;
ケースR.id.led_2on:
commandArduino( "http://192.168.0.15/?cmd=3");
Toast.makeText(getApplicationContext()、 "led_2on"、Toast.LENGTH_LONG).show();
ブレーク;
ケースR.id.led_2off:
commandArduino( "http://192.168.0.15/?cmd=4");
Toast.makeText(getApplicationContext()、 "led_2off"、Toast.LENGTH_LONG).show();
ブレーク;
}
}
}
ステップ3:あなたのXMLを書く
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
アンドロイド:layout_height = "match_parent"
アンドロイド:paddingBottom = "@ dimen / activity_vertical_margin"
アンドロイド:paddingLeft = "@ dimen / activity_horizontal_margin"
アンドロイド:paddingRight = "@ dimen / activity_horizontal_margin"
アンドロイド:paddingTop = "@ dimen / activity_vertical_margin"
tools:context = "。MainActivity">
<>
android:id = "@ + id / textView1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
アンドロイド:layout_gravity = "center"
android:text = "Arduino Simple Automation"
android:textAppearance = "?android:attr / textAppearanceLarge" />
<>
アンドロイド:ID = "@ + ID / linearLayout1"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_alignLeft = "@ + id / textView1"
android:layout_below = "@ + id / textView1"
アンドロイド:向き= "水平">
<>
アンドロイド:ID = "@ + ID / led_1on"
アンドロイド:layout_width = "100dp"
アンドロイド:layout_height = "100dp"
アンドロイド:layout_weight = "0.39"
アンドロイド:テキスト= "LED 1が点灯" />
<>
android:id = "@ + id / led_1off"
アンドロイド:layout_width = "100dp"
アンドロイド:layout_height = "100dp"
アンドロイド:layout_weight = "0.39"
アンドロイド:テキスト= "LED 1 OFF" />
<>
アンドロイド: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"
アンドロイド:向き= "水平">
<>
アンドロイド:id = "@ + id / led_2on"
アンドロイド:layout_width = "100dp"
アンドロイド:layout_height = "100dp"
アンドロイド:layout_weight = "0.39"
アンドロイド:テキスト= "LED 2が点灯" />
<>
アンドロイド:ID = "@ + ID / led_2off"
アンドロイド:layout_width = "100dp"
アンドロイド:layout_height = "100dp"
アンドロイド:layout_weight = "0.39"
アンドロイド:テキスト= "LED 2 OFF" />
ステップ4:マニフェストに権限を追加する
あなたのAndroidのマニフェストは、次のようになります。
package = "com.androidarduino"
アンドロイド:versionCode = "1"
アンドロイド:versionName = "1.0">
<>
アンドロイド:minSdkVersion = "9"
アンドロイド:targetSdkVersion = "17" />