ESP8266 - Requête HTTP
Ce tutoriel vous explique comment utiliser l'ESP8266 pour effectuer une requête HTTP vers un serveur web, une API ou un service web. En détail, vous apprendrez :
Ou vous pouvez acheter les kits suivants:
Divulgation : Certains des liens fournis dans cette section sont des liens affiliés Amazon. Nous pouvons recevoir une commission pour tout achat effectué via ces liens, sans coût supplémentaire pour vous. Nous vous remercions de votre soutien.
Il existe quelques concepts de base du web tels que : adresse web (URL), nom d'hôte, chemin d'accès, chaîne de requête, requête HTTP... Vous pouvez en apprendre davantage à leur sujet dans le tutoriel HTTP
Le code ESP8266 complet pour effectuer une requête HTTP GET/POST est le suivant.
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.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.php";
WiFiClient client;
HTTPClient http;
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());
http.begin(client, HOST_NAME + PATH_NAME);
int httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
} else {
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() {
}
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.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 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());
WiFiClient client;
HTTPClient http;
http.begin(client, HOST_NAME + PATH_NAME + "?" + queryString);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
} else {
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() {
}
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.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 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());
WiFiClient client;
HTTPClient http;
http.begin(client, HOST_NAME + PATH_NAME);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.POST(queryString);
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
} else {
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() {
}
※ NOS MESSAGES
N'hésitez pas à partager le lien de ce tutoriel. Cependant, veuillez ne pas utiliser notre contenu sur d'autres sites web. Nous avons investi beaucoup d'efforts et de temps pour créer ce contenu, veuillez respecter notre travail !