0

I'm trying to communicate my Android App with the ESP8266 but I'm getting no response from the ESP. I connected the ESP to my WI-FI network so I am able to access via browser on my PC and phone, it works fine but the moment I try to do it with the Volley in the app, nothing happens.

The ESP responds to browser access but using Volley it seems like it doesn't even gets the request, nothing happens in the terminal.

Here are the codes

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();
  }
}
  • i'm having the same issue... but i think that you need to have two return lines at the end of your header before you send data... – me_ Aug 13 '20 at 09:34

1 Answers1

0

Why not trying to turn your ESP into a web server and talk to it through http requests (POST and GET).

Taha teber
  • 20
  • 7
  • I'm sorry but isn't that what I did? I'm using the WebServer example in the ESP, and using the Volley library to do the HTTP GET/POST requests. I think I didn't specify that, sorry. – Bruno Copceski Mar 27 '20 at 14:56