Home / References / ESP32 Library / WiFiClientSecure
Description
The write
method in the WiFiClient
library enables your ESP32 to transmit data to a connected server or client over a Wi-Fi network. With multiple ways to use it—whether sending a single byte, a buffer, PROGMEM data, or streaming content—this method is a cornerstone for networked applications like IoT devices, web clients, or custom protocols. It empowers makers to communicate seamlessly with the digital world.
Syntax and Usage
The write
method offers four distinct ways to send data, each suited to different scenarios. Here’s how you can use it:
– Writing a single byte:
client.write(data);
Send a single byte of data to the connected endpoint.
– Writing a buffer of bytes:
client.write(buf, size);
Send a sequence of bytes from a memory buffer, with a specified length.
– Writing PROGMEM data:
Send a buffer of bytes stored in PROGMEM (program memory), useful for conserving RAM.
client.write_P(buf, size);
– Writing from a Stream:
client.write(stream);
Send data directly from a Stream
object, such as Serial
or a file.
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 write
method’s arguments vary depending on the usage. Here’s a detailed list:
- data: An unsigned 8-bit integer (
uint8_t
) representing a single byte to send. Used for single-byte writes. - buf: A pointer to an array of bytes (
const uint8_t*
) in RAM containing the data to send. Used with buffer writes. - size: An integer (
size_t
) specifying the number of bytes to send from the buffer. Used with buffer and PROGMEM writes. - buf (PROGMEM): A pointer to a PROGMEM-stored buffer (
PGM_P
) containing the data to send. Used for PROGMEM writes. - stream: A reference to a
Stream
object (e.g.,Serial
) from which data is read and sent. Used for stream writes.
Return Value
The write
method returns a size_t
value indicating the number of bytes successfully written to the connection. A return value of 0 typically means no data was sent, possibly due to a failed connection or empty input. Checking this value ensures your data transmission worked as intended.
Example Codes
Below are examples for each way to use the write
method, connecting to www.httpbin.org
where applicable. These snippets show practical applications for your ESP32 projects.
Example 1: Writing a Single Byte
This example connects to www.httpbin.org
and sends a minimal HTTP GET request byte-by-byte using write
. The server’s response is printed to the Serial monitor, proving the data was received.
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
* Description: This example connects to www.httpbin.org
* and sends a minimal HTTP GET request byte-by-byte using
* ESP32 WiFiClient Library's write method.
* The server’s response is printed to the Serial monitor,
* proving 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;
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
if (client.connect(host, port)) {
Serial.println("Connected to server");
// Send HTTP GET request byte-by-byte
const char* request = "GET /get HTTP/1.1\r\nHost: www.httpbin.org\r\nConnection: close\r\n\r\n";
size_t bytesWritten = 0;
for (int i = 0; i < strlen(request); i++) {
bytesWritten += client.write(request[i]);
}
Serial.print("Total bytes written: ");
Serial.println(bytesWritten);
// Wait for and display server response
delay(1000); // Give server time to respond
Serial.println("Server response:");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
client.stop();
Serial.println("\nConnection closed");
} else {
Serial.println("Connection failed");
}
}
void loop() {
}
Example 2: Writing a Buffer of Bytes
This example sends a full HTTP GET request to www.httpbin.org
using a buffer of bytes, showcasing how to transmit structured data.
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
* Description: This example sends a full HTTP GET request
* to www.httpbin.org using a buffer of bytes, showcasing
* how to transmit structured data with ESP32 WiFiClient
* Library's write method.
*
* 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;
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
if (client.connect(host, port)) {
Serial.println("Connected to server");
const char* request = "GET /get HTTP/1.1\r\nHost: www.httpbin.org\r\nConnection: close\r\n\r\n";
size_t bytesWritten = client.write((const uint8_t*)request, strlen(request)); // Send buffer
Serial.print("Bytes written: ");
Serial.println(bytesWritten);
delay(1000); // Wait for response
while (client.available()) {
Serial.write(client.read());
}
client.stop();
} else {
Serial.println("Connection failed");
}
}
void loop() {
}
Example 3: Writing PROGMEM Data
This example stores an HTTP request in PROGMEM and sends it to www.httpbin.org
, demonstrating RAM-efficient data transmission.
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
* Description: This example stores an HTTP request in
* PROGMEM and sends it to www.httpbin.org with
* ESP32 WiFiClient Library's write method.
* Demonstrating RAM-efficient data transmission.
This example sends a full HTTP GET request
* to www.httpbin.org using a buffer of bytes, showcasing
* how to transmit structured data with ESP32 WiFiClient
* Library's write method.
*
* 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;
// Store request in PROGMEM
const char request[] PROGMEM = "GET /get HTTP/1.1\r\nHost: www.httpbin.org\r\nConnection: close\r\n\r\n";
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
if (client.connect(host, port)) {
Serial.println("Connected to server");
size_t bytesWritten = client.write_P(request, strlen_P(request)); // Send PROGMEM data
Serial.print("Bytes written: ");
Serial.println(bytesWritten);
delay(1000); // Wait for response
while (client.available()) {
Serial.write(client.read());
}
client.stop();
} else {
Serial.println("Connection failed");
}
}
void loop() {
}
Example 4: Writing from a Stream
This example automatically sends a hardcoded HTTP POST request to www.httpbin.org/post
with a key-value pair (message=Hello from ESP32
) using a StringStream
. The server’s JSON response, printed to the Serial monitor, proves the data was received.
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
* Description: This example demonstrates how to use
* ESP32 WiFiClient Library's write method to
* send a data directly from a Stream object.
* 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>
#include <StreamString.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;
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
if (client.connect(host, port)) {
Serial.println("Connected to server");
// Define the HTTP POST request
String request = "POST /post HTTP/1.1\r\n"
"Host: www.httpbin.org\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: 22\r\n"
"Connection: close\r\n"
"\r\n"
"message=Hello from ESP32";
// Create a Stream from the String
StreamString stream;
stream.print(request);
// Send the stream data
size_t bytesWritten = client.write(stream);
Serial.print("Bytes written: ");
Serial.println(bytesWritten);
// Wait for and display server response
delay(1000); // Give server time to respond
Serial.println("Server response:");
while (client.available()) {
Serial.write(client.read());
}
client.stop();
Serial.println("\nConnection closed");
} else {
Serial.println("Connection failed");
}
}
void loop() {
}
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- 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!