Flutter/Dart Package for SmartConfig Provisioning of ESP Devices

esp_smartconfig

This package brings the functionalities of EspTouch and EspTouchV2 protocols of SmartConfig technology straight into the realm of Dart language. Crafted entirely in Dart, this package can smoothly integrate with any Flutter project and is compatible across a variety of platforms, namely Windows, Linux, macOS, Android, and iOS.

Unraveling SmartConfig

SmartConfig is a powerful technology that enables the seamless connection of a new Wi-Fi device to a Wi-Fi network. It leverages a mobile application to broadcast network credentials from a smartphone or a tablet to a Wi-Fi device that hasn’t been set up yet.

In simpler terms, SmartConfig’s brilliance lies in its ability to establish a Wi-Fi network connection for an unconfigured IoT device, all without necessitating a direct connection between the device and the configurator.

Decoding the Working of SmartConfig

Here’s a peek into the standard activation process facilitated by SmartConfig:

  • The IoT device switches itself to WiFi monitor mode and begins to capture every WiFi packet transmitted Over-The-Air, all while maintaining encryption.
  • The configurator fires up the IoT device’s app, sets the WiFi network name and password, and then kick-starts the activation process.
  • The application encrypts and encodes the WiFi credentials into the length field of UDP broadcast packets which are then dispatched to the WiFi Access Point. While the packets are encrypted, the length remains visible.
  • The IoT device scrutinizes the length of the captured package, decodes the WiFi credentials, and connects to the WiFi network.
  • Once connected, the device announces its successful connection to the configurator.

How to use esp_smartconfig package?

This package simplifies the process of incorporating the provisioning process into Dart or Flutter applications.

To guide you through the implementation of this package, I have created a step-by-step YouTube video explaining how to seamlessly weave the provisioning logic into your code.

For the sake of demonstration purposes I will make Dart console application, and to do that we can execute next command (it is assumed that you have installed dart on your pc):

dart create --template console smart_config_dart

Once when the previous command is executed you will get newly created project folder with the name smart_config_dart. Open that project in your favorite code editor to start with actual coding.

Open the file bin/smart_config_dart.dart and paste the next code:

import 'dart:io';

import 'package:esp_smartconfig/esp_smartconfig.dart';

void main() async {
  final provisioner = Provisioner.espTouch();

  provisioner.listen((response) {
    print("Device ($response) is connected to WiFi!");
  });

  await provisioner.start(ProvisioningRequest.fromStrings(
    ssid: "NETWORK_NAME_GOES_HERE",
    bssid: "ROUTER_BSSID_GOES_HERE",
    password: "NETWORK_PASSWORD_GOES_HERE",
  ));

  await Future.delayed(Duration(seconds: 10));

  provisioner.stop();
  exit(0);
}

Enter the correct WiFi credentials (in previous code) on which you want to connect your ESP device. If you want to connect ESP device on the same network on which your PC is connected, than you can execute next command (on Windows) to find out BSSID of your router:

netsh wlan show interfaces

Once when you have enter correct WiFi informations into the code, you can power-on your ESP device (on which is running procedure for SmartConfig packages capturing) and execute the Dart application.

When device successfully connects to the WiFi network, appropriate feedback message will be printed inside of Debug Console of Dart application. That message will contain IP address that device has got from the DHCP server, and BSSID of the device itself.

Links

Package has been published on the official Google’s Dart/Flutter registry:

https://pub.dev/packages/esp_smartconfig

Source code of the package can be found in the next GitHub repository:

https://github.com/abobija/esp-smartconfig-dart