使用Volley与ESP8266与AndroidApp通信

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

我正在尝试将我的Android应用与ESP8266进行通信,但ESP没有任何回应。我将ESP连接到WI-FI网络,因此可以通过PC和手机上的浏览器进行访问,它可以正常工作,但是当我尝试使用应用程序中的Volley进行操作时,什么也没发生。

ESP响应浏览器访问,但是使用Volley似乎甚至没有收到请求,终端中什么也没发生。

这里是代码

Android:

package com.tests;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class MainActivity extends AppCompatActivity {

    private Button getReq;
    private TextView respText;
    private String url ="http://xxx.xxx.x.xx/request1/";

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

        getReq = (Button) findViewById(R.id.gtBt);
        respText = (TextView) findViewById(R.id.resText);

        getReq.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
                StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                    new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        respText.setText(response);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        respText.setText("That didn't work!");
                    }
                });
                queue.add(stringRequest);
            }
        });
    }
}

ESP8266:

#include <ESP8266WiFi.h>

#ifndef STASSID
#define STASSID "xxxxxx"
#define STAPSK  "xxxxxx"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

char bffWifi[20];

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
  Serial.begin(115200);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print(F("Connecting to "));
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(F("."));
  }
  Serial.println();
  Serial.println(F("WiFi connected"));

  // Start the server
  server.begin();
  Serial.println(F("Server started"));

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  Serial.println(F("new client"));

  client.setTimeout(5000); // default is 1000

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(F("request: "));
  Serial.println(req);

  // Match the request
  int val;
  if (req.indexOf("/request1/") != -1) {
   Serial.println("Request Received");
  } 

    // Send the response to the client
    // it is OK for multiple small client.print/write,
    // because nagle algorithm will group them into one single packet

    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println("");

    sprintf(bffWifi,"IT WORKS");

    Serial.println(bffWifi);
    client.println(bffWifi);

    // The client will actually be *flushed* then disconnected
    // when the function returns and 'client' object is destroyed (out-of-scope)
    // flush = ensure written data are received by the other side
    Serial.println(F("Disconnecting from client"));


  // read/ignore the rest of the request
  // do not client.flush(): it is for output only, see below
  while (client.available()) {
    // byte by byte is not very efficient
    client.read();
  }
}
java android rest android-volley esp8266
1个回答
0
投票

为什么不尝试将您的ESP转换为Web服务器并通过http请求(POST和GET)与其进行对话。

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