ESP8266到ANDROID通讯

问题描述 投票:0回答:1

这是我第一次在这里问一个问题,并且真的希望有人可以解决我的问题。

我一直致力于我的设计项目,其中一项功能是它可以从ESP8266 NodeMCU与Android进行通信,而NodeMCU无需连接到网络。因此,它就像Android直接连接到Wifi模块,将NodeMCU设置为Access Point和WebServer。

我建立了一个简单的通信,但我希望它是恒定和同步的,因为我正在使用此连接来显示从NodeMCU到Android的传感器读数。我的问题是,它不会经常给出值,它只会给出第一个数据然后什么都没有。

这是Android中的代码:

首先是客户类

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

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

Socket socket = null;

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

@Override
protected String doInBackground(Void... arg0) {
Log.d("Status_2", "Sample_2");
    try {
        if(socket == null)
        {
            socket = new Socket(dstAddress, dstPort);
        }

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

        int bytesRead;
        InputStream inputStream = socket.getInputStream();

        /*
         * notice: inputStream.read() will block if no data return
         */
        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();
            }
        }
    }*/
    //textResponse.setText(response);
    return response;
}

@Override
protected void onPostExecute(String result)
{
    Log.d("Status_1", "sample");
    textResponse.setText(response);
    super.onPostExecute(result);
}

}

这是来自主要活动

public class MainActivity extends Activity {

TextView response;
EditText editTextAddress, editTextPort;
Button buttonConnect, buttonClear, buttonStop;
int check = 0;
public static Client myClient;


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


    editTextAddress =  findViewById(R.id.addressEditText);
    editTextPort =  findViewById(R.id.portEditText);
    buttonConnect =  findViewById(R.id.connectButton);
    buttonClear =  findViewById(R.id.clearButton);
    buttonStop = findViewById(R.id.buttonStop);
    response =  findViewById(R.id.responseTextView);

    myClient = new Client(editTextAddress.getText()
            .toString(), Integer.parseInt(editTextPort
            .getText().toString()), response);

    buttonStop.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            check = 0;
        }
    });

    buttonConnect.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View arg0)
        {
            check = 1;
                myClient.execute();


        }
    });

    buttonClear.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            response.setText("");
        }
    });
}

}

以下是Arduino的代码:

int status = WL_IDLE_STATUS;
boolean alreadyConnected = false; // whether or not the client was connected 
previously

WiFiServer server(8080);

void setup() 
{

   delay(1000);
   Serial.begin(115200);
   Serial.println();
  // MotionSensor_Setup();
   Serial.print("Configuring access point...");
   /* You can remove the password parameter if you want the AP to be open. */
   WiFi.softAP(ssid,password);
   delay(10000);
   IPAddress myIP = WiFi.softAPIP();
   Serial.print("AP IP address: ");
   Serial.println(myIP);

   Serial.println("\nStarting server...");
  // start the server:
  server.begin();

}




void loop() 
{
 // wait for a new client:
  WiFiClient client = server.available();
  // when the client sends the first byte, say hello:
    if(client)
    {
     if (!alreadyConnected) 
{
  // clead out the input buffer:
  client.flush();
  Serial.println(analogRead(A0));
  Serial.println();
  client.println("Hello, client!");
  client.println(analogRead(A0));
  delay(500);
  alreadyConnected = true;
   }
    }




if (client.available() > 0) {
  // read the bytes incoming from the client:
  char thisChar = client.read();
  // echo the bytes back to the client:
  server.write(thisChar);
  // echo the bytes to the server as well:
  Serial.write(thisChar);
}

}

最后我在void loop()方法中尝试了这段代码。

void loop() 
{
 // wait for a new client:
  WiFiClient client = server.available();
  // when the client sends the first byte, say hello:
    while(client.connected())
    {
     client.println(analogRead(A0));
     delay(1000);
     Serial.println(analogRead(A0));
   }
    }

它进入了while条件并在串行监视器中显示数据,但没有显示在Android中。

我尝试了几种技术但仍然没有运气。

我非常感谢你的回复,谢谢!

android arduino esp8266 nodemcu arduino-esp8266
1个回答
0
投票

你可以试试这个 - > Really simple TCP client我根据这本手册做了我的android服务器,一切正常

© www.soinside.com 2019 - 2024. All rights reserved.