아두이노 나노 ESP32 - HTTP 요청 | Arduino Nano ESP32 - HTTP Request

이 튜토리얼은 Arduino Nano ESP32를 사용하여 웹 서버, API, 또는 웹 서비스에 HTTP 요청을 보내는 방법에 대한 지침을 제공합니다. 좀 더 자세히 말하자면, 여러분은 다음을 배우게 될 것입니다:

Arduino Nano ESP32 web client

준비물

1×Arduino Nano ESP32 Amazon
1×USB Cable Type-C 쿠팡 | Amazon
1×Breadboard 쿠팡 | Amazon
1×Jumper Wires Amazon
1×(Optional) DC Power Jack 쿠팡 | Amazon
1×(Recommended) Screw Terminal Adapter for Arduino Nano 쿠팡 | Amazon
공개: 이 섹션에서 제공된 링크 중 일부는 제휴 링크입니다. 이 링크를 통해 구매한 경우 추가 비용없이 수수료를 받을 수 있습니다. 지원해 주셔서 감사합니다.

웹 클라이언트와 웹 서버의 기본 개념

웹의 몇 가지 기본 개념들에는 웹 주소(URL), 호스트명, 경로 이름, 쿼리 문자열, HTTP 요청 등이 있습니다. HTTP 튜토리얼에서 이들에 대해 자세히 배울 수 있습니다.

HTTP 요청하는 방법

라이브러리 포함하기

#include <WiFi.h> #include <HTTPClient.h>

WiFi SSID와 비밀번호를 선언하십시오.

const char WIFI_SSID[] = "YOUR_WIFI_SSID"; // 변경하세요 const char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD"; // 변경하세요

호스트 이름, 경로 이름, 쿼리 문자열

String HOST_NAME = "http://YOUR_DOMAIN.com"; // 변경하세요 String PATH_NAME = "/products/arduino"; // 변경하세요 //String PATH_NAME = "/products/arduino.php"; // 변경하세요 String queryString = "temperature=26&humidity=70"; // 선택적

HTTP 클라이언트 객체를 선언하십시오.

HTTPClient http;

서버에 연결되어 있고, HTTP 요청을 보낼 때. 예를 들어, HTTP GET

http.begin(HOST_NAME + PATH_NAME); //HTTP int httpCode = http.GET();

웹 서버에서 응답 데이터를 읽으세요.

// httpCode가 음수인 경우 오류가 발생합니다. if (httpCode > 0) { // 서버에서 파일을 찾았습니다 if (httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } else { // HTTP 헤더가 전송되었고 서버 응답 헤더가 처리되었습니다 Serial.printf("[HTTP] GET... code: %d\n", httpCode); } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end();

HTTP 요청에 데이터를 포함하는 방법

우리는 HTTP 요청에 데이터를 포함하여 웹 서버로 데이터를 보낼 수 있습니다. 데이터 형식은 HTTP 요청 방법에 따라 달라집니다:

  • HTTP GET 요청의 경우
    • 데이터는 경로명의 쿼리 문자열로만 전송될 수 있습니다.
  • HTTP POST 요청
    • 데이터는 쿼리 문자열 형태뿐만 아니라 Json, XML, 이미지 등 어떤 형태로든 전송될 수 있습니다.
    • 데이터는 HTTP 요청 본문에 포함됩니다.

    HTTP GET 및 POST 방식 모두에서 쿼리 문자열 형식으로 데이터를 보내는 방법을 배워봅시다

    쿼리 문자열을 생성하십시오.

    int temp = // 센서로부터 int humi = // 센서로부터 String queryString = String("temperature=") + String(temp) + String("&humidity=") + String(humi);

    HTTP GET: 경로명질의 문자열을 추가하십시오.

    http.begin(HOST_NAME + PATH_NAME + "?" + queryString); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); int httpCode = http.GET();

    HTTP POST: HTTP 본문에 쿼리 문자열을 넣으세요.

    http.begin(HOST_NAME + PATH_NAME); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); int httpCode = http.POST(queryString);

    GET과 POST 모두에서 웹 서버의 응답 데이터를 읽으십시오.

    // httpCode가 에러인 경우 음수가 됩니다 if (httpCode > 0) { // 서버에서 파일을 찾음 if (httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } else { // HTTP 헤더가 전송되었고 서버 응답 헤더가 처리되었습니다 Serial.printf("[HTTP] GET/POST... code: %d\n", httpCode); } } else { Serial.printf("[HTTP] GET/POST... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end();

HTTP 요청을 만들기 위한 완전한 Arduino Nano ESP32 코드

아래는 HTTP GET/POST 요청을 만들기 위한 전체 Arduino Nano ESP32 코드입니다.

/* * 이 Arduino Nano ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino Nano ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-nano-esp32/arduino-nano-esp32-http-request */ #include <WiFi.h> #include <HTTPClient.h> const char WIFI_SSID[] = "YOUR_WIFI_SSID"; // 바꾸세요 const char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD"; // 바꾸세요 String HOST_NAME = "http://YOUR_DOMAIN.com"; // 바꾸세요 String HOST_NAME = "http://YOUR_DOMAIN.com"; // 바꾸세요 //String PATH_NAME = "/products/arduino.php"; // 바꾸세요 void setup() { Serial.begin(9600); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.println("Connecting"); while(WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to WiFi network with IP Address: "); Serial.println(WiFi.localIP()); HTTPClient http; http.begin(HOST_NAME + PATH_NAME); //HTTP int httpCode = http.GET(); // httpCode will be negative on error if(httpCode > 0) { // file found at server if(httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } else { // HTTP header has been send and Server response header has been handled Serial.printf("[HTTP] GET... code: %d\n", httpCode); } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } void loop() { }

데이터와 함께 HTTP GET 요청을 만드는 Arduino Nano ESP32 코드 완성하기

/* * 이 Arduino Nano ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino Nano ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-nano-esp32/arduino-nano-esp32-http-request */ #include <WiFi.h> #include <HTTPClient.h> const char WIFI_SSID[] = "YOUR_WIFI_SSID"; // 변경하세요 const char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD"; // 변경하세요 String HOST_NAME = "http://YOUR_DOMAIN.com"; // 변경하세요 String PATH_NAME = "/products/arduino"; // 변경하세요 //String PATH_NAME = "/products/arduino.php"; // 변경하세요 String queryString = "temperature=26&humidity=70"; void setup() { Serial.begin(9600); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.println("Connecting"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to WiFi network with IP Address: "); Serial.println(WiFi.localIP()); HTTPClient http; http.begin(HOST_NAME + PATH_NAME + "?" + queryString); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); int httpCode = http.GET(); // httpCode will be negative on error if (httpCode > 0) { // file found at server if (httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } else { // HTTP header has been send and Server response header has been handled Serial.printf("[HTTP] GET... code: %d\n", httpCode); } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } void loop() { }

데이터를 사용하여 HTTP POST 요청을 만드는 아두이노 나노 ESP32 코드 완성

/* * 이 Arduino Nano ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino Nano ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-nano-esp32/arduino-nano-esp32-http-request */ #include <WiFi.h> #include <HTTPClient.h> const char WIFI_SSID[] = "YOUR_WIFI_SSID"; // 바꾸세요 const char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD"; // 바꾸세요 String HOST_NAME = "http://YOUR_DOMAIN.com"; // 바꾸세요 String PATH_NAME = "/products/arduino"; // 바꾸세요 //String PATH_NAME = "/products/arduino.php"; // 바꾸세요 String queryString = "temperature=26&humidity=70"; void setup() { Serial.begin(9600); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.println("Connecting"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to WiFi network with IP Address: "); Serial.println(WiFi.localIP()); HTTPClient http; http.begin(HOST_NAME + PATH_NAME); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); int httpCode = http.POST(queryString); // httpCode will be negative on error if (httpCode > 0) { // file found at server if (httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } else { // HTTP header has been send and Server response header has been handled Serial.printf("[HTTP] POST... code: %d\n", httpCode); } } else { Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } void loop() { }

※ OUR MESSAGES

  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!