ESP32 WiFiClient Library – readStringUntil 

Home / References / ESP32 Library / WiFiClientSecure

Description

The readStringUntil method is a powerful tool in the ESP32 WiFiClient Library, designed to read data from a network stream until a specified terminator character is encountered. This method is particularly useful for parsing responses from servers, such as HTTP responses or custom protocols, making it a key component for IoT and network-based projects.


Syntax and Usage

The readStringUntil method is straightforward to use. Below is the basic syntax, followed by its primary usage scenario:

String WiFiClient::readStringUntil(char terminator)

This method has a single way of usage:

  • Reading Until a Terminator Character: Reads characters from the client’s input stream into a String until the specified terminator character is detected. It stops reading at the terminator (exclusive) or when no more data is available.

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)

  • terminator (char): The character that signals the end of the string to be read. Common examples include '\n' (newline), '\r' (carriage return), or any custom delimiter relevant to your protocol.

Return Value

The method returns a String object containing all characters read from the stream up to, but not including, the terminator character. If no data is available or the connection is closed, it returns an empty string ("").


Example Codes

Below is an example demonstrating the usage of readStringUntil. This example connects an ESP32 to www.httpbin.org, sends an HTTP GET request, and reads the response line-by-line using the method.

Example: Reading HTTP Response Line-by-Line

This code connects to www.httpbin.org, sends a simple GET request, and uses readStringUntil to read each line of the server’s response until a newline character ('\n') is encountered. It’s ideal for parsing HTTP headers or JSON responses.

/*
 * Author: Avant Maker
 * Date: February 24, 2025
 * Version: 1.0
 *
 * Description: This example demonstrates how to use 
 * ESP32 WiFiClient Library's readStringUntil method to
 * read each line of the server’s response until a newline
 * character ('\n') is encountered.
 *
 * 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 WiFi SSID
const char* password = "your-PASSWORD"; // Replace with your WiFi password
const char* host = "www.httpbin.org";
const int port = 80;

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

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

void loop() {
    WiFiClient client;

    // Connect to the server
    if (!client.connect(host, port)) {
        Serial.println("Connection failed");
        delay(5000);
        return;
    }

    // Send HTTP GET request
    client.print(String("GET /get HTTP/1.1\r\n") +
                 "Host: " + host + "\r\n" +
                 "Connection: close\r\n\r\n");

    // Wait for server response
    unsigned long timeout = millis();
    while (client.available() == 0) {
        if (millis() - timeout > 5000) {
            Serial.println("Client Timeout");
            client.stop();
            return;
        }
    }

    // Read and print response line-by-line
    while (client.available()) {
        String line = client.readStringUntil('\n');
        Serial.println(line);
    }

    client.stop();
    delay(10000); // Wait before next request
}

How to Use: Upload this code to your ESP32 after replacing your-SSID and your-PASSWORD with your WiFi credentials. Open the Serial Monitor at 115200 baud to see the HTTP response from www.httpbin.org/get, parsed line-by-line.

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