🛠 Se connecter au Wi-Fi et interroger une API HTTP
Objectives
Connect the M5StickC Plus2 to a 2.4 GHz Wi-Fi network (the ESP32-PICO-V3-02 chip does not support 5 GHz; the v2's 3D antenna offers better range than the v1) and query an HTTP API using a GET request with the HTTPClient library, then display a value extracted from the JSON.
Hardware
- 1× M5StickC Plus2 (v2)
- 2.4 GHz Wi-Fi network + access to a simple HTTP API (e.g.,
http://ip.jsontest.comor a local server) - Arduino IDE / PlatformIO: M5Unified + HTTPClient (included in the esp32 package)
Complete code
// Wi-Fi 2.4 GHz + requete HTTP GET (reconnexion auto par minute)
#include <M5Unified.h>
#include <WiFi.h>
#include <HTTPClient.h>
const char* SSID = "VOTRE_WIFI_24GHZ";
const char* PASS = "motdepasse";
const char* URL = "http://ip.jsontest.com/"; // renvoie {"ip": "..."}
void setup {
auto cfg = M5.config;
M5.begin(cfg);
Serial.begin(115200);
M5.Display.setRotation(1);
M5.Display.setTextSize(1);
M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
M5.Display.printf("WiFi %s...\n", SSID);
WiFi.mode(WIFI_STA); // station uniquement
WiFi.begin(SSID, PASS);
while (WiFi.waitForConnectResult != WL_CONNECTED) {
M5.Display.println("echec, nouvelle tentative...");
delay(3000);
}
M5.Display.printf("IP: %s\n", WiFi.localIP.toString.c_str);
}
void boucleHttp {
HTTPClient http;
http.begin(URL);
int code = http.GET;
M5.Display.setCursor(0, 40);
if (code == HTTP_CODE_OK) {
String corps = http.getString;
int i = corps.indexOf("\"ip\": \"") + 7; // extraction JSON maison
String ip = (i > 6) ? corps.substring(i, corps.indexOf('"', i)) : "?";
M5.Display.setTextColor(TFT_GREEN, TFT_BLACK);
M5.Display.printf("IP publique: %s", ip.c_str);
Serial.println(corps);
} else {
M5.Display.setTextColor(TFT_RED, TFT_BLACK);
M5.Display.printf("HTTP erreur %d", code);
}
http.end;
}
void loop {
M5.update;
if (M5.BtnA.wasPressed) boucleHttp; // A = relancer une requete
if (WiFi.status != WL_CONNECTED) WiFi.reconnect;
vTaskDelay(50 / portTICK_PERIOD_MS);
}
Expected verification
After connecting, the local IP is displayed; pressing A triggers a GET request and the public IP returned by the API is displayed in green. The serial monitor shows the raw JSON body.
Common errors
- Router on 5 GHz: the chip only supports 802.11 b/g/n 2.4 GHz — the network appears "not found" even though it exists.
- Forgetting
WiFi.mode(WIFI_STA)beforeWiFi.begin: random behavior. - Using ADC2 (pins G25/G26) for analog readings while Wi-Fi is active: on the classic ESP32, ADC2 is unavailable when Wi-Fi is running — use ADC1 (G32/G33/G36).
- Using an
https://URL with simple HTTPClient: WiFiClientSecure is required, otherwise a connection error occurs. - SSID/password strings with accents or improperly escaped characters in the code.
- Freezing the loop during the GET request: the interface becomes unresponsive; for a product, move the request to a FreeRTOS task.
Other tutorials for this board
Code examples for this board
hello-stick.inoArduinoboutons.inoArduinobuzzer.inoArduinoinclinometre.inoArduinohorloge.inoArduinoir-tv.inoArduino