Search code examples
androidembeddediotokhttpesp8266

OkHttp fails to connect to ESP8266


I'm trying to send a simple HTTP request to a ESP8266 from my Android phone. I'm following this tutorial. The code works for the URL used in the tutorial (https://reqres.in/api/users?page=2), however not for the URL that turns on the LED on ESP8266. (Turning on the LED is possible through a browser though, so it must be something with the code). The URL is https://192.168.137.78/led/1.
The stacktrace is quite long, but this is the first error,

2020-06-13 12:55:55.948 13594-13631/com.example.esp W/System.err: java.net.ConnectException: Failed to connect to /192.168.137.78:443

The rest of the stacktrace:

2020-06-13 12:55:55.948 13594-13631/com.example.esp W/System.err: java.net.ConnectException: Failed to connect to /192.168.137.78:443
    2020-06-13 12:55:55.949 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:285)
    2020-06-13 12:55:55.949 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:195)
    2020-06-13 12:55:55.950 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:249)
    2020-06-13 12:55:55.950 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:108)
    2020-06-13 12:55:55.950 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:76)
    2020-06-13 12:55:55.951 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:245)
    2020-06-13 12:55:55.951 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
    2020-06-13 12:55:55.951 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
    2020-06-13 12:55:55.951 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:96)
    2020-06-13 12:55:55.952 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
    2020-06-13 12:55:55.952 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
    2020-06-13 12:55:55.952 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
    2020-06-13 12:55:55.953 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
    2020-06-13 12:55:55.953 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
    2020-06-13 12:55:55.953 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:197)
    2020-06-13 12:55:55.954 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:502)
    2020-06-13 12:55:55.954 13594-13631/com.example.esp W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    2020-06-13 12:55:55.954 13594-13631/com.example.esp W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    2020-06-13 12:55:55.955 13594-13631/com.example.esp W/System.err:     at java.lang.Thread.run(Thread.java:919)
    2020-06-13 12:55:55.956 13594-13631/com.example.esp W/System.err: Caused by: java.net.ConnectException: failed to connect to /192.168.137.78 (port 443) from /192.168.137.82 (port 42408) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)
    2020-06-13 12:55:55.956 13594-13631/com.example.esp W/System.err:     at libcore.io.IoBridge.isConnected(IoBridge.java:288)
    2020-06-13 12:55:55.957 13594-13631/com.example.esp W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:193)
    2020-06-13 12:55:55.957 13594-13631/com.example.esp W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:135)
    2020-06-13 12:55:55.957 13594-13631/com.example.esp W/System.err:     at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:142)
    2020-06-13 12:55:55.957 13594-13631/com.example.esp W/System.err:     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
    2020-06-13 12:55:55.958 13594-13631/com.example.esp W/System.err:     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
    2020-06-13 12:55:55.958 13594-13631/com.example.esp W/System.err:     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
    2020-06-13 12:55:55.959 13594-13631/com.example.esp W/System.err:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
    2020-06-13 12:55:55.959 13594-13631/com.example.esp W/System.err:     at java.net.Socket.connect(Socket.java:621)
    2020-06-13 12:55:55.959 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.platform.Platform.connectSocket(Platform.kt:120)
    2020-06-13 12:55:55.960 13594-13631/com.example.esp W/System.err:     at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:283)
    2020-06-13 12:55:55.960 13594-13631/com.example.esp W/System.err:   ... 18 more
    2020-06-13 12:55:55.961 13594-13631/com.example.esp W/System.err: Caused by: android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
    2020-06-13 12:55:55.961 13594-13631/com.example.esp W/System.err:     at libcore.io.IoBridge.isConnected(IoBridge.java:275)
    2020-06-13 12:55:55.961 13594-13631/com.example.esp W/System.err:   ... 28 more

Solution

  • You're sending an HTTPS request, not an HTTP request. You haven't shared the code you're running on the ESP8266 but I'll bet that it's an HTTP server not an HTTPS server. Change the URL that you're trying to connect with to http:// instead of https://.