ESP32 WiFiClient Library – print

Home / References / ESP32 Library / WiFiClientSecure

Description

The print method in the ESP32 WiFiClient library is a versatile tool that allows you to send data over a network connection to a remote server or client. This method is inherited from the Print class and is commonly used to transmit strings, numbers, or other data types in a human-readable format. Whether you’re building a web client to communicate with a server or debugging network interactions, print simplifies the process of sending data across a WiFi connection. This method is essential for makers and learners crafting IoT projects with the ESP32.


Syntax and Usage

The print method can be used in multiple ways depending on the type of data you want to send. Below are the primary usages:

With a Single Argument: 

server.client.print(data);

Sends a specific value (e.g., string, integer, float) to the connected client or server.

With a String and Formatting: 

client.print(value, format);

Allows sending formatted data, such as adding a newline or specific number base (e.g., DEC, HEX).

For practical applications and examples of this method, please consult the “Example Code” section on this page. This section provides comprehensive guidance to help you better understand and apply the method effectively.


Argument(s)

The print method can accept the following arguments:

  • data: The data to be sent. This can be a string, integer, float, or other supported type compatible with the Print class. It is required for meaningful use of the method.
  • format (optional): Specifies the format of the data, such as DEC for decimal, HEX for hexadecimal, or BIN for binary. Used primarily with numeric data to control how it’s represented.

Return Value

The print method returns an integer of type size_t, representing the number of bytes successfully written to the network connection. If the write fails (e.g., due to a disconnected client), it may return 0. This return value is useful for verifying that your data was sent correctly.


Example Codes

Below are example code demonstrating usage of the print method described in the “Syntax and Usage” section. The example uses the ESP32 to connect to www.httpbin.org, a testing service that echoes back requests.

Using print with a Single Argument
This example demonstrates how to use the print method from the ESP32 WiFiClient Library to send a POST request to a web server, specifically the HTTPBin server (www.httpbin.org), and how to handle the response.

/*
 * Author: Avant Maker
 * Date: February 24, 2025
 * Version: 1.0
 * Description: This example demonstrates how to use the print
 * method from the ESP32 WiFiClient Library to send a POST
 * request to a web server, specifically the HTTPBin server
 * (www.httpbin.org), and how to handle the response.
 *
 * License: MIT 
 * 
 * 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/references/esp32-arduino-core-index/
 *
 * 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>

const char* ssid = "your-SSID";          // Replace with your Wi-Fi SSID
const char* password = "your-PASSWORD";  // Replace with your Wi-Fi password
const char* host = "www.httpbin.org";
const int port = 80;

void setup() {
    Serial.begin(115200);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("Connected to WiFi");
}

void loop() {
    WiFiClient client;
    if (client.connect(host, port)) {
        Serial.println("Connected to server");
        // Send a POST request with sample data
        String postData = "avantmaker=HelloFromESP32";
        client.print("POST /post HTTP/1.1\r\n");
        client.print("Host: www.httpbin.org\r\n");
        client.print("Content-Type: application/x-www-form-urlencoded\r\n");
        client.print("Content-Length: ");
        client.print(postData.length());
        client.print("\r\nConnection: close\r\n\r\n");
        client.print(postData);

        // Wait for the server's response
        while (client.connected() || client.available()) {
            if (client.available()) {
                String line = client.readStringUntil('\n');
                Serial.println(line); // Print server response line by line
            }
        }
        client.stop();
        Serial.println("Disconnected");
    } else {
        Serial.println("Connection failed");
    }
    delay(5000);
}

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 !!