ESP32 HTTPS Request (Yêu cầu HTTPS)
Hướng dẫn này chỉ cho bạn cách sử dụng ESP32 để kết nối đến web server, Web API, REST API, Web service...
Về HTTPS
HTTPS hoàn toàn giống với HTTP, ngoại trừ việc HTTPS trao đổi dữ liệu một cách an toàn giữa client và server bằng cách mã hóa dữ liệu.
Do đó, để học về HTTPS, bạn chỉ cần thực hiện hai bước:
- Học ESP32 - HTTP Request trước
- Học cách mã hóa dữ liệu. May mắn thay, việc mã hóa dữ liệu được thực hiện bởi thư viện. Bạn chỉ cần thay đổi http thành https trong URL để biến HTTP thành HTTPS.
Dưới đây là hai ví dụ về việc thực hiện HTTPS request
- Mã ESP32 để thực hiện HTTPS GET Request với dữ liệu
/*
* Mã ESP32 này được phát triển bởi newbiely.vn
* Mã ESP32 này được cung cấp để sử dụng công khai, không có ràng buộc.
* Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập:
* https://newbiely.vn/tutorials/esp32/esp32-https-request
*/
#include <WiFi.h>
#include <HTTPClient.h>
const char WIFI_SSID[] = "YOUR_WIFI_SSID"; // CHANGE IT
const char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD"; // CHANGE IT
String HOST_NAME = "https://YOUR_DOMAIN.com"; // CHANGE IT
String PATH_NAME = "/products/arduino"; // CHANGE IT
//String PATH_NAME = "/products/arduino.php"; // CHANGE IT
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() {
}
- Mã ESP32 để thực hiện HTTPS POST Request với dữ liệu
/*
* Mã ESP32 này được phát triển bởi newbiely.vn
* Mã ESP32 này được cung cấp để sử dụng công khai, không có ràng buộc.
* Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập:
* https://newbiely.vn/tutorials/esp32/esp32-https-request
*/
#include <WiFi.h>
#include <HTTPClient.h>
const char WIFI_SSID[] = "YOUR_WIFI_SSID"; // CHANGE IT
const char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD"; // CHANGE IT
String HOST_NAME = "https://YOUR_DOMAIN.com"; // CHANGE IT
String PATH_NAME = "/products/arduino"; // CHANGE IT
//String PATH_NAME = "/products/arduino.php"; // CHANGE IT
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() {
}