Home / References / ESP32 Library / WiFiClientSecure
Description
The println method in the ESP32 WiFiClient library is a powerful and convenient way to send data over a network connection, automatically appending a newline character (\r\n) at the end. Inherited from the Print class, this method is perfect for sending formatted messages or commands to a server or client, making it a favorite for IoT projects and network debugging. With println, makers and learners can easily communicate structured data across WiFi, enhancing their ESP32 creations.
Syntax and Usage
The println method can be used in different ways depending on your needs. Below are the primary usages:
– Without Arguments:
client.println();Sends only a newline sequence (\r\n) to the connected client or server, useful for formatting or signaling the end of a message.
– With a Single Argument:
client.println(data);Sends a value (e.g., string, integer, float) followed by a newline, ideal for sending complete lines of data.
– With a Value and Formatting:
client.println(value, format);Sends a formatted value (e.g., decimal, hexadecimal) followed by a newline, offering control over numeric representation.
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 println method can accept the following arguments:
- value (optional): The data to be sent before the newline. This can be a string, integer, float, or other type supported by the
Printclass. If omitted, only a newline is sent. - format (optional): Specifies the format of the data, such as
DECfor decimal,HEXfor hexadecimal, orBINfor binary. Used with numeric data to define its representation.
Return Value
The println method returns an integer of type size_t, indicating the number of bytes written to the network connection, including the newline characters (\r\n). A return value of 0 may indicate a failure, such as a disconnected client, making it a handy way to check the success of your transmission.
Example Codes
Below are example codes showcasing each usage of the println method from the “Syntax and Usage” section. These examples connect the ESP32 to www.httpbin.org and use POST requests to demonstrate data transmission and server response.
Example 1: Using println Without Arguments
This example sends a POST request to www.httpbin.org with a simple message, using println() without arguments to complete the HTTP headers with a blank line, as required by the HTTP protocol. The server’s response is printed to the Serial monitor.
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
* Description: This example sends a POST request to
* www.httpbin.org with a simple message, using
* ESP32 WiFiClient Library's println method
* without arguments to complete the HTTP headers
* with a blank line, as required by the HTTP protocol.
* The server’s response is printed to the Serial monitor.
*
* 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");
String postData = "message=Hello";
client.println("POST /post HTTP/1.1");
client.println("Host: www.httpbin.org");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(postData.length());
client.println("Connection: close");
client.println(); // Blank line to end headers
client.println(postData);
while (client.connected() || client.available()) {
if (client.available()) {
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
client.stop();
Serial.println("Disconnected");
} else {
Serial.println("Connection failed");
}
delay(5000);
}Example 2: Using println With a Single Argument
This example sends a POST request to www.httpbin.org with a message, using println to send each header and the data line, automatically adding newlines. The server’s response confirms the data was received.
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
* Description: This example sends a POST request to
* www.httpbin.org with a message, using ESP32 WiFiClient
* Library's println method to send each header and
* the data line, automatically adding newlines.
* The server’s response confirms the data was received.
*
* 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");
String postData = "test=HelloFromESP32";
client.println("POST /post HTTP/1.1");
client.println("Host: www.httpbin.org");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println(String("Content-Length: ") + postData.length());
client.println("Connection: close");
client.println(); // Blank line to end headers
client.println(postData);
while (client.connected() || client.available()) {
if (client.available()) {
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
client.stop();
Serial.println("Disconnected");
} else {
Serial.println("Connection failed");
}
delay(5000);
}ESP32 Library Index
- ESP32 WiFi Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- ESP32 AsyncUDP Librarry
- ESP32 WiFiClient Library
- Connection
- Send Data
- Receive Data
- Config
- Status
- Which ESP32 Boards are Recommended for Learners
- How to Copy Codes from AvantMaker.com
- What is SPIFFS and how to upload files to it?
- What is LIttleFS and how to upload files to it?
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!