2019-2-12 14:27:15 [显示全部楼层]
2611浏览
查看: 2611|回复: 1

[教程] [经验] 用app控制树莓派GPIO

[复制链接]
本帖最后由 tyjjr 于 2019-3-13 17:20 编辑

[经验] 用app控制树莓派GPIO图1

第一步:所需材料
树莓派
android手机
电脑,用来把app装在手机上
tica Neue, Helvetica, Arial, sans-serif">

第二步:应用程序部分


应用程序编码:
[经验] 用app控制树莓派GPIO图2


复制代码

到MainActivity.java,注意被忘了改成自己的IP

定义按钮
[mw_shl_code=applescript,true]
/* 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);

[/mw_shl_code]

添加Add onclick/onchange:
把onclick/onchange listeners 放在OnCreate函数:


添加权限到应用程序:
[经验] 用app控制树莓派GPIO图3

我们只需要互联网接入的权限,打开清单文件并添加:
<uses-permission android:name="android.permission.INTERNET" ></uses>在<application... 前面

第三步:树莓派部分
[经验] 用app控制树莓派GPIO图4
[经验] 用app控制树莓派GPIO图5
在/var/www/html 删除 index.html,做一个新文件index.php
在index.php 需要添加:
[mw_shl_code=applescript,true]<?php

exec("gpio mode 0 out");

exec("gpio mode 2 out");

exec("gpio mode 3 out");

if (isset($_GET['led1'])) {

if($_GET['led1'] == 1) {

exec("gpio write 0 1");// pin 0 in wiring pi is gpio 17

} else {

exec("gpio write 0 0");

}

}

if(isset($_GET['led2'])) {

if($_GET['led2'] == 1) {

exec("gpio write 2 1");// pin 2 in wiring pi is gpio 27

} else {

exec("gpio write 2 0");

}

}

if(isset($_GET['led3'])) {

if($_GET['led3'] == 1) {

exec("gpio write 3 1");// pin 3 in wiring pi is gpio 22

} else {

exec("gpio write 3 0");

}

}

?>[/mw_shl_code]
来源BBS网站348081236

gada888  版主

发表于 2019-4-7 16:30:54

安卓app编程可不好学
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail