kuroの覚え書き

96の個人的覚え書き

ESP32をアクセスポイントにする

ESP32-WROOM-32EをアクセスポイントにしてWifiネットワークのとりあえずの中継地点とする

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>

// Set these to your desired credentials.
const char *ssid = "kuroesp"; //SSID
const char *pass = "kk********kk"; //password
const IPAddress ip(192,168,11,1); //IPアドレス
const IPAddress subnet(255,255,255,0); //サブネットマスク

WiFiClient client; //クライアント情報に関する変数
WiFiServer server(80); //ポート80(http)の接続を管理する変数

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println("Configuring access point...");
  
  if (!WiFi.softAP(ssid, pass)) {
    log_e("Soft AP creation failed.");
    while(1);
  }

  // IPAddress myIP = WiFi.softAPIP();
  WiFi.softAPConfig(ip, ip, subnet); //IP及びサブネットマスクの設定
  Serial.print("AP IP address: ");

  // Serial.println(myIP);
  Serial.println(ip);

  server.begin();

  Serial.println("Server started");
}

void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();
            client.print(HtmlSet()); //Htmlを送信
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}

/* クライアントに表示するhtml*/
String HtmlSet(void){
  String str = "";

  str += "<html lang=\"ja\">";
  str += "<head>";
  str += "<meta http-equiv=\"refresh\" content=\"5\">";
  str += "<meta charset=\"UTF-8\">";
  str += "<title>ESP AP</title>";
  str += "</head>";
  str += "<body>";
  str += "<h1>kuro ESP AccessPoint Works!!</h1>";
  str += "</body>";
  str += "</html>";

  return str;
}