GET is one of the HTTP methods which is used to retrieve a resource from the server. The ESP8266 is a chip that has a full TCP/IP protocol stack. The ESP8266 is able to send requests and retrieve responses from a server using the HTTP protocol.
This tutorial provides example how to send GET request using ESP8266 NodeMCU development board.
Components
No. | Component | Quantity |
---|---|---|
1. | ESP8266 NodeMCU + Micro USB cable | 1 |
Code
We define constants to store Wi-Fi network SSID, password and a server URL. We create an instance of HTTPClient
class that will be used to send GET requests. In the setup
function we initialize serial communication and connect to a Wi-Fi network.
In the loop
function we invoke the begin
method which parses URL. To send the GET request we need to call the GET
method. We can read response content by invoking the getString
method. To close the TCP connection we can use the end
method.
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char *WIFI_SSID = "YOUR WIFI NETWORK NAME";
const char *WIFI_PASSWORD = "YOUR WIFI PASSWORD";
const char *URL = "http://httpbin.org/get";
WiFiClient client;
HTTPClient httpClient;
void setup()
{
Serial.begin(9600);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected");
}
void loop()
{
httpClient.begin(client, URL);
httpClient.GET();
String content = httpClient.getString();
httpClient.end();
Serial.println(content);
delay(5000);
}
Leave a Comment
Cancel reply