【发布时间】:2016-11-28 01:00:03
【问题描述】:
我今天搜索和搜索,但我似乎无法弄清楚这里有什么问题。作为背景,我正在使用 Android 应用程序通过 PHP 与我的 RPI 进行通信,并将串行命令发送到我的 arduino。作为参考,我遵循this 和所有适用的说明来创建它。我的应用 MainActivity 看起来像这样(更改了 IP)
package piduinotest.piduinotest;
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 com.piduinotest.piduinotest.R;
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);
/********************************/
/* Define all the buttons */
/********************************/
Button led1 = (Button) findViewById(R.id.SCW);
Button led2 = (Button) findViewById(R.id.SCCW);
Button led3 = (Button) findViewById(R.id.SStep);
/*******************************************************/
/* Set an onclick/onchange listener for every button */
/*******************************************************/
led1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
/* button is led 1 */
new Background_get().execute("led1=1");
} else if (event.getAction() == MotionEvent.ACTION_UP) {
new Background_get().execute("led1=0");
}
return true;
}
});
led2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
/* button is led 2 */
new Background_get().execute("led2=1");
} else if (event.getAction() == MotionEvent.ACTION_UP) {
new Background_get().execute("led2=0");
}
return true;
}
});
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;
}
});
}
/*****************************************************/
/* This is a background process for connecting */
/* to the arduino server and sending */
/* the GET request with the added data */
/*****************************************************/
private class Background_get extends AsyncTask<String, Void, String> {
@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.101/index.php" + 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("\n");
in.close();
connection.disconnect();
return result.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
我提到的位于 index.php 的 PHP 代码如下所示:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include "php_serial.class.php";
$serial = new phpSerial;
$serial->deviceSet("/dev/ttyACM0");
$serial->confBaudRate(9600);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->deviceOpen();
if (!empty($_GET['led1']==1)){
$serial-sendMessage("a");
}
elseif (!empty($_GET['led2']==1)){
$serial-sendMessage("b");
}
elseif (!empty($_GET['led3']==1)){
$serial-sendMessage("c");
}
else {
$serial->deviceClose();
}
$serial->deviceClose();
?>
无论出于何种原因,我似乎都无法理解,我在串行监视器上看不到任何东西。我没有看到任何东西被传递给arduino。我可以粘贴任何其他可能有用的代码,但我不确定还有什么。我确实在两组代码上都添加了一个按钮(arduino 程序中尚不存在按钮 C,但我只按 a 和 b,如果这有意义的话)
我错过了什么?我知道这是一个多方面的问题,但我希望这里的人能看到我所缺少的。
作为参考,我通过 cli 执行时遇到的错误(这可能无关紧要,因为我什么都没传递)如下
PHP Notice: Undefined index: led1 in /var/www/html/index.php on line 14
Notice: Undefined index: led1 in /var/www/html/index.php on line 14
PHP Notice: Undefined index: led2 in /var/www/html/index.php on line 17
Notice: Undefined index: led2 in /var/www/html/index.php on line 17
PHP Notice: Undefined index: led3 in /var/www/html/index.php on line 20
Notice: Undefined index: led3 in /var/www/html/index.php on line 20
Android Studio 的输出也显示以下内容
Connected to the target VM, address: 'localhost:8600', transport: 'socket'
I/System.out: Debugger has connected
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1436)
I/InjectionManager: Inside getClassLibPath caller
W/System: ClassLoader referenced unknown path: /data/app/piduinotest.piduinotest-1/lib/arm64
I/InstantRun: Instant Run Runtime started. Android package is piduinotest.piduinotest, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/piduinotest.piduinotest-1/lib/arm64
D/InjectionManager: InjectionManager
D/InjectionManager: fillFeatureStoreMap piduinotest.piduinotest
I/InjectionManager: Constructor piduinotest.piduinotest, Feature store :{}
I/InjectionManager: featureStore :{}
W/ResourcesManager: getTopLevelResources: /data/app/piduinotest.piduinotest-1/base.apk / 1.0 running in piduinotest.piduinotest rsrc of package piduinotest.piduinotest
W/ResourcesManager: getTopLevelResources: /data/app/piduinotest.piduinotest-1/base.apk / 1.0 running in piduinotest.piduinotest rsrc of package piduinotest.piduinotest
D/Activity: performCreate Call Injection manager
I/InjectionManager: dispatchOnViewCreated > Target : piduinotest.piduinotest.MainActivity isFragment :false
D/SecWifiDisplayUtil: Metadata value : SecSettings2
D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{45576f0 I.E...... R.....ID 0,0-0,0}
D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
D/libEGL: loaded /vendor/lib64/egl/libGLES_mali.so
D/libEGL: eglInitialize EGLDisplay = 0x7f96f6b178
I/OpenGLRenderer: Initialized EGL, version 1.4
[ 11-27 20:08:59.802 15445:15709 D/ ]
ro.exynos.dss isEnabled: 0
D/mali_winsys: new_window_surface returns 0x3000, [1440x2560]-format:1
D/libGLESv1: DTS_GLAPI : DTS is not allowed for Package : piduinotest.piduinotest
D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 84 - 0, 0) vi=Rect(0, 84 - 0, 1127) or=1
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@e621a30 time:752185528
D/ViewRootImpl: MSG_RESIZED: ci=Rect(0, 84 - 0, 0) vi=Rect(0, 84 - 0, 0) or=1
D/ViewRootImpl: ViewPostImeInputStage processPointer 0
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
W/System.err: java.io.FileNotFoundException: http://10.238.203.202/index.phpled1=1
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:242)
W/System.err: at piduinotest.piduinotest.MainActivity$Background_get.doInBackground(MainActivity.java:99)
W/System.err: at piduinotest.piduinotest.MainActivity$Background_get.doInBackground(MainActivity.java:91)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err: at java.lang.Thread.run(Thread.java:818)
D/ViewRootImpl: ViewPostImeInputStage processPointer 1
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
W/System.err: java.io.FileNotFoundException: http://192.168.1.101/index.phpled1=0
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:242)
W/System.err: at piduinotest.piduinotest.MainActivity$Background_get.doInBackground(MainActivity.java:99)
W/System.err: at piduinotest.piduinotest.MainActivity$Background_get.doInBackground(MainActivity.java:91)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err: at java.lang.Thread.run(Thread.java:818)
D/ViewRootImpl: ViewPostImeInputStage processPointer 0
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
W/System.err: java.io.FileNotFoundException: http://192.168.1.101/index.phpled2=1
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:242)
W/System.err: at piduinotest.piduinotest.MainActivity$Background_get.doInBackground(MainActivity.java:99)
W/System.err: at piduinotest.piduinotest.MainActivity$Background_get.doInBackground(MainActivity.java:91)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err: at java.lang.Thread.run(Thread.java:818)
D/ViewRootImpl: ViewPostImeInputStage processPointer 1
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
W/System.err: java.io.FileNotFoundException: http://192.168.1.101/index.phpled2=0
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:242)
W/System.err: at piduinotest.piduinotest.MainActivity$Background_get.doInBackground(MainActivity.java:99)
W/System.err: at piduinotest.piduinotest.MainActivity$Background_get.doInBackground(MainActivity.java:91)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err: at java.lang.Thread.run(Thread.java:818)
D/ViewRootImpl: ViewPostImeInputStage processPointer 0
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
W/System.err: java.io.FileNotFoundException: http://192.168.1.101/index.phpled1=1
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:242)
W/System.err: at piduinotest.piduinotest.MainActivity$Background_get.doInBackground(MainActivity.java:99)
W/System.err: at piduinotest.piduinotest.MainActivity$Background_get.doInBackground(MainActivity.java:91)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err: at java.lang.Thread.run(Thread.java:818)
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@e621a30 time:752193855
D/ViewRootImpl: ViewPostImeInputStage processPointer 1
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
W/System.err: java.io.FileNotFoundException: http://10.238.203.202/index.phpled1=0
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:242)
W/System.err: at piduinotest.piduinotest.MainActivity$Background_get.doInBackground(MainActivity.java:99)
W/System.err: at piduinotest.piduinotest.MainActivity$Background_get.doInBackground(MainActivity.java:91)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err: at java.lang.Thread.run(Thread.java:818)
【问题讨论】:
-
1. Spell out (RPI) - 我知道你的意思是 Raspberry Pi,但它也指美国的一所工程学校。 2.你有没有看
Background_get中的url字符串。我的猜测是它看起来像http://192.168.1.101/index.phpled3=0,它不是有效的 http 语法。一如既往地单独尝试每个组件,以确保每个部分在将它们全部组合在一起之前都可以正常工作。
标签: java php android arduino raspberry-pi