(増量中)M5NanoC6、WebServerのG1とG2接点情報の表示


(増量中)M5NanoC6、WebServerのG1とG2接点情報の表示


Arduino Grove M5stack m5nanoC6


x 過去ログを見よ!!
x 3.0.7
x なぜか、なんかいかモニターを開くと成功する
x なぜかスタックのセグメントエラー(アクセス範囲特権例外)が出て、
小一時間、葛藤して、HTMLのホームページのデータを関数の外の
グローバル変数にしてあげたら、おさまった。(良かったです。)


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


```c++

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


```

結果


![o_coq632.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/07ee6b2f-0321-4d11-536b-fa383f16aa97.jpeg)

![o_coq631.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/37c957f5-cffa-4a9a-c70a-402bea542aa0.jpeg)

![o_coq633.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/0f190edc-9050-6e4f-e319-f5c2dbbb95a2.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 st_okikae(char *s,char *c,char *a,char *b){
    //char *s = "Alice";  //サーチ
    //char *c = "Bomb";  //チェンジ
    //char *a = "I Love Alice."; //全体
    //char b[16] = {0};  //一時領域

    char v,w; //文字の一時

    int k = 0;
    while(s[k++]){/*空文*/}

    int j = 0;
    while(1){
        
        int i = 0;
        while(1){
            v = s[i];
            w = a[i+j];
            if(v == 0 || w == 0){
                break;
            }else if(v != w){
                break; 
            }//endif
            i++;
        }//while i

        if(v == 0) {
            break; //true
        } else if( w == 0){
            break; //false
        }//endif
        
        b[j] = a[j];
        j++;
    }//while j
    k = (k-1) + j;

    b[j] = 0;

    if( v == 0 ) {

        int i = 0;
        while( (b[j] = c[i]) ){j++;i++;}

        while( (b[j] = a[k]) ){j++;k++;}

    }//endif
}//st_okikae


char html_tmp_G1[4096] = {0};
char html_tmp_G2[4096] = {0};
char html[] = {
    "                                \n"
    "<HTML lang=\"jp\">              \n"
    "                                \n"
    "<HEAD>                          \n"
    "                                \n"
    "  <meta charset=\"UTF-8\">      \n"
    "                                \n"
    "  <TITLE>ようこそ</TITLE>        \n"
    "                                \n"
    "</HEAD>                         \n"
    "                                \n"
    "<BODY>                          \n"
    "                                \n"
    "  <H1>M5NanoC6の接点情報</H1>    \n"
    "                                \n"
    "  <H2>GPIO-G1</H2>              \n"
    "                                \n"
    "  AAA                           \n"
    "                                \n"
    "  <H2>GPIO-G2</H2>              \n"
    "                                \n"
    "  BBB                           \n"
    "                                \n"
    "                                \n"
    "  <H3>適度に更新してください</H3>  \n"
    "                                \n"
    "</BODY>                         \n"
    "                                \n"
    "</HTML>                         \n"
    "                                \n"
    "                                \n"
};

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

  //↓接点の情報を開始

  //G1
  if(digitalRead(1) == 0){
    st_okikae("AAA","(0) Button G1 was released",html,html_tmp_G1);
  }else{
    st_okikae("AAA","(1) Button G1 was pressed",html,html_tmp_G1);
  }

  //G2
  if(digitalRead(2) == 0){
    st_okikae("BBB","(0) Button G2 was released",html_tmp_G1,html_tmp_G2);
  }else{
    st_okikae("BBB","(1) Button G2 was pressed",html_tmp_G1,html_tmp_G2);
  }
  
  //↑接点の情報が終了

  server.send(200, "text/html", html_tmp_G2);
  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);

  pinMode(1, INPUT);
  pinMode(2, INPUT);

  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
}


```