Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

M5stackをweb鯖化してMCP23017への命令を遠隔で行えるようにする

shuzo-kino.hateblo.jp
の続き。
M5stackと、それにつないだMCP23017とでLEDをブラウザ越しにいじったりできるようにする。

実際のところ

#include <M5Stack.h>
#include "Wire.h"
#include  <WiFi.h>
#include  <WiFiClient.h>
#include  <WebServer.h>
#include  <ESPmDNS.h>


const char* ssid = "###";
const char* password = "###";

WebServer server(80);

void handleRoot() {
    //  LED の制御
    M5.Lcd.print("handleLED: ");
    if (server.method() == HTTP_POST) {
       String val = server.arg("led");
       ledBup(val.toInt());
    }
String mes = "\
<html lang=\"ja\">\n\
<meta charset=\"utf-8\">\n\
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n\
<head>\n\
 <title>webLedTest</title>\n\
</head>\n\
<body style=\"font-family: sans-serif; background-color: #ffeeaa ;\" >\n\
 <h1>webLedTest</h1>\n\
    <p>\n\
    <form action='' method='post'>\n\
      <button name='led' value='255'>ALL</button>\n\
    </form>\n\
    <form action='' method='post'>\n\
      <button name='led' value='10'>8+2</button>\n\
    </form>\n\
    <form action='' method='post'>\n\
      <button name='led' value='8'>8</button>\n\
    </form>\n\
    <form action='' method='post'>\n\
      <button name='led' value='4'>4</button>\n\
    </form>\n\
    <form action='' method='post'>\n\
      <button name='led' value='2'>2</button>\n\
    </form>\n\
    <form action='' method='post'>\n\
      <button name='led' value='1'>1</button>\n\
    </form>\n\
    <form action='' method='post'>\n\
      <button name='led' value='0'>0</button>\n\
    </form>\n\
  </p>\n\
</body>\n\
</html>\n";

 server.send(200, "text/html", mes);
}

void ledBup(int val) {
  Wire.beginTransmission(0x20);
  Wire.write(0x13); // address bank B
  Wire.write(val); // except B0, all Bs HIGH
  Wire.endTransmission();
  delay(500);
}

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

    M5.begin();  //Init M5Core.
    M5.Power.begin(); //Init Power module.
    M5.Lcd.setBrightness(200);
    M5.Lcd.setCursor(10, 10); //文字表示の左上位置を設定
    M5.Lcd.setTextColor(RED); //文字色設定(背景は透明)(WHITE, BLACK, RED, GREEN, BLUE, YELLOW..)
    M5.Lcd.setTextSize(2);//文字の大きさを設定(1~7)

    Wire.begin(); // wake up I2C bus
    // set I/O pins to outputs
    Wire.beginTransmission(0x20);
    Wire.write(0x00); // IODIRA register
    Wire.write(0x00); // set all of port A to outputs
    Wire.write(0x00); // set all of port B to outputs
    Wire.endTransmission();

    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);

    // Wait for connection
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        M5.Lcd.print(".");
    }

    M5.Lcd.print(ssid);
    M5.Lcd.print("\n");
    M5.Lcd.print(WiFi.localIP());
    M5.Lcd.print("\n");

    if (MDNS.begin("esp32led")) {
        M5.Lcd.print("MDNS responder started");
        M5.Lcd.print("\n");
    }

    server.on("/", handleRoot);
    server.on("/inline", []() {
        server.send(200, "text/plain", "hello from esp8266!");
    });
    server.onNotFound(handleNotFound);
    server.begin();

    M5.Lcd.setCursor(10, 10); //文字表示の左上位置を設定
    M5.Lcd.print("HTTP server started");
    M5.Lcd.print("\n");

}


void loop(void) {

    server.handleClient();
}