ESP32 - Requisição HTTPS
Este tutorial ensina você a usar o ESP32 para se comunicar com um servidor web, Web API, REST API, serviço web ...
Sobre HTTPS
O HTTPS é idêntico ao HTTP, exceto que o HTTPS troca dados com segurança entre o cliente e o servidor, criptografando os dados.
Por isso, para aprender sobre HTTPS, você só precisa seguir dois passos:
- Aprenda primeiro como fazer uma requisição HTTP
- Aprenda a criptografar dados. Felizmente, a criptografia é feita pela biblioteca. Você só precisa trocar http por https na URL para transformar HTTP em HTTPS.
Abaixo estão dois exemplos de requisição HTTPS
- Código ESP32 para Fazer Requisição HTTPS GET com dados
/*
* Este código de ESP32 foi desenvolvido por pt.newbiely.com
* Este código de ESP32 é disponibilizado ao público sem nenhuma restrição.
* Para tutoriais completos e diagramas de ligação, visite:
* https://pt.newbiely.com/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() {
}
- Código ESP32 HTTPS para Fazer Requisição HTTPS POST com dados
/*
* Este código de ESP32 foi desenvolvido por pt.newbiely.com
* Este código de ESP32 é disponibilizado ao público sem nenhuma restrição.
* Para tutoriais completos e diagramas de ligação, visite:
* https://pt.newbiely.com/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() {
}