WedServerの接点情報でWebClientのリレーを動かす。


参考




https://qiita.com/caa45040/items/5b6e969820e3a24dbb20


https://qiita.com/caa45040/items/1fbcc5de22277b651b1a


https://qiita.com/caa45040/items/cc179c3788341467dafb


x 過去ログを見よ!!
x 3.0.7
x [ツール][シリアルモニタ]を何度か閉じたり開いたりする。
x IPアドレスも都合よく書き換える



ここを修正する

```c++

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

```

結果

![o_coq621.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/ba52e115-9c35-545e-22b9-a1936ea82783.jpeg)


![o_coq618.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/33c7a8ac-dda1-e3ad-ea23-69b447027fb9.jpeg)


![o_coq619.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/5eb6d7c8-ff23-c8d2-c27c-87d903402dba.jpeg)

![o_coq620.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/64d951a3-e23d-69bc-8619-5ba53e13b06c.jpeg)



プログラム

(WedServer接点情報 NanoC6)

```c++


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

const int led = 7;

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


  //↓接点の情報を開始
  static char data[256+1];  
  static uint32_t msg_count = 0;

  int inJ = digitalRead(1);

  char *strJ;
  if(inJ == 0){
    strJ = "Button J was released";
  }else{
    strJ = "Button J was pressed";
  }

  snprintf(data, sizeof(data), "(%d) %s  #%lu",inJ ,strJ , msg_count++);

  //↑接点の情報が終了


  server.send(200, "text/plain", data);
  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);
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  for(int i=0;i<10;i++){
    delay(500);
    Serial.print(".");
  }

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


```

(WebClientリレー駆動 StampS3)

```c++


/**
 * BasicHTTPClient.ino
 *
 *  Created on: 24.05.2015
 *
 */

#include <Arduino.h>

#include <WiFi.h>
#include <WiFiMulti.h>

#include <HTTPClient.h>

#define USE_SERIAL Serial

WiFiMulti wifiMulti;

/*
const char* ca = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" \
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" \
"DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" \
"SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" \
"GkxldCdzIEVuY3J5cHQgQXV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h9KEik3JHRRHGJo\n" \
"uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n" \
"wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n" \
"X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n" \
"PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n" \
"KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n" \
"-----END CERTIFICATE-----\n";
*/

void setup() {

  pinMode(15, OUTPUT);//ブザーのポート設定
  digitalWrite(15, LOW);//ブザーのオフ

  USE_SERIAL.begin(115200);

  USE_SERIAL.println();
  USE_SERIAL.println();
  USE_SERIAL.println();

  for (uint8_t t = 4; t > 0; t--) {
    USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
    USE_SERIAL.flush();
    delay(1000);
  }

  const char *ssid = "ご自宅のSSID";
  const char *password = "ご自宅のパスワード";
  wifiMulti.addAP(ssid, password);
}


int st_cmp(char *a,char *b){

    //char *a = "ABC";
    //char *b = "0123456ABC012";
    
    char v,w;
    
    int j = 0;
    while(1){
        
        int i = 0;
        while(1){
            v = a[i];
            w = b[i+j];
            if(v == 0 || w == 0){
                break;
            }else if(v != w){
                break; 
            }//endif
            i++;
        }//while i

        if(v == 0) {
            //printf("true\n");break;
            return(j);
        } else if( w == 0){
            //printf("false\n");break;
            return(0);
        }//endif
        
        j++;
    }//while j
}//st_cmp


void loop() {
  // wait for WiFi connection
  if ((wifiMulti.run() == WL_CONNECTED)) {

    HTTPClient http;

    USE_SERIAL.print("[HTTP] begin...\n");
    // configure traged server and url
    //http.begin("https://www.howsmyssl.com/a/check", ca); //HTTPS
    //http.begin("http://example.com/index.html");  //HTTP
    http.begin("http://192.168.0.196/");  //HTTP


    USE_SERIAL.print("[HTTP] GET...\n");
    // start connection and send HTTP header
    int httpCode = http.GET();

    // httpCode will be negative on error
    if (httpCode > 0) {
      // HTTP header has been send and Server response header has been handled
      USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

      // file found at server
      if (httpCode == HTTP_CODE_OK) {
        USE_SERIAL.println("S##########");
          
        String payload = http.getString();
        USE_SERIAL.println(payload);

        USE_SERIAL.println("E##########");


        //USE_SERIAL.println(payload.c_str());

    //ブザーの処理
    if(st_cmp("Button J was pressed",(char *)payload.c_str()) != 0){
      digitalWrite(15, HIGH);
    }else if(st_cmp("Button J was released",(char *)payload.c_str()) != 0){
      digitalWrite(15, LOW);
    } //else {
      //digitalWrite(15, LOW);
    //}//endif

      }
    } else {
      USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
    }

    http.end();
  }

  delay(5000);
}



```