ESP32 WiFiClient Library – peek

Home / References / ESP32 Library / WiFiClientSecure

Description

The peek() method in the ESP32 WiFiClient Library allows you to preview the next byte of incoming data from a network connection without removing it from the buffer. This is particularly useful for checking incoming data before deciding how to process it, making it a powerful tool for makers building network-enabled projects with the ESP32. Whether you’re parsing data from a server or debugging a connection, peek() provides a non-destructive way to inspect the stream.


Syntax and Usage

The peek() method is straightforward and has a single usage without arguments. Below is how you can incorporate it into your ESP32 sketches:

int nextByte = client.peek();

This method is typically called on a WiFiClient object after establishing a connection to a server. It lets you “peek” at the next available byte without consuming it, leaving the data intact for subsequent reads.

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)

This method does not require any arguments. It simply checks the next byte in the input buffer of the current network connection and returns its value, making it easy to use in a variety of scenarios.


Return Value

The peek() method returns an integer representing the next byte of data in the input buffer. Specifically:

  • If data is available, it returns the value of the next byte (0 to 255).
  • If no data is available (i.e., the buffer is empty), it returns -1.

This return value allows you to safely check for incoming data without risking the loss of information.


Example Codes

Below is an example demonstrating how to use the peek() method in a practical ESP32 project. This example connects to a test server and uses peek() to inspect incoming data before processing it.

Example: Using peek() to Preview Server Response

This sketch connects to www.httpbin.org, sends a simple GET request, and uses peek() to check the first byte of the response before reading it. It’s a great way to understand how peek() fits into network communication workflows.

/*
 * Author: Avant Maker
 * Date: February 24, 2025
 * Version: 1.0
 * Description: This example demonstrates how to use 
 * ESP32 WiFiClient Library's peek method to
 * check the first byte of the response before reading it. 
 *
 * 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>

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

WiFiClient client;

void setup() {
    Serial.begin(115200);

    // Connect to WiFi
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("\nWiFi connected");

    // Connect to the server
    Serial.print("Connecting to ");
    Serial.println(host);
    if (client.connect(host, port)) {
        Serial.println("Connected to server");

        // Send a GET request
        client.println("GET /get HTTP/1.1");
        client.println("Host: www.httpbin.org");
        client.println("Connection: close");
        client.println();
    } else {
        Serial.println("Connection failed");
    }
}

void loop() {
    if (client.available()) {
        // Peek at the next byte
        int nextByte = client.peek();
        Serial.print("Next byte (peek): ");
        Serial.println(nextByte);

        // Read the byte to confirm
        char c = client.read();
        Serial.print("Read byte: ");
        Serial.println((int)c);

        // Small delay for readability
        delay(100);
    }

    // Stop the client when the connection ends
    if (!client.connected()) {
        Serial.println("Disconnected");
        client.stop();
        while (true) delay(1000); // Halt after disconnect
    }
}

Explanation: In this example, the ESP32 connects to www.httpbin.org and sends a GET request. The peek() method is used in the loop() to preview each incoming byte of the server’s response. After peeking, the byte is read with read() and printed to the Serial Monitor for comparison. This demonstrates how peek() allows you to inspect data without consuming it, giving you flexibility in how you handle the stream.

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