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?

More than 1 year has passed since last update.

wemosでalexaデバイス その6

Last updated at Posted at 2023-03-24

概要

wemos d1で、alexaデバイスを作ってみる。
練習問題、やってみた。

練習問題

APを立てて、パソコンでWiFiに入って、ブラウザでssidとpassを入力して、ルーターに入って、
ブラウザでLチカする。

方針

AP(アクセスポイント)を、立てる。
DNSサーバー、立てる。
WEBサーバー、立てる。

  • 手順
    パソコンのWiFiでkoko testに、パスワード無しで、入る。
    ブラウザで、www.test.comを開く。
    Settingsをクリック。
    ssidとpassを入力。
    送信をクリック。
    wemosは、ルーターに接続。

サンプルコード

#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>

IPAddress apIP(192, 168, 1, 100);
ESP8266WebServer webServer(80);
const char * ap_ssid = "koko test";
DNSServer dnsServer;
const char * ssid = "****";
const char * password = "****";

void _top() {
	String html = "";
	html +="<html><body><h2>";
	html +="LED [<a href='/off'>ON</a>] [<a href='/on'>off</a>]";
	html +="<br><a  href='/Input'>Settings</a>";
	html +="</h2></body></html>";
	webServer.send(200, "text/html", html);
}

void _on() {
	digitalWrite(LED_BUILTIN, HIGH);
	webServer.sendHeader("Location", String("/"), true);
	webServer.send(302, "text/plain", "");
}
void _off() {
	digitalWrite(LED_BUILTIN, LOW);
	webServer.sendHeader("Location", String("/"), true);
	webServer.send(302, "text/plain", "");
}
void wifiInput() {
	String v1 = String((char *) ssid);
	String v2 = String((char *) password);
	String html = "";
	html += "<h1>WiFi Settings</h1>";
	html += "<form method='post' action='/Set'><br>";
	html += "ESSID<br><input type='text' name='essid' value='" + v1 + "'><br>";
	html += "KEY(pass)<br><input type='text' name='key' value='" + v2 + "'><br>";
	html += "<input type='submit'><br>";
	html += "</form>";
	webServer.send(200, "text/html", html);
}

void wifiSet() {
	String setSsid = webServer.arg("essid");
	String setKey = webServer.arg("key");
	String html = "";
	html += "<h2>WiFi Settings<br>";
	html += "ESSID: " + setSsid + "<br>";
	html += "key(pass): " + setKey + "<br>";
	html += "</h2>";
	webServer.send(200, "text/html", html);
	Serial.print("Connecting to ");
	Serial.println(setSsid);
	WiFi.begin(setSsid, setKey);
	while (WiFi.status() != WL_CONNECTED)
	{
		delay(500);
		Serial.print(".");
	}
	Serial.println("");
	Serial.println("WiFi connected");
	Serial.println("IP address: ");
	Serial.println(WiFi.localIP());
}
void setup() {
	pinMode(LED_BUILTIN, OUTPUT);
	Serial.begin(115200);
	delay(10);
	Serial.print("ok");
	Serial.print("start ap");
	WiFi.mode(WIFI_AP);
	WiFi.softAP(ap_ssid);
	delay(200);
	WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
	dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
	dnsServer.start(53, "*", apIP);
	webServer.on("/", _top);
	webServer.on("/on", HTTP_GET, _on);
	webServer.on("/off", HTTP_GET, _off);
	webServer.on("/Input", wifiInput);
	webServer.on("/Set", wifiSet);
	webServer.onNotFound(_top);
	webServer.begin();
}
void loop() {
	dnsServer.processNextRequest();
	webServer.handleClient();
}





以上。

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?