如何通过esp8266从arduino连续发送数据到Android应用程序

时间:2016-09-06 14:42:34

标签: android arduino command android-wifi esp8266

我正在尝试通过esp8266将数据从arduino发送到Android应用程序。目前,arduino和Android应用程序通过esp8266使用TCP / IP套接字API连接。但是,当试图从arduino连续发送数据时,它只被android应用程序收到一次。实际上,只有当我继续执行AT + CLOSE命令来关闭套接字并结束通信时,应用程序才能很好地接收该消息。这种方法使我只能获取一次数据。所以,我试图在关闭它之后再尝试配置通信(请检查我的代码)

void loop()
{
 if(Serial1.available())
 {
  Serial.println("heeeere");
  int connectionId = Serial1.read()-48;

  while(analogRead(4)>0)
  {
    convertedvalue = String((analogRead(4)*5)/1024); //convert read analog  value before sending it to the android app
    convertedvaluelength = String(convertedvalue.length()); //get the length of the converted value
    content = "Converted value is "; //make the response to be send to the android app
    content += convertedvalue; //make the response to be send to the android app
    sendCIPData(connectionId,content);
    sendData("AT+CIPCLOSE=0\r\n",1000,DEBUG); //close the connection
    delay(10000);
    sendData("AT+CIPSERVER=0\r\n",1000,DEBUG); // turn off server 
    delay(10000);
    sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
    delay(10000);
   }
 }
}

Android代码:

package com.example.youssefguirat.socketseverywhere;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;



class Client extends AsyncTask<Void, Void, Void> {

    String dstAddress;
    int dstPort;
    String response = "";
    TextView textResponse;

    Client(String addr, int port,TextView textResponse) {
        dstAddress = addr;
        dstPort = port;
        this.textResponse=textResponse;
    }

    @Override
    protected Void doInBackground(Void... arg0) {

        Socket socket = null;

        try {
            socket = new Socket(dstAddress, dstPort);

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
                    1024);
            byte[] buffer = new byte[1024];

            int bytesRead;
            InputStream inputStream = socket.getInputStream();
            socket.sendUrgentData(16);
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, bytesRead);
                response += byteArrayOutputStream.toString("UTF-8");
            }

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            response = "UnknownHostException: " + e.toString();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            response = "IOException: " + e.toString();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

/*    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        textResponse.setText(values[0]);
        Toast.makeText(MainActivity.this,"Server:"+values[0], Toast.LENGTH_LONG).show();
        Log.w("MSG","Updating with msg");
    }*/

    @Override
    protected void onPostExecute(Void result) {
        textResponse.setText(response);
        super.onPostExecute(result);
    }

}

public class MainActivity extends Activity {

    Button buttonSend;
    TextView textViewSocket;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonSend = (Button) findViewById(R.id.buttonSend);
        textViewSocket = (TextView)  findViewById(R.id.textViewSocket);

        buttonSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Client myClient = new Client("192.168.1.25",80,textViewSocket);
                myClient.execute();

            }
        });


    }

}

正如您所看到的,我为了从esp8266获得正确而重要的答案而拖延了很长时间,但它不起作用:(busy response when trying to close and configure the server each time I send data

所以,有人可以帮助我!我真的被卡住了:(

0 个答案:

没有答案