用于串行连接的Java到PHP(Raspberry Pi)

时间:2016-11-28 01:00:03

标签: java php android arduino raspberry-pi

我今天搜索和搜索过,但我似乎无法弄清楚这里有什么问题。对于后台我使用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 个答案:

答案 0 :(得分:0)

好的,我想出来了。我遇到的第一个问题是连接的字符串。我最初发布的地方

 URL url = new URL("http://192.168.1.101/index.php" + params[0]);

应该是这个

 URL url = new URL("http://192.168.1.101/?" + params[0]);

这允许传递参数。我遇到的第二个问题是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 (isset($_GET['led1'])){
        if($_GET['led1']==1){
        $serial->sendMessage("a");
}}

elseif (isset($_GET['led2'])){
        if($_GET['led2']==1){
        $serial->sendMessage("b");
}}
elseif (isset($_GET['led3'])){
        if($_GET['led3']==1){
        $serial->sendMessage("c");
}}


$serial->deviceClose();

echo "I've sent a message! \n\r";
?>

我希望这有助于那些试图完成同样事情的人。目前这是按设计工作的。 Android应用程序能够通过php页面将命令发送到通过串口连接的arduino。