2
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?

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

Last updated at Posted at 2024-11-10

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

修正する箇所


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

結果

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

o_coq629.jpg

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

o_coq630.jpg

o_coq626.jpg

o_coq628.jpg

プログラム




#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
}


2
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
2
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?