こちらのプログラムを ESP32 に書き換えました。
M5Stack Core2: HTTP クライアント (Get)
Ubuntu 24.04 上の Arduino IDE 2.3.2 を使いました。
ボードは ESP32 Dev Module です。
http_get.ino
// ---------------------------------------------------------------
/*
http_get.ino
Jul/25/2023
*/
// ---------------------------------------------------------------
#include <Arduino.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
WiFiMulti wifiMulti;
HTTPClient http;
const char *ssid = "rs500m-e1eaf5-1";
const char *password = "7f7a63e07b1c6";
int count = 0;
// ---------------------------------------------------------------
void setup()
{
Serial.begin(115200);
wifiMulti.addAP(ssid, password);
delay(1000);
Serial.println("*** setup *** aaa ***");
delay(1000);
Serial.println("*** setup ***");
}
// ---------------------------------------------------------------
void loop()
{
Serial.println("*** loop *** " + String(count));
delay(5000);
String url_target = "https://httpbin.org/get";
http_get_proc(url_target);
delay(5000);
count++;
}
// ---------------------------------------------------------------
void http_get_proc(String url_target)
{
if((wifiMulti.run() == WL_CONNECTED)) {
Serial.print("[HTTP] begin...\n");
http.begin(url_target);
Serial.print("[HTTP] GET...\n");
int httpCode = http.GET();
if(httpCode > 0) {
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
}
}else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
else
{
Serial.print("connect failed");
}
}
// ---------------------------------------------------------------
ターミナルプログラム
cu -s 115200 -l /dev/ttyUSB0