1
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のサンプルを改造して接点情報を表示。(3.1.1)

Last updated at Posted at 2025-01-09

x 過去ログを見よ!!
x 3.1.1
x なぜか、なんかいか「ツール」-「シリアルモニター」を開くと成功する(windows系)
 リ、コンパイルするとリセットが掛る(ubuntu系)

目的
最小改造でG1の状態をWEBに表示

SSIDとパスワードは、修正する事


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

ローカルドメインを使用する


http://esp32.local/

image_original (25).jpg

Screenshot from 2025-01-10 04-22-40_original.jpg

Screenshot from 2025-01-10 04-23-06_original.jpg

Screenshot from 2025-01-10 04-21-29_original.jpg

プログラム





//WebServer_sw1_M5NanoC6_1


//ヘッダー
#include <WiFi.h>
#include <NetworkClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>


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

WebServer server(80);


void handleRoot() {

  //↓処理を開始

  static uint32_t msg_count = 0;

  //                       0123456789
  static char data[256] = "G1 = _ #__";

  data[5] = '0' + digitalRead(1);

  data[8] = '0' + (msg_count % 100)/10;
  data[9] = '0' + (msg_count % 100)%10;

  msg_count++;

  //↑処理が終了

  //転送
  server.send(200, "text/plain", data);

}


void handleNotFound() {

  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);

}


//初期化
void setup(void) {

  pinMode(1, INPUT); //GPIO init

  //WiFiの初期化
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  //シリアルの初期化
  Serial.begin(9600);
  Serial.println("");
  //シリアルの待ちが0.5*9
  //delay(3000);//決め打ちなので、おかしかったら調整してね! wifiの待ち0.5*6
  for (int i = 0; i < (9 + 6); i++) {
    delay(500); //接続待ち
    Serial.print(".");
  }//for

  //ローカルドメインネームサーバーの設定
  MDNS.begin("esp32"); delay(2000);//決め打ちなので、おかしかったら調整してね!

  //接続情報の表示
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("MDNS responder started");

  //割り込みに登録↓ start
  server.on("/", handleRoot);

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

  server.onNotFound(handleNotFound);
  //割り込みに登録↑ end

  server.begin(); //webサービスの初期化
  Serial.println("HTTP server started");

}//setup


//メインループ
void loop(void) {
  server.handleClient();
  delay(2);  //allow the cpu to switch to other tasks
}//loop






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