M5NanoC6、WebServerを使ってそれっぽいHTMLをPCに表示させる


x 過去ログを見よ!!
x 何度か「シリアルモニタ」を閉じたり開いたりする。


修正する箇所

```c++

const char *ssid = "ご自宅のSSID";
const char *password = "ご自宅のパスワード";

```

結果

(なぜかUSBシリアルが失敗)

![o_coq629.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/70c6716e-14cb-d45a-e1ef-785e75f660fe.jpeg)



(開きなおす)
(なぜかUSBシリアルが成功)

![o_coq630.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/b0235728-f29c-609e-44ac-19c2cb9e6ace.jpeg)



![o_coq626.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/4a1eebaa-8dea-f290-754b-c32309a322c1.jpeg)


![o_coq628.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/56ccfbc4-d294-a1e7-f324-99ef2b14f565.jpeg)


プログラム


```c++



#include <WiFi.h>
#include <NetworkClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>

const char *ssid = "ご自宅のSSID";
const char *password = "ご自宅のパスワード";

WebServer server(80);

const int led = 7;

void handleRoot() {
  digitalWrite(led, 1);

  char html[] = {
    "                                "
    "<HTML lang=\"jp\">              "
    "                                "
    "<HEAD>                          "
    "                                "
    "  <meta charset=\"UTF-8\">      "
    "                                "
    "  <TITLE>ようこそ</TITLE>        "
    "                                "
    "</HEAD>                         "
    "                                "
    "<BODY>                          "
    "                                "
    "  <H1>M5NanoC6の接点情報</H1>    "
    "                                "
    "  <H2>GPIO-G1</H2>              "
    "                                "
    "  AAA                           "
    "                                "
    "  <H2>GPIO-G2</H2>              "
    "                                "
    "  BBB                           "
    "                                "
    "                                "
    "  <H3>適度に更新してください</H3>  "
    "                                "
    "</BODY>                         "
    "                                "
    "</HTML>                         "
    "                                "
    "                                "
  };
  
  server.send(200, "text/html", html);
  digitalWrite(led, 0);
}

void handleNotFound() {
  digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

void setup(void) {
  pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  for(int i=0;i<9;i++){
    delay(500); //起動待ち
    Serial.print(".");
  }
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp32")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);

  server.on("/inline", []() {
    server.send(200, "text/plain", "this works as well");
  });

  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");
}

void loop(void) {
  server.handleClient();
  delay(2);  //allow the cpu to switch to other tasks
}


```