Search code examples
flutterdartwifiiotesp32

Flutter | IoT | Http Request fails after SoftAp Provisioning


With my Flutter app on Android I can add an ESP to my WiFi and send requests to it.

However provisioning and sending requests are working perfectly on their own, but not in direct succession.

Goal is to do the whole connection handling inside the app:
I start with connecting to the ESPs own WiFi, next i provide my local WiFi credentials and apply the config through SoftAp provisioning. After that the ESP and my smartphone are in the same local WiFi and i can discover the ESP. I choose the ESP and send an authentication request, which results in this Error message:

E/flutter (31739): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Connection failed
E/flutter (31739): #0      IOClient.send (package:http/src/io_client.dart:88:7)
E/flutter (31739): <asynchronous suspension>
E/flutter (31739): #1      ChopperClient.send (package:chopper/src/base.dart:305:23)
E/flutter (31739): <asynchronous suspension>
E/flutter (31739): #2      ESPDevice.sendRequest ([...filepath...])
E/flutter (31739): <asynchronous suspension>

Sending the request will work perfectly …

  • again, after closing the app completely and opening it again.
  • with a smartphone (which was not involved in the provisioning).
  • with postman.
  • if the provisioning is done via Apple Home.
  • if the app is freshly opened after provisioning with the ESP SoftAP Prov app.

Sending the request won’t work …

  • if the provisioning is done in the app.
  • if the app is running in background, while provisioning with the ESP SoftAP Prov app.

Here is my (simplified) Code:

await WiFiForIoTPlugin.connect(ssid, password: password);
await WiFiForIoTPlugin.forceWifiUsage(true);
Provisioning provisioning = Provisioning(transport: TransportHTTP(hostname: '192.168.4.1:80'), security: Security1(pop: pop));
await provisioning.establishSession();
await provisioning.sendWifiConfig(ssid: newSsid, password: newPassword);
await provisioning.applyWifiConfig();
await WiFiForIoTPlugin.disconnect();

OnWillPop

await provisioning.dispose();
await WiFiForIoTPlugin.disconnect();

Packages

I'm using the following packages:

  • bonsoir: ^2.0.0 (find the devices in my WiFi)
  • http: ^0.13.5 (send requests)
  • chopper: ^4.0.6 (send requests)
  • esp_provisioning_softap: ^1.0.3 (add the ESP to my WiFi)
  • wifi_iot: ^0.3.18 (connect to the ESP to provide WiFi credentials inside my app)

Solution

  • I fixed my problem by adding the following line to my OnWillPop Method:

    await WiFiForIoTPlugin.forceWifiUsage(false);
    

    Somehow the forceWifiUsage had problems using WiFi.