ESP32 Wifi http get and post method request

To perform HTTP GET and HTTP POST requests on an ESP32, you can use the WiFi and HTTPClient libraries.

Setup Overview

  • Hardware Requirements:

    • ESP32 board.
    • Access to a Wi-Fi network.
  • Libraries:

    • WiFi.h for managing Wi-Fi connectivity.
    • HTTPClient.h for sending HTTP GET and POST requests.
  • Target URL:

    • Replace the placeholders with your desired server's endpoints.
                            
                               #include <WiFi.h>
#include <HTTPClient.h>

// Replace with your network credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

// Server URL
const char* serverName = "http://jsonplaceholder.typicode.com/posts/1"; // Example API

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

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

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;

    // Start the HTTP GET request
    http.begin(serverName);
    int httpResponseCode = http.GET();

    if (httpResponseCode > 0) {
      String payload = http.getString(); // Get the response payload
      Serial.println("HTTP Response Code: " + String(httpResponseCode));
      Serial.println("Response Payload: " + payload);
    } else {
      Serial.println("Error on HTTP request: " + String(httpResponseCode));
    }

    http.end(); // Free resources
  } else {
    Serial.println("Wi-Fi Disconnected");
  }

  delay(10000); // Wait for 10 seconds before the next request
}
                            
                        
                            
                               #include <WiFi.h>
#include <HTTPClient.h>

// Replace with your network credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

// Server URL
const char* serverName = "http://jsonplaceholder.typicode.com/posts"; // Example API

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

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

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;

    // Start the HTTP POST request
    http.begin(serverName);
    http.addHeader("Content-Type", "application/json"); // Specify content type

    // JSON payload for the POST request
    String postPayload = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}";
    int httpResponseCode = http.POST(postPayload);

    if (httpResponseCode > 0) {
      String response = http.getString(); // Get the response payload
      Serial.println("HTTP Response Code: " + String(httpResponseCode));
      Serial.println("Response Payload: " + response);
    } else {
      Serial.println("Error on HTTP request: " + String(httpResponseCode));
    }

    http.end(); // Free resources
  } else {
    Serial.println("Wi-Fi Disconnected");
  }

  delay(10000); // Wait for 10 seconds before the next request
}
                            
                        

“LVGL is the only framework that I've seen as open source Graphics Library for Microcontrollers. It’s easy to customize, adapts to any design, and the build size is tiny.”

Rahul R S
Embedded System Engineer