Package implements EspTouch and EspTouchV2 protocols of SmartConfig technology and it is fully written in plain Dart language which means that it can be used in any Flutter projects as well and it supports all platforms that includes Windows, Linux, macOS, Android and iOS.
What is SmartConfig?
SmartConfig is a provisioning technology that connects a new Wi-Fi device to a Wi-Fi network. It uses a mobile app to broadcast the network credentials from a smartphone, or a tablet, to an un-provisioned Wi-Fi device.
In other words, the idea behind SmartConfig is to allow an unconfigured IoT device to connect to a WiFi network without requiring a direct connection between the configurator and the device itself.
How SmartConfig works?
This is how a standard activation procedure works:
- The IoT device sets itself in WiFi monitor mode and starts to capturing every WiFi (encrypted) packet Over-The-Air.
- The configurator opens the IoT device’s app and sets the WiFi network name and password after which the configurator starts the activation process.
- The application encodes the WiFi credentials into the length field of UDP broadcast packets which is then sent to WiFi Access Point. Packages are encrypted, but the length is still readable.
- The IoT device reads the length of the captured package and decodes the WiFi credentials.
- Device connects to the WiFi network and advertises itself to inform the configurator about successful connection.
How to use esp_smartconfig package?
With the help of this package we can easily implement provisioning process into Dart or Flutter application.
I have made YouTube video about this package, and there I have explained step-by-step how to implement 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: