0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ESP32: WiFiManager の使い方

Posted at

こちらのプログラムを WiFiManager に書き換えました。
ESP32: HTTP クライアント (Get)

WiFiManager では、SSID とパスワードは、EEPROM に保存されます。

プログラム

http_get.ino
// ---------------------------------------------------------------
/*
	http_get.ino

					Mar/28/2025
*/
// ---------------------------------------------------------------
#include <WiFiManager.h>
#include <HTTPClient.h>

HTTPClient http;

WiFiManager wm;
String APNAME=wm.getDefaultAPName();
String CPID=APNAME.substring(6);

int count = 0;

// ---------------------------------------------------------------
void setup()
{
	Serial.begin(115200);
	delay(1000);
	Serial.println("*** setup *** start ***");

	 if (wm.autoConnect()) {
		Serial.println("autoConnect() success!");
	} else {
		Serial.println("autoConnect() failed!");
	}

	Serial.println(WiFi.localIP());
	delay(2000);

	Serial.println("*** setup *** end ***");
}

// ---------------------------------------------------------------
void loop()
{
	Serial.println("*** loop *** " + String(count));
	delay(2000);
	if ( WiFi.status() == WL_DISCONNECTED ) {
		if (wm.autoConnect()) {
			Serial.println("autoConnect() success!");
		} else {
			Serial.println("autoConnect() failed!");
		}

		delay(3000);
		}

	if (WiFi.status() == WL_CONNECTED ) {
		String url_target = "https://httpbin.org/get";
		http_get_proc(url_target);
		delay(2000);
		Serial.println("APNAME = " + APNAME);
		Serial.println("CPID = " + CPID);
		delay(2000);
		}

	delay(1000);
	count++;
}

// ---------------------------------------------------------------
void http_get_proc(String url_target)
{
	if ( WiFi.status() == 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");
		}
}

// ---------------------------------------------------------------

保存された SSID と パスワードを消去するプログラム

ssid_clean.ino
// ---------------------------------------------------------------------
/*
	ssid_clean.ino
						Mar/28/2025
*/
// -------------------------------------------------------------------
#include <WiFiManager.h>

WiFiManager wm;
String APNAME=wm.getDefaultAPName();
String CPID=APNAME.substring(6);

int icount = 0;

// -------------------------------------------------------------------
void setup() {
	Serial.begin(115200);

	delay(1000);
	Serial.println("*** setup *** start ***");

	Serial.println("APNAME = " + APNAME);
	Serial.println("CPID = " + CPID);

	Serial.println("Wi-Fi 設定を消去中...");
	wm.resetSettings();
	delay(1000);

	Serial.println("*** setup *** eee ***");
}

// -------------------------------------------------------------------
void loop() {
	Serial.println("*** loop *** " + String(icount));
	Serial.println("APNAME = " + APNAME);
	Serial.println("CPID = " + CPID);
	delay(4000);
	icount++;
}

// -------------------------------------------------------------------
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?