Skip to content
Advertisement

Sending data from android to esp8266

I have an arduino project, where i was able to set up an esp8266 as a webserver, and i am able to send data to it, eg. if i put “http://192.168.4.1/get?data=010” into the browser, it works perfectly.
I want to send data using an android app, which pretty much means using the above mentioned url, just with different values for “data”. I’ve tried using okhttp3, but it doesn’t work.
Here is what I’ve tried:

public void sendMessage(View view) throws IOException {
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url("http://192.168.4.1/get?data=010")
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            public void onResponse(Call call, Response response)
                    throws IOException {
                System.out.println(response.toString());
            }

            public void onFailure(Call call, IOException e) {
                System.out.println("Failed");
            }
        });
    }

This seems to work, with other apis, eg. if i put in https://reqres.in/api/users?page=2 as the url I get a response, but when i try to connect to the arduino, it doesn’t do anything.

Advertisement

Answer

Adding android:usesClearTextTraffic="true" to the AndroidManifest.xml file solved the issue.

Thanks for the answer blackapps.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement