ESP32 AsyncUDP Library – print()

Home / References / ESP32 Library / AsyncUDP Library

Description

The print() method is a high-level data transmission function in the AsyncUDP library that provides Arduino-style printing capabilities for ESP32 UDP communication. Inherited from the Arduino Print class, this method enables developers to send formatted text, numbers, and strings to UDP endpoints using familiar Arduino printing syntax.

The method is available in both the AsyncUDP class and the AsyncUDPPacket class, offering convenient options for sending human-readable data over UDP networks. The print() method automatically converts various data types to their string representations before transmission, making it ideal for debugging output, sensor data logging, status messages, configuration updates, and user-friendly communication protocols. It supports multiple data types including integers, floating-point numbers, strings, characters, and formatted text with customizable number bases and decimal precision. The method operates asynchronously and integrates seamlessly with ESP32’s networking stack, providing a simple yet powerful interface for UDP text-based communication in IoT applications, remote monitoring systems, and network debugging tools.

This page is part of the Comprehensive Guide to the ESP32 Arduino Core Library, accessible on AvantMaker.com.

Syntax and Usage

The print() method has multiple overloads and can be used in different contexts:

AsyncUDP class: 

udp.print(data)

Prints data to the connected remote endpoint.

AsyncUDP class formatted: 

udp.print(number, format)

Prints numbers with specified format (DEC, HEX, OCT, BIN).

AsyncUDP class floating-point: 

udp.print(floatNumber, decimalPlaces)

Prints floating-point numbers with specified decimal places.

AsyncUDPPacket class: 

packet.print(data)

Prints response data back to the packet sender.

AsyncUDPPacket formatted: 

packet.print(number, format)

Prints formatted numbers back to the packet sender.

Arguments

The print() method accepts the following arguments:

  • data (various types): The data to be printed. Can be String, const char*, int, long, unsigned int, unsigned long, float, double, or char.
  • format (int): For integer types, specifies the number base: DEC (decimal), HEX (hexadecimal), OCT (octal), or BIN (binary).
  • decimalPlaces (int): For floating-point numbers, specifies the number of decimal places to display (0-6).

Return Value

The print() method returns a size_t value indicating the number of bytes successfully transmitted. A return value of 0 indicates that the transmission failed, while a positive value represents the actual number of characters printed and sent. The method automatically converts the input data to its string representation, calculates the resulting string length, and transmits the formatted text via UDP. For the AsyncUDP class, the method uses the internally stored remote IP address and port from a previous connect() call. For the AsyncUDPPacket class, the method automatically sends the formatted response back to the original sender’s IP address and port. The method ensures reliable text transmission by utilizing the underlying UDP write() functionality and provides immediate feedback about transmission success through its return value.

Example Code

Simple Example: UDP Print Sender

This example demonstrates how to use the print() method to send formatted data to a remote UDP endpoint. The ESP32 connects to a specified remote IP address and port, then periodically sends various types of formatted data including strings, numbers, sensor readings, and system information. This demonstrates the practical application of the print() method for IoT communications, data logging, remote monitoring, and debugging applications.

How to use this example: Upload this code to your ESP32, update the WiFi credentials and remote IP address. The ESP32 will connect to the specified remote endpoint and start sending formatted data every 3 seconds. Use the companion receiver example (provided below) on another ESP32 or computer to see the transmitted data.

/*
 * Author: Avant Maker
 * Date: January 15, 2025
 * Version: 1.0
 * License: MIT
 * 
 * Description: 
 * UDP Print Sender that demonstrates print() method usage for transmitting
 * formatted text and data to a remote UDP endpoint. This example shows how
 * to use Arduino-style print() functions for UDP communication, including
 * various data types, number formatting, and structured text output.
 * Perfect for debugging, logging, and human-readable IoT communications.
 *
 * Features Demonstrated:
 * - UDP connection establishment using connect()
 * - Formatted data transmission using print() method
 * - Multiple data type printing (text, integers, floats)
 * - Number formatting (decimal, hexadecimal, binary, octal)
 * - Floating-point precision control
 * - Sensor data simulation and formatted output
 * - System information reporting
 * - Connection status monitoring and error handling
 *
 * Hardware Requirements:
 * - ESP32 development board
 * - WiFi network access
 * - Remote UDP server (use companion receiver example)
 *
 * How to use this example:
 * 1. Update WiFi credentials (ssid and password)
 * 2. Update remote IP address and port (remoteIP and remotePort)
 * 3. Upload code to ESP32
 * 4. Use companion receiver example to see transmitted data
 * 5. Monitor Serial output for transmission status and statistics
 *
 * Code Source:
 * This example code is sourced from the Comprehensive Guide
 * to the ESP32 Arduino Core Library, accessible on AvantMaker.com.
 * For additional code examples and in-depth documentation related to
 * the ESP32 Arduino Core Library, please visit:
 *
 * https://avantmaker.com/home/all-about-esp32-arduino-core-library/
 *
 * AvantMaker.com, your premier destination for all things
 * DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
 * to empowering makers, learners, and enthusiasts with
 * the resources they need to bring their innovative ideas to life.
 */

#include <WiFi.h>
#include <AsyncUDP.h>

// WiFi credentials
const char* ssid = "your_SSID";          // Replace with your Wi-Fi SSID
const char* password = "your_PASSWORD";  // Replace with your Wi-Fi password

// Remote server configuration
IPAddress remoteIP(192, 168, 0, 123); // UPDATE: Remote server IP address
const uint16_t remotePort = 8888;      // UPDATE: Remote server port

AsyncUDP udp;
unsigned long messageCounter = 0;
bool isConnected = false;

// Simulation variables for sensor data
float temperature = 25.0;
float humidity = 60.0;
int lightLevel = 512;
bool ledState = false;
int batteryLevel = 85;

void setup() {
  Serial.begin(115200);
  Serial.println("ESP32 UDP Print Sender - print() Method Demo");
  
  // Display configuration
  Serial.println("=== Configuration ===");
  Serial.printf("Target Server: %s:%d\n", remoteIP.toString().c_str(), remotePort);
  Serial.printf("Device MAC: %s\n", WiFi.macAddress().c_str());
  Serial.println("====================");
  
  // Connect to WiFi
  WiFi.begin(ssid, password);
  Serial.printf("Connecting to WiFi: %s", ssid);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  
  Serial.println("\nWiFi connected successfully!");
  Serial.printf("Local IP: %s\n", WiFi.localIP().toString().c_str());
  Serial.printf("Gateway: %s\n", WiFi.gatewayIP().toString().c_str());
  Serial.printf("DNS: %s\n", WiFi.dnsIP().toString().c_str());
  Serial.printf("Signal Strength: %d dBm\n", WiFi.RSSI());
  
  // Connect to remote UDP endpoint
  if (udp.connect(remoteIP, remotePort)) {
    Serial.printf("UDP connected to %s:%d\n", remoteIP.toString().c_str(), remotePort);
    isConnected = true;
    
    // Send initial connection message using print()
    size_t sent = udp.print("ESP32 Print Sender Connected - Device: " + WiFi.macAddress());
    Serial.printf("Welcome message sent: %d bytes\n", sent);
  } else {
    Serial.println("Failed to connect to UDP endpoint");
    isConnected = false;
  }
  
  Serial.println("Starting formatted data transmission...");
  Serial.println("======================================");
}

void loop() {
  static unsigned long lastSend = 0;
  static int dataType = 0;
  
  // Send data every 3 seconds
  if (millis() - lastSend > 3000) {
    lastSend = millis();
    
    if (!isConnected) {
      // Attempt to reconnect
      Serial.println("Attempting to reconnect...");
      if (udp.connect(remoteIP, remotePort)) {
        isConnected = true;
        Serial.println("Reconnected successfully");
      } else {
        Serial.println("Reconnection failed");
        return;
      }
    }
    
    messageCounter++;
    
    // Send different types of formatted data based on cycle
    switch (dataType % 6) {
      case 0:
        sendTextMessage();
        break;
      case 1:
        sendSensorData();
        break;
      case 2:
        sendFormattedNumbers();
        break;
      case 3:
        sendFloatingPointData();
        break;
      case 4:
        sendSystemInfo();
        break;
      case 5:
        sendFormattedReport();
        break;
    }
    
    dataType++;
  }
  
  // Update simulated sensor values
  updateSensorValues();
  
  // Display periodic status
  static unsigned long lastStatus = 0;
  if (millis() - lastStatus > 30000) { // Every 30 seconds
    lastStatus = millis();
    displayStatus();
  }
  
  delay(1000);
}

void sendTextMessage() {
  size_t sent = udp.print("Hello from ESP32! Message #");
  sent += udp.print(messageCounter);
  sent += udp.print(" | Time: ");
  sent += udp.print(millis()/1000);
  sent += udp.print("s | Free Heap: ");
  sent += udp.print(ESP.getFreeHeap());
  sent += udp.print(" bytes");
  
  Serial.printf("[%lu] Text Message Sent: %d bytes\n", messageCounter, sent);
  Serial.println("Content: Chained print() calls for message composition");
  Serial.println("─────────────────────────────────────────");
}

void sendSensorData() {
  size_t sent = udp.print("SENSOR|Temp:");
  sent += udp.print(temperature, 2);  // 2 decimal places
  sent += udp.print("°C|Humidity:");
  sent += udp.print(humidity, 1);     // 1 decimal place
  sent += udp.print("%|Light:");
  sent += udp.print(lightLevel);
  sent += udp.print("|LED:");
  sent += udp.print(ledState ? "ON" : "OFF");
  sent += udp.print("|Battery:");
  sent += udp.print(batteryLevel);
  sent += udp.print("%");
  
  Serial.printf("[%lu] Sensor Data Sent: %d bytes\n", messageCounter, sent);
  Serial.printf("Temperature: %.2f°C, Humidity: %.1f%%, Light: %d\n", 
                temperature, humidity, lightLevel);
  Serial.println("─────────────────────────────────────────");
}

void sendFormattedNumbers() {
  int randomValue = random(1000, 9999);
  
  size_t sent = udp.print("NUMBER_FORMATS|Value:");
  sent += udp.print(randomValue);
  sent += udp.print("|HEX:");
  sent += udp.print(randomValue, HEX);
  sent += udp.print("|BIN:");
  sent += udp.print(randomValue, BIN);
  sent += udp.print("|OCT:");
  sent += udp.print(randomValue, OCT);
  
  Serial.printf("[%lu] Formatted Numbers Sent: %d bytes\n", messageCounter, sent);
  Serial.printf("Value: %d (HEX: %X, BIN: %s, OCT: %o)\n", 
                randomValue, randomValue, String(randomValue, BIN).c_str(), randomValue);
  Serial.println("─────────────────────────────────────────");
}

void sendFloatingPointData() {
  float voltage = 3.3 + (random(-100, 101) / 1000.0);
  float current = 0.125 + (random(-25, 26) / 1000.0);
  float power = voltage * current;
  
  size_t sent = udp.print("POWER|V:");
  sent += udp.print(voltage, 3);      // 3 decimal places
  sent += udp.print("V|I:");
  sent += udp.print(current, 4);      // 4 decimal places
  sent += udp.print("A|P:");
  sent += udp.print(power, 5);        // 5 decimal places
  sent += udp.print("W");
  
  Serial.printf("[%lu] Power Data Sent: %d bytes\n", messageCounter, sent);
  Serial.printf("Voltage: %.3fV, Current: %.4fA, Power: %.5fW\n", voltage, current, power);
  Serial.println("─────────────────────────────────────────");
}

void sendSystemInfo() {
  size_t sent = udp.print("SYSTEM|Model:");
  sent += udp.print(ESP.getChipModel());
  sent += udp.print("|CPU:");
  sent += udp.print(ESP.getCpuFreqMHz());
  sent += udp.print("MHz|RAM:");
  sent += udp.print(ESP.getFreeHeap());
  sent += udp.print("b|Flash:");
  sent += udp.print(ESP.getFlashChipSize());
  sent += udp.print("b|RSSI:");
  sent += udp.print(WiFi.RSSI());
  sent += udp.print("dBm");
  
  Serial.printf("[%lu] System Info Sent: %d bytes\n", messageCounter, sent);
  Serial.printf("CPU: %dMHz, Free RAM: %d bytes, RSSI: %d dBm\n", 
                ESP.getCpuFreqMHz(), ESP.getFreeHeap(), WiFi.RSSI());
  Serial.println("─────────────────────────────────────────");
}

void sendFormattedReport() {
  size_t sent = udp.print("REPORT|ID:");
  sent += udp.print(messageCounter, HEX);     // Message counter in hex
  sent += udp.print("|Uptime:");
  sent += udp.print(millis()/1000);
  sent += udp.print("s|MAC:");
  sent += udp.print(WiFi.macAddress());
  sent += udp.print("|IP:");
  sent += udp.print(WiFi.localIP().toString());
  sent += udp.print("|Status:OK");
  
  Serial.printf("[%lu] Status Report Sent: %d bytes\n", messageCounter, sent);
  Serial.printf("Report ID: %X, Uptime: %lu seconds\n", messageCounter, millis()/1000);
  Serial.println("─────────────────────────────────────────");
}

void updateSensorValues() {
  // Simulate changing sensor values
  static unsigned long lastUpdate = 0;
  if (millis() - lastUpdate > 2000) {
    lastUpdate = millis();
    
    // Simulate temperature variation
    temperature += (random(-20, 21) / 10.0);
    if (temperature < 20.0) temperature = 20.0;
    if (temperature > 30.0) temperature = 30.0;
    
    // Simulate humidity variation
    humidity += (random(-50, 51) / 10.0);
    if (humidity < 40.0) humidity = 40.0;
    if (humidity > 80.0) humidity = 80.0;
    
    // Simulate light level variation
    lightLevel += random(-50, 51);
    if (lightLevel < 0) lightLevel = 0;
    if (lightLevel > 1023) lightLevel = 1023;
    
    // Simulate battery level variation
    batteryLevel += random(-2, 3);
    if (batteryLevel < 0) batteryLevel = 0;
    if (batteryLevel > 100) batteryLevel = 100;
    
    // Toggle LED state occasionally
    if (random(0, 10) > 8) {
      ledState = !ledState;
    }
  }
}

void displayStatus() {
  Serial.println("╔══════════════════════════════════════════════════╗");
  Serial.println("║                 PRINT TRANSMISSION STATUS       ║");
  Serial.println("╚══════════════════════════════════════════════════╝");
  Serial.printf("Messages Sent: %lu\n", messageCounter);
  Serial.printf("Connection Status: %s\n", isConnected ? "Connected" : "Disconnected");
  Serial.printf("Target: %s:%d\n", remoteIP.toString().c_str(), remotePort);
  Serial.printf("Local IP: %s\n", WiFi.localIP().toString().c_str());
  Serial.printf("WiFi Signal: %d dBm\n", WiFi.RSSI());
  Serial.printf("Uptime: %lu seconds\n", millis() / 1000);
  Serial.printf("Free Heap: %d bytes\n", ESP.getFreeHeap());
  Serial.println("══════════════════════════════════════════════════");
}

Testing Example: UDP Print Receiver

This companion example demonstrates how to create a UDP receiver that listens for formatted data sent by the previous example. This receiver ESP32 will display all incoming UDP text data, allowing you to verify that the print() method is working correctly. The receiver provides detailed parsing and display of formatted messages, making it perfect for testing and debugging UDP text communications.

How to test with two ESP32 boards:

  1. Setup the Receiver: Upload this “UDP Print Receiver” code to your first ESP32 and note its IP address from the Serial Monitor.
  2. Setup the Sender: Upload the previous “UDP Print Sender” code to your second ESP32 and update the remoteIP variable with the first ESP32’s IP address.
  3. Configure Network: Ensure both ESP32 boards are connected to the same WiFi network.
  4. Monitor Results: Open Serial Monitor for both ESP32s to see formatted data transmission and reception in real-time.
  5. Verify Data: Compare sent data on sender with received data on receiver to verify successful transmission and formatting.
/*
 * Author: Avant Maker
 * Date: January 15, 2025
 * Version: 1.0
 * License: MIT
 * 
 * Description: 
 * UDP Print Receiver for testing print() method functionality. This example
 * creates a UDP server that listens for incoming formatted text data and
 * displays it with detailed parsing and analysis. It's designed to work with
 * the UDP Print Sender example to provide comprehensive testing of the
 * AsyncUDP print() method capabilities.
 */

#include <WiFi.h>
#include <AsyncUDP.h>
#include <map>

// WiFi credentials
const char* ssid = "your_wifi_ssid";
const char* password = "your_wifi_password";

// Server configuration
const uint16_t serverPort = 8888; // Port to listen on

AsyncUDP udp;
unsigned long totalPacketsReceived = 0;
unsigned long totalBytesReceived = 0;
unsigned long totalTextMessages = 0;

// Structure to track sender statistics
struct SenderStats {
  String senderIP;
  unsigned long packetCount;
  unsigned long byteCount;
  unsigned long firstSeen;
  unsigned long lastSeen;
  String lastMessage;
  String messageType;
};

std::map<String, SenderStats> senderMap;

void setup() {
  Serial.begin(115200);
  Serial.println("ESP32 UDP Print Receiver - Testing print() Method");
  
  // Connect to WiFi
  WiFi.begin(ssid, password);
  Serial.printf("Connecting to WiFi: %s", ssid);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  
  Serial.println("\nWiFi connected successfully!");
  Serial.printf("Receiver IP: %s\n", WiFi.localIP().toString().c_str());
  Serial.printf("Update sender ESP32 with this IP address: %s\n", WiFi.localIP().toString().c_str());
  
  // Start UDP server
  if (udp.listen(serverPort)) {
    Serial.printf("UDP Print Server listening on port %d\n", serverPort);
    Serial.println("Waiting for formatted data from sender...");
    
    // Set up packet handler
    udp.onPacket([](AsyncUDPPacket packet) {
      handleIncomingPacket(packet);
    });
  } else {
    Serial.println("Failed to start UDP server");
  }
}

void loop() {
  // Display periodic statistics
  static unsigned long lastStats = 0;
  if (millis() - lastStats > 30000) { // Every 30 seconds
    lastStats = millis();
    displayStatistics();
  }
  
  delay(1000);
}

void handleIncomingPacket(AsyncUDPPacket packet) {
  // Get packet information
  IPAddress senderIP = packet.remoteIP();
  uint16_t senderPort = packet.remotePort();
  size_t packetSize = packet.length();
  
  // Update statistics
  totalPacketsReceived++;
  totalBytesReceived += packetSize;
  totalTextMessages++;
  
  // Update sender statistics
  String senderKey = senderIP.toString() + ":" + String(senderPort);
  unsigned long currentTime = millis();
  
  if (senderMap.find(senderKey) == senderMap.end()) {
    // New sender
    SenderStats newSender;
    newSender.senderIP = senderIP.toString();
    newSender.packetCount = 1;
    newSender.byteCount = packetSize;
    newSender.firstSeen = currentTime;
    newSender.lastSeen = currentTime;
    senderMap[senderKey] = newSender;
    
    Serial.println("╔══════════════════════════════════════════════════════════════════╗");
    Serial.println("║                    NEW PRINT SENDER DETECTED                    ║");
    Serial.println("╚══════════════════════════════════════════════════════════════════╝");
  } else {
    // Existing sender
    senderMap[senderKey].packetCount++;
    senderMap[senderKey].byteCount += packetSize;
    senderMap[senderKey].lastSeen = currentTime;
  }
  
  // Extract and display packet data
  String dataString = "";
  for (size_t i = 0; i < packetSize; i++) {
    dataString += (char)packet.data()[i];
  }
  senderMap[senderKey].lastMessage = dataString;
  
  // Analyze message type
  String messageType = analyzeMessageType(dataString);
  senderMap[senderKey].messageType = messageType;
  
  // Display packet information with detailed parsing
  Serial.println("┌──────────────────────────────────────────────────────────────────┐");
  Serial.printf("│ Packet #%lu from %s:%d (%d bytes) - Type: %s\n", 
                totalPacketsReceived, senderIP.toString().c_str(), senderPort, packetSize, messageType.c_str());
  Serial.println("├──────────────────────────────────────────────────────────────────┤");
  Serial.printf("│ Raw Content: %s\n", dataString.c_str());
  Serial.println("├──────────────────────────────────────────────────────────────────┤");
  
  // Parse and display structured data
  parseAndDisplayData(dataString, messageType);
  
  Serial.println("└──────────────────────────────────────────────────────────────────┘");
  
  // Send acknowledgment back to sender using print() method
  size_t sent = packet.print("ACK_PRINT:");
  sent += packet.print(totalPacketsReceived);
  sent += packet.print(":Received ");
  sent += packet.print(packetSize);
  sent += packet.print(" chars of type ");
  sent += packet.print(messageType);
  
  Serial.printf("Acknowledgment sent: %d bytes\n", sent);
  Serial.println("══════════════════════════════════════════════════════════════════");
}

String analyzeMessageType(const String& data) {
  if (data.startsWith("Hello from ESP32")) {
    return "TEXT_MESSAGE";
  } else if (data.startsWith("SENSOR|")) {
    return "SENSOR_DATA";
  } else if (data.startsWith("NUMBER_FORMATS|")) {
    return "FORMATTED_NUMBERS";
  } else if (data.startsWith("POWER|")) {
    return "POWER_DATA";
  } else if (data.startsWith("SYSTEM|")) {
    return "SYSTEM_INFO";
  } else if (data.startsWith("REPORT|")) {
    return "STATUS_REPORT";
  } else if (data.startsWith("ESP32 Print Sender")) {
    return "WELCOME_MESSAGE";
  } else {
    return "UNKNOWN";
  }
}

void parseAndDisplayData(const String& data, const String& type) {
  if (type == "SENSOR_DATA") {
    Serial.println("│ Parsed Sensor Data:");
    // Parse sensor data format: SENSOR|Temp:XX.XX°C|Humidity:XX.X%|Light:XXX|LED:ON/OFF|Battery:XX%
    int tempStart = data.indexOf("Temp:") + 5;
    int tempEnd = data.indexOf("°C");
    int humStart = data.indexOf("Humidity:") + 9;
    int humEnd = data.indexOf("%");
    int lightStart = data.indexOf("Light:") + 6;
    int lightEnd = data.indexOf("|LED:");
    int ledStart = data.indexOf("LED:") + 4;
    int ledEnd = data.indexOf("|Battery:");
    int battStart = data.indexOf("Battery:") + 8;
    int battEnd = data.lastIndexOf("%");
    
    if (tempStart > 4 && tempEnd > tempStart) {
      Serial.printf("│   Temperature: %s°C\n", data.substring(tempStart, tempEnd).c_str());
    }
    if (humStart > 8 && humEnd > humStart && humEnd > tempEnd) {
      Serial.printf("│   Humidity: %s%%\n", data.substring(humStart, humEnd).c_str());
    }
    if (lightStart > 5 && lightEnd > lightStart) {
      Serial.printf("│   Light Level: %s\n", data.substring(lightStart, lightEnd).c_str());
    }
    if (ledStart > 3 && ledEnd > ledStart) {
      Serial.printf("│   LED State: %s\n", data.substring(ledStart, ledEnd).c_str());
    }
    if (battStart > 7 && battEnd > battStart) {
      Serial.printf("│   Battery Level: %s%%\n", data.substring(battStart, battEnd).c_str());
    }
    
  } else if (type == "FORMATTED_NUMBERS") {
    Serial.println("│ Parsed Number Formats:");
    // Parse format: NUMBER_FORMATS|Value:XXXX|HEX:XXXX|BIN:XXXXXXXX|OCT:XXXX
    int valStart = data.indexOf("Value:") + 6;
    int valEnd = data.indexOf("|HEX:");
    int hexStart = data.indexOf("HEX:") + 4;
    int hexEnd = data.indexOf("|BIN:");
    int binStart = data.indexOf("BIN:") + 4;
    int binEnd = data.indexOf("|OCT:");
    int octStart = data.indexOf("OCT:") + 4;
    
    if (valStart > 5 && valEnd > valStart) {
      Serial.printf("│   Decimal: %s\n", data.substring(valStart, valEnd).c_str());
    }
    if (hexStart > 3 && hexEnd > hexStart) {
      Serial.printf("│   Hexadecimal: %s\n", data.substring(hexStart, hexEnd).c_str());
    }
    if (binStart > 3 && binEnd > binStart) {
      Serial.printf("│   Binary: %s\n", data.substring(binStart, binEnd).c_str());
    }
    if (octStart > 3) {
      Serial.printf("│   Octal: %s\n", data.substring(octStart).c_str());
    }
    
  } else if (type == "POWER_DATA") {
    Serial.println("│ Parsed Power Data:");
    // Parse format: POWER|V:X.XXXV|I:X.XXXXXA|P:X.XXXXXW
    int voltStart = data.indexOf("V:") + 2;
    int voltEnd = data.indexOf("V|I:");
    int currStart = data.indexOf("I:") + 2;
    int currEnd = data.indexOf("A|P:");
    int powStart = data.indexOf("P:") + 2;
    int powEnd = data.lastIndexOf("W");
    
    if (voltStart > 1 && voltEnd > voltStart) {
      Serial.printf("│   Voltage: %sV\n", data.substring(voltStart, voltEnd).c_str());
    }
    if (currStart > 1 && currEnd > currStart) {
      Serial.printf("│   Current: %sA\n", data.substring(currStart, currEnd).c_str());
    }
    if (powStart > 1 && powEnd > powStart) {
      Serial.printf("│   Power: %sW\n", data.substring(powStart, powEnd).c_str());
    }
    
  } else if (type == "TEXT_MESSAGE") {
    Serial.println("│ Parsed Text Message:");
    // Extract message number and timing info
    int msgStart = data.indexOf("Message #") + 9;
    int msgEnd = data.indexOf(" | Time:");
    int timeStart = data.indexOf("Time: ") + 6;
    int timeEnd = data.indexOf("s | Free");
    int heapStart = data.indexOf("Free Heap: ") + 11;
    int heapEnd = data.indexOf(" bytes");
    
    if (msgStart > 8 && msgEnd > msgStart) {
      Serial.printf("│   Message Number: %s\n", data.substring(msgStart, msgEnd).c_str());
    }
    if (timeStart > 5 && timeEnd > timeStart) {
      Serial.printf("│   Sender Uptime: %s seconds\n", data.substring(timeStart, timeEnd).c_str());
    }
    if (heapStart > 10 && heapEnd > heapStart) {
      Serial.printf("│   Sender Free Heap: %s bytes\n", data.substring(heapStart, heapEnd).c_str());
    }
    
  } else {
    Serial.printf("│ Raw Message: %s\n", data.c_str());
  }
}

void displayStatistics() {
  Serial.println("╔══════════════════════════════════════════════════════════════════╗");
  Serial.println("║                    PRINT RECEIVER STATISTICS                    ║");
  Serial.println("╚══════════════════════════════════════════════════════════════════╝");
  Serial.printf("Total Text Messages Received: %lu\n", totalTextMessages);
  Serial.printf("Total Packets Received: %lu\n", totalPacketsReceived);
  Serial.printf("Total Characters Received: %lu\n", totalBytesReceived);
  Serial.printf("Active Senders: %d\n", senderMap.size());
  Serial.printf("Server Uptime: %lu seconds\n", millis() / 1000);
  Serial.printf("Free Heap: %d bytes\n", ESP.getFreeHeap());
  Serial.printf("WiFi Signal: %d dBm\n", WiFi.RSSI());
  
  if (!senderMap.empty()) {
    Serial.println("\nSender Details:");
    Serial.println("IP Address    | Messages | Chars  | First Seen | Last Seen | Last Type");
    Serial.println("--------------|----------|--------|------------|-----------|----------");
    
    unsigned long currentTime = millis();
    for (auto& sender : senderMap) {
      Serial.printf("%-13s | %-8lu | %-6lu | %-10lu | %-9lu | %s\n",
                   sender.second.senderIP.c_str(),
                   sender.second.packetCount,
                   sender.second.byteCount,
                   sender.second.firstSeen / 1000,
                   sender.second.lastSeen / 1000,
                   sender.second.messageType.c_str());
    }
  }
  Serial.println("══════════════════════════════════════════════════════════════════");
}

For more ESP32 development resources and tutorials, visit the All About ESP32 Resources Hub on  AvantMaker.com

ESP32 Library Index

ESP32 Arduino Core Library


FAQ

Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!

error: Content is protected !!