dongyong6428 2016-11-28 01:00
浏览 64

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

I've searched and searched today, but I can't seem to figure out what is wrong here. For background I am using an Android app to communicate via PHP to my RPI and send serial commands to my arduino. For reference I followed this and all applicable instructions in order to create this. My application MainActivity looks like this (with IP changed)

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("
");

                in.close();
                connection.disconnect();
                return result.toString();

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    }

My PHP code located at index.php that is mentioned looks like the following:

<?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();

?>

For whatever reason that seems to elude me, I don't see anything coming through on the serial monitor. I don't see that anything is being passed to the arduino. I can paste any other code that may be helpful, but I am not sure what else that would be. I do have an added button on both sets of code (button C does not exist yet in the arduino program, but I am only pressing a and b, if that makes sense)

What am I missing? I know this is a multi-faceted question, but I am hoping that someone here can see what I am missing.

For reference the errors that I get when executing via cli (which probably don't matter as i am passing nothing) are as follows

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

Also the output from Android Studio is showing me the following

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条回答 默认 最新

  • douduan4116 2016-11-28 04:36
    关注

    Ok, I figured this out. The first problem I had was the string for the connection. Where I had originally posted

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

    it should have been this

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

    This allowed the parameters to be passed. The second problem that I had was that the php statements were incorrect. I modified it to the following

    <?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! 
    ";
    ?>
    

    I hope this helps those who are trying to accomplish the same thing. Currently this is working as designed. Android application is able to send commands via php page to the arduino connected via serial.

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)