WiFi

The WiFi Server utility provides UDP-based wireless communication for the Echo microcontroller. Designed for robotics applications requiring higher bandwidth than BLE, it creates a WiFi access point .

Key Features

  • Automatic WiFi access point creation

  • UDP protocol for low-latency communication

  • Automatic client detection and connection management

  • Support for large data transmissions with automatic packet fragmentation

  • Built-in connection timeout handling

  • Optional debug output for development

Performance Note: UDP communication provides lower latency than TCP, making it ideal for real-time robot control applications.


Overview

Method/Constructor
Description

WiFiServerBridge(const char* _ssid, const char* _password, unsigned int _port)

Constructor. Initializes the class with the WiFi SSID, password, and UDP listening port. Also initializes internal state variables like _isConnected and _debugMode(Both False by default).

void begin()

Starts the WiFi Access Point with the given SSID and password, and begins listening for UDP packets on the specified port. Prints debug information if debug mode is enabled.

void processIncoming()

Checks for incoming UDP packets. Updates connection status (_isConnected), stores the last client IP/port, records the last packet time, and saves received data. Also handles connection timeout after 2 seconds of inactivity. Should be called repeatedly in loop().

void sendData(const String& data)

Sends data to the last connected client. If the client is not “connected” (no recent packets received), it does nothing.

String readData()

Returns the last received data as a String and clears the internal buffer. If multiple packets arrive before this is called, previous data may be lost.

bool getStatus() const

Returns the current connection status (true if a client has sent a packet within the last 2 seconds, false otherwise).

void enableDebug(bool enable)

Enables or disables debug printing for received and sent data, connection events, and other internal state information.

void setTimeout(unsigned long ms)

Configures the connection timeout in milliseconds. Default is 2000 ms. Affects when _isConnected is set to false after inactivity.

void setTimeoutConnectionDependent(bool enable)

f set to true, _isConnected will now depend on whether a WiFi client is associated with the access point, rather than waiting for a packet timeout. Overrides the normal packet-timeout behavior. Pass false to revert to the default packet-based timeout.


Quick Start

Your Echo now creates a WiFi network named "EchoRobot" and listens for UDP packets on port 8888.


API Reference

WiFiServerBridge(const char* ssid, const char* password, unsigned int port)

Creates a WiFi server instance that will host an access point.

Parameters:

  • ssid: The WiFi network name

  • password: Network password (minimum 8 characters)

  • port: UDP port number for communication (typically 8888 or 9999)

Example:


begin()

Initializes the WiFi access point and starts the UDP server. Must be called once during setup.

Example:


processIncoming()

Checks for incoming UDP packets and updates connection status. Must be called regularly in the main loop for the server to function.

Example:

Important: Without calling this method, the server cannot receive data or detect client connections.


sendData(const String& data)

Sends raw data to the connected client via UDP. Automatically handles packet fragmentation for large transmissions.

Parameters:

  • data: String data to transmit

Example:

Note: Data is only sent if a client is currently connected. Check connection status with getStatus() before sending critical data.


readData()

Retrieves the most recent data received from the client. Returns an empty string if no new data is available.

Returns:

  • String: The received data, or empty string if no data available

Example:

Important: Data is cleared after being read. Store the returned value if needed for subsequent operations.


getStatus()

Returns the current connection state. A client is considered connected if data has been received within the last 2 seconds.

Returns:

  • true: A client is connected and communication is active

  • false: No active connection

Example:


enableDebug(bool enable)

Enables or disables detailed debug output to Serial Monitor. Useful for development and troubleshooting.

Parameters:

  • enable: true to enable debug output, false to disable

Example:

Debug output includes:

  • Network initialization details

  • Incoming packet information with client IP and port

  • Outgoing packet confirmation

  • Connection timeout notifications


setTimeout(unsigned long ms)

Sets the timeout time in ms, by default, it's 2000ms (2 seconds). Which means that if a packet is not received within 2 seconds, the _isConnected status will be set to false.

Parameters:

  • unsigned long ms represents time in ms

Example:


void setTimeoutConnectionDependent(bool enable);

By default, the server relies on the client constantly sending data to the Echo. This can cause issues in certain applications. To override the default configuration, you can call the setTimeoutConnectionDependent(bool enable) and set it to true. This will now make the status dependent on a client connection rather than a packet detection.


Complete Example: WiFi Controlled Robot

This example demonstrates a complete WiFi-controlled Zippy robot which uses an external app that sends controller data.


Best Practices

Connection Management

Always call processIncoming() at the start of your loop:

Connection Timeout

The server considers a client disconnected after 2 seconds without receiving data. For applications requiring persistent connections, implement periodic heartbeat messages from the client.

Safety Considerations

Implement fail-safe behavior when connections are lost:


Troubleshooting

Cannot Connect to WiFi Network

  • Verify the network name appears in WiFi scan results

  • Ensure password is at least 8 characters

  • Check that the Echo board has successfully started (monitor Serial output)

  • Try restarting the Echo board

  • Verify your device supports 2.4GHz WiFi (5GHz is not supported)

No Data Received on Echo

  • Verify processIncoming() is called in the loop

  • Check that you're sending to the correct IP (192.168.4.1)

  • Confirm the correct port number (8888 in examples)

  • Enable debug mode to see incoming packet details

  • Verify UDP mode is selected in your client application

Data Not Sent from Echo

  • Confirm client connection with getStatus()

  • Verify the Echo received at least one packet from the client first

  • Check Serial output with debug mode enabled

  • Ensure client is listening on the correct port

Intermittent Connection

  • Check WiFi signal strength (move devices closer)

  • Verify stable power supply to Echo board

  • Reduce transmission frequency if overwhelming the network

  • Check for WiFi interference from other networks

Large Data Not Received Completely

  • The library automatically fragments large packets

  • Ensure your client can handle multi-packet transmissions

  • Check client buffer size is adequate

  • Monitor Serial debug output for packet transmission status


Technical Specifications

Network Configuration

  • Protocol: UDP (User Datagram Protocol)

  • WiFi Mode: Access Point (AP)

  • Default IP Address: 192.168.4.1

  • Subnet Mask: 255.255.255.0

  • Maximum Packet Size: 1400 bytes (single packet)

  • Connection Timeout: 2000ms (2 seconds)

  • Multi-packet Format: MULTI:packetId:currentPacket:totalPackets:data

Performance Characteristics

  • Latency: Typically < 10ms for local network

  • Throughput: Depends on network conditions and packet size

  • Client Limit: Standard ESP32 AP supports up to 4 simultaneous clients

  • Range: Approximately 50-100 meters in open space (varies with environment)

Power Considerations

WiFi communication requires more power than BLE:

  • AP Mode (Active): ~120-150 mA

  • Transmitting: ~150-200 mA

  • Idle (Connected): ~80-100 mA

For battery-powered applications, consider implementing sleep modes between transmissions or using BLE for extended operation.

Last updated