日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# Android wifi控制灯,求助如何在基于安卓通过WiFi与Arduino通信,实现对LED灯的控制。...

發布時間:2023/12/10 C# 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# Android wifi控制灯,求助如何在基于安卓通过WiFi与Arduino通信,实现对LED灯的控制。... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

滿意答案

dkmeng

推薦于 2017.12.15

采納率:55%????等級:9

已幫助:567人

項目需要的硬件如下:

Arduino Uno

Ethernet Shield

LED燈 2個.

電阻 2個.

面包板(可選)

連接導線

路由器一個

項目要的連接管腳如下:

LED 1 --> pin 6 to ground

LED 2 --> pin 7 to ground

項目需要的軟件如下:

Eclipse IDE

Arduino IDE 1.x.x

LED 1 --> pin 6 to ground

LED 2 --> pin 7 to ground

項目需要的軟件如下:

Eclipse IDE

Arduino IDE 1.x.x

Step 1: 在 Arduino上編程如下:#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: 制作安卓APP

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;

}

}

}

01分享舉報

總結

以上是生活随笔為你收集整理的C# Android wifi控制灯,求助如何在基于安卓通过WiFi与Arduino通信,实现对LED灯的控制。...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。