EspTouch for Android

repository·master·Indexed 22 days ago

https://github.com/espressifapp/esptouchforandroid

Libraries and an application for configuring ESP8266 and ESP32 devices to connect to Wi-Fi Access Points using the Smart Config protocol. Includes support for the original EspTouch and EspTouch V2, providing APIs for synchronization via EspSyncListener and provisioning via EspProvisioningRequest and IEspProvisioner.

Tokens
6.5K
Snippets
16
Records
33
Agent score
77%

What's inside esptouchforandroid

  1. How ESP-TOUCH provisioning works

    master

    ESP-TOUCH uses Smart Config technology to provision Wi-Fi credentials (SSID and password) to ESP8266 and ESP32 devices via a smartphone.

    Because the device is not yet connected to a network, the smartphone cannot communicate with it directly via IP. Instead, the smartphone sends a series of UDP packets to the Wi-Fi Access Point (AP). The SSID and password are encoded into the Length field of these UDP packets. The device, operating in a specific mode, listens to these packets to extract and parse the credentials.

    UDP Packet Structure:

    FieldLength (Bytes)Description
    DA6Destination MAC Address
    SA6Source MAC Address
    Length2Encodes SSID and password
    LLC3Logical Link Control
    SNAP5Subnetwork Access Protocol
    DATAVariablePayload
    FCS4Frame Check Sequence
  2. Understand ESP-TOUCH working modes and IP interaction

    master

    The operational mode of the ESP chip affects how it handles the Wi-Fi connection during configuration:

    • ESP8266: Requires Sniffer mode to be enabled; Station/soft-AP mode must be disabled.
    • ESP32: Can have both Sniffer and Station modes enabled simultaneously.

    IP Interaction

    Once configuration is successful, the smartphone obtains the device's IP address, and the device returns the smartphone's IP address. This enables custom local area network (LAN) communication between the two.

  3. How ESP-TOUCH Smart Config works

    master

    ESP-TOUCH uses Smart Config technology to connect ESP8266 and ESP32 devices to a Wi-Fi network. Since the device is not yet connected to the internet, a Wi-Fi-enabled device (like a smartphone) sends a series of UDP data packets to the Wi-Fi Access Point (AP).

    The SSID and password are encoded within the Length field of the UDP packet. The device listens for these packets and parses them to obtain the network credentials.

    UDP Packet Structure

    FieldLength (Bytes)Description
    DA6Destination MAC address
    SA6Source MAC address
    Length2Contains SSID and Key
    LLC3Logical Link Control
    SNAP5Subnetwork Access Protocol
    DATAVariablePayload
    FCS4Frame Check Sequence
  4. Initialize an EsptouchTask

    master

    To start a provisioning task, instantiate EsptouchTask with the Access Point (AP) credentials and the application context. You can optionally configure the communication method using setPackageBroadcast.

    • setPackageBroadcast(true): Sends broadcast packets.
    • setPackageBroadcast(false): Sends multicast packets.
    Context context; // Set Application context
    byte[] apSsid = {}; // Set AP's SSID
    byte[] apBssid = {}; // Set AP's BSSID
    byte[] apPassword = {}; // Set AP's password
    
    EsptouchTask task = new EsptouchTask(apSsid, apBssid, apPassword, context);
    task.setPackageBroadcast(true); // if true send broadcast packets, else send multicast packets
  5. Configure an ESP8266 or ESP32 device via ESP-TOUCH

    master

    To provision a device using the ESP-TOUCH app, follow these steps:

    1. Prepare Device: Ensure the device supports ESP-TOUCH and has its Smart Config function enabled.
    2. Connect Smartphone: Connect your smartphone to the target Wi-Fi router.
    3. Open App: Launch the ESP-TOUCH App.
    4. Input Credentials: Enter the router's SSID and password. If the router is unencrypted (open), leave the password field blank.

    Operational Requirements & Limitations:

    • ESP8266: Must have Sniffer mode enabled; Station and soft-AP modes must be disabled.
    • ESP32: Can have both Sniffer and Station modes enabled simultaneously.
    • Frequency/Protocol: 5 GHz frequency bands and the 802.11ac protocol are not supported.
    • AP Isolation: If enabled on the router, the App may fail to receive the success notification.
    • Multi-Device: The App supports configuring multiple devices to the same router at once.
  6. Import EspTouch for Android into your project

    master

    To use the EspTouch libraries in your Android project, you must first add the JitPack repository to your root build.gradle file, and then add the specific library dependency to your app module's build.gradle file.

    Important Compatibility Note: EspTouchV2 is not compatible with the original EspTouch. Choose the version that matches the Smart Config implementation running on your ESP device.

    // 1. In your root build.gradle
    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
    
    // 2. In your app module's build.gradle
    // For original EspTouch:
    implementation 'com.github.EspressifApp:lib-esptouch-android:1.1.1'
    
    // OR for EspTouchV2:
    implementation 'com.github.EspressifApp:lib-esptouch-v2-android:2.2.1'
  7. Configure an ESP device via ESP-TOUCH

    master

    To connect an ESP8266 or ESP32 device to a Wi-Fi network using the ESP-TOUCH app, follow these steps:

    1. Prepare the device: Ensure the device is powered on and its Smart Config function is enabled.
    2. Connect the phone: Connect your smartphone to the target Wi-Fi router.
    3. Open the app: Launch the ESP-TOUCH application on your phone.
    4. Enter credentials: Input the router's SSID and password. If the network is open (no encryption), leave the password field empty.

    Important Requirements & Limitations

    • Network Band: Does not support 5 GHz bands or the 802.11ac protocol. Use 2.4 GHz.
    • AP Isolation: If the router has AP Isolation enabled, the app may fail to receive the success confirmation.
    • Distance: Connection time increases as the distance between the device and the router increases.
    • Router State: Ensure the router is fully operational before starting the configuration.
    • Multi-device support: The app can configure multiple devices to join the same router simultaneously.
  8. Configure provisioning parameters via EspProvisioningRequest

    master

    To initiate the provisioning process, you must provide an EspProvisioningRequest object to the EspProvisioningParams constructor. The EspProvisioningParams class internally parses this request to generate the necessary data packets for the EspTouch protocol.

    Key fields in the request that influence the provisioning payload include:

    • ssid: The target Wi-Fi SSID.
    • password: The Wi-Fi password.
    • reservedData: Additional custom data to be sent during provisioning.
    • aesKey: If provided, the provisioning process will use AES encryption for the password and reserved data.
    • securityVer: Specifies the security version (e.g., 2 for AES IV generation).
    • address: The target device address (determines if IPv4 or IPv6 flags are set in the header).
    • bssid: Used to calculate the BSSID CRC in the packet header.
  9. Troubleshoot ESP-TOUCH connection issues

    master

    If provisioning fails, check the following common causes:

    • Distance: If the device is too far from the router, connection may time out or take longer.
    • Router Power: Ensure the router is powered on before starting the configuration process.
    • AP Isolation: Check if the router has 'AP Isolation' enabled; this prevents the App from receiving the success notification.
    • Network Band: Ensure you are not attempting to use a 5 GHz band or 802.11ac, as they are unsupported.
    • Timeout: If the App returns a failure message, the timeout period may have expired before the device could parse the credentials.