如何使用Android应用程序控制arduino IO引脚

步骤1:部件
对于这个instructables你需要几个部分。
一个arduino
一个android智能手机或平板电脑(我正在使用android 5.0.1)
以太网屏蔽
3 led的
3 220欧姆电阻
一些跳线
a breadboard
安装了android studio的计算机
步骤2:以太网盾
我从gearbest.com获得了这个以太网屏蔽。
它立即在我的arduino mega(也来自gearbest.com)上工作
在屏蔽上你有2个spi设备。 sd卡读卡器和用于以太网的w5100 ic。
在这个instructables中,我们只使用以太网部件。
步骤3:架构
我们需要将3个led连接到arduino。您可以使用除引脚0,1,10到13和50到53之外的每个引脚。
我使用的是引脚22,引脚23和引脚24.
您还需要将arduino连接到你的本地网络。不需要互联网。
第4步:arduino草图
对于arduino草图,我从示例网络服务器草图开始。
我尝试记录每一件事,但如果你有问题可以随意提问!
#include
#include
// set the mac address
byte mac[] = {0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed};
// set the ip address
ipaddress ip(192, 168, 1, 177);
// start a server at port 80 (http)
ethernetserver server(80);
void setup() {
// open serial communications
serial.begin(9600);
// start the ethernet connection and the server
ethernet.begin(mac, ip);
server.begin();
// pin 22 - 24 output (leds)
pinmode(22, output);
pinmode(23, output);
pinmode(24, output);
}
void loop() {
// check if client connected
ethernetclient client = server.available();
if (client) { // if there is a client.。.
boolean currentlineisblank = true;
string buffer = “”; // a buffer for the get request
while (client.connected()) {
if (client.available()) {
char c = client.read();// read the data of the client
buffer += c; // store the data in a buffer
if (c == ‘ ’ && currentlineisblank) {// if 2x new line ==》 request ended
// send a standard http response header
client.println(“http/1.1 200 ok”);
client.println(“content-type: text/html”);
client.println(“connection: close”);
client.println(); // blank line ==》 end response
break;
}
if (c == ‘ ’) { // if new line
currentlineisblank = true;
buffer = “”; // clear buffer
} else if (c == ‘ ’) { // if cariage return.。.
//read in the buffer if there was send “get /?。..”
if(buffer.indexof(“get /?led1=1”)》=0) { // if led1 = 1
digitalwrite(24, high); // led 1 》 on
}
if(buffer.indexof(“get /?led1=0”)》=0) { // if led1 = 0
digitalwrite(24, low); // led 1 》 off
}
if(buffer.indexof(“get /?led2=1”)》=0) { // if led2 = 1
digitalwrite(22, high); // led 2 》 on
}
if(buffer.indexof(“get /?led2=0”)》=0) { // if led2 = 0
digitalwrite(22, low); // led 2 》 off
}
if(buffer.indexof(“get /?led3=1”)》=0) { // if led3 = 1
digitalwrite(23, high); // led 3 》 on
}
if(buffer.indexof(“get /?led3=0”)》=0) { // if led3 = 0
digitalwrite(23, low); // led 3 》 off
}
} else {
currentlineisblank = false;
}
}
}
delay(1);
client.stop();
}
}
那是arduino上的代码。
很简单,对吧?让我们去看看应用程序吧!
第5步:应用程序布局
为了创建一个android工作室项目我会在这里重定向你。开头是相同的,选择一个名称并创建主要活动,但在删除“hello world” textview后,您需要添加任意类型的3个按钮。我正在使用开关,切换按钮和普通按钮,但您可以选择最喜欢的。
注意:
如果出现渲染错误,请在窗口顶部将 apptheme 更改为 appcompat.noactionbar
!注意!
正常按钮只会在按下时点亮它的led。释放按钮后led将熄灭。
在 res/values/styles.xml 中,您需要将父级更改为:“theme.appcompat.noactionbar”
好的,现在我们可以开始编写应用程序了!
第6步:应用程序编码
为了对应用进行编码,我让您更轻松。您需要将此代码复制到 mainactivity.java ,并将包 laurens_wuyts.arduinoiocontrol 更改为 company.appname 。
package laurens_wuyts.arduinoiocontrol;
import android.app.activity;
import android.os.asynctask;
import android.support.v7.app.actionbaractivity;
import android.os.bundle;
import android.view.menu;
import android.view.menuitem;
import android.view.motionevent;
import android.view.view;
import android.widget.button;
import android.widget.compoundbutton;
import android.widget.switch;
import android.widget.togglebutton;
import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.net.httpurlconnection;
import java.net.url;
public class mainactivity extends activity{
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
}
@override
public boolean oncreateoptionsmenu(menu menu) {
// inflate the menu; this adds items to the action bar if it is present.
getmenuinflater().inflate(r.menu.menu_main, menu);
return true;
}
@override
public boolean onoptionsitemselected(menuitem item) {
// handle action bar item clicks here. the action bar will
// automatically handle clicks on the home/up button, so long
// as you specify a parent activity in androidmanifest.xml.
int id = item.getitemid();
//noinspection simplifiableifstatement
if (id == r.id.action_settings) {
return true;
}
return super.onoptionsitemselected(item);
}
/*****************************************************/
/* this is a background process for connecting */
/* to the arduino server and sending */
/* the get request withe the added data */
/*****************************************************/
private class background_get extends asynctask {
@override
protected string doinbackground(string.。. params) {
try {
/*********************************************************/
/* change the ip to the ip you set in the arduino sketch */
/*********************************************************/
url url = new url(“http://192.168.1.177/?” + params[0]);
httpurlconnection connection = (httpurlconnection) url.openconnection();
bufferedreader in = new bufferedreader(new inputstreamreader(connection.getinputstream()));
stringbuilder result = new stringbuilder();
string inputline;
while ((inputline = in.readline()) != null)
result.append(inputline).append(“ ”);
in.close();
connection.disconnect();
return result.tostring();
} catch (ioexception e) {
e.printstacktrace();
}
return null;
}
}
}
在此代码中,您只需要将ip更改为arduino的ip。
要检查按钮,您需要做两件事:
定义按钮
为每个按钮添加onclick/onchange监听器。
定义按钮:
/* for a switch */
switch led1 = (switch) findviewbyid(r.id.led1);
/* for a toggle button */
togglebutton led2 = (togglebutton) findviewbyid(r.id.led2);
/* for a normal button */
button led3 = (button) findviewbyid(r.id.led3);
添加onclick/onchange:
将onclick/onchange侦听器放在oncreate函数中。
/* for a switch you‘ll need an “oncheckedchangelistener” like this */
led1.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {
public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
if (ischecked) {
/* switch is led 1 */
new background_get().execute(“led1=1”);
} else {
new background_get().execute(“led1=0”);
}
}
});
/* for a toggle button you also need a “oncheckedchangelistener” */
led2.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {
public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
if(ischecked) {
/* toggle button is led 2 */
new background_get().execute(“led2=1”);
} else {
new background_get().execute(“led2=0”);
}
}
});
/* for a button you’ll need a “ontouchlistener” */
led3.setontouchlistener(new view.ontouchlistener() {
@override
public boolean ontouch(view v, motionevent event) {
if (event.getaction() == motionevent.action_down) {
/* button is led 3 */
new background_get().execute(“led3=1”);
} else if (event.getaction() == motionevent.action_up) {
new background_get().execute(“led3=0”);
}
return true;
}
});
这就是所有编码这需要做!现在我们需要为您的应用添加权限。
步骤7:向您的应用添加权限
让您的应用运行你需要赋予它权限。我们只需要1个权限:上网。要获得此权限,您需要打开清单文件并添加:

步骤8:恢复
在这个教程中,我向您展示了如何通过网络从android手机控制arduino io引脚。
我还为想要使用它的人提供了完整的应用程序目录。


深度解析手机中的通信组件
区块链技术在这五个行业中的应用介绍
张忠谋:内地目前不是对手、台积电劲敌是三星
百度智能音箱以强劲姿态进入市场前三名 形成三分天下的市场格局
研华三大新成长引擎打造Edge+生态圈,加速AIoT应用落地全球——专访研华嵌入式总经理张家豪
如何使用Android应用程序控制arduino IO引脚
紧随Vive的脚步, Oculus 的虚拟墙“Guardian”上线
2019年第二季度苹果iPhone出货量将大幅改善
光学系统的几何光学设计理论基础
带你领悟智能大灯光源技术路线
基于去中心化的区块链交易和支付平台OmiseGO介绍
2023增透膜/减反射膜行业分析
如何保护自己手机的隐私需要关闭什么功能
专注精密光学镜头领域,中润光学拟科创板IPO
我国整体智能家电的发展仍处在早期阶段 未来任重而道远
CPU的定义和分类解析
intel新品轮番轰炸_intel新cpu将集成wifi
简单介绍氨气检测仪是什么
电感器和电容器的高频特性基础知识——阻抗和谐振(1)
详解高压无功补偿发生器(SVG)电源解决方案