IoT回転灯プロジェクト Lチカ程度のwebも作っちゃう

さて皆さん、と、前回はLチカレベルまで終了したので今回は参照するWebページでon/offをしたいと思います。

ざっくり、動きを説明すると…
GETでlightを指定するとファイルlightに書き込みます。

lightファイルは回転灯が監視しているので1が入ると光ります。

<?php
  if($_GET)file_put_contents("light",$_GET['light']);
  $light = file_get_contents("light");
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
<body>
  <form method="GET">
    <select name="light">
      <option name="1" value="1" <?php if($light == "1") echo "selected";?>>ON</option>
      <option name="0" value="0" <?php if($light == "0") echo "selected";?>>OFF</option>
    </select>
    <input type="submit" value="変更" />
  <form>
</body>
</html>

はい、超シンプルです。

次は… 実際の利用イメージとして、新着が来ると光るって感じですね。
1.最新の情報を監視して、差分が発生すると回転灯を光らせる…
2.Webでチェックを外すと(既読)回転が止まる
3.回転灯にボタンを付けて、確認ボタンでwebに確認パケットを投げる。
ここまで出来ればHappyだな。

これが、今回のプロジェクトのゴールだ!
量産型を作るかをここで悩む…

たぶん前回と変わった部分があるのでarduinoのソースも書いておきます。

     // Content-Type: text/plain
  String conent = (text.substring(text.lastIndexOf("Content-Type") + 24 + 2));
  Serial.print(conent);
  if(conent.equals("1")){
    turnOn = true;
  }

これかな、ポイントは、GETで持ってきたhttpにはヘッダーが入っているので本文部分を抽出するのですが、”Content-Type”を決め打ちにしています。
24 + 2の部分は環境により調整が必要です。
ファイル内容が抽出できれば、簡単です。
1だったら光らすフラグを上げます。
フラグはダサいと思われますが… ま、回転灯が点灯している段階で瞬断で回転するのを避けるためにフラグを使っています。

#include <ESP8266WiFi.h>
#define LIGHT_PIN 13

const char* ssid     = "........";
const char* password = "........";
const char* host = "example.localhost";
void setup() {
  Serial.begin(115200);
  delay(10);
  // 回転灯の制御pinを出力にする。
  pinMode(LIGHT_PIN, OUTPUT);
  
  // We start by connecting to a WiFi network
  Serial.println("\n");
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // 回転灯を点灯させるかの判断フラグ
  boolean turnOn = false;

  Serial.print("connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect(host, 80)) {
    Serial.println("connection failed");
    return;
  }
  
  // We now create a URI for the request
  String url = "/light";
  
  Serial.print("Requesting URL: ");
  Serial.println(url);
  
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
  
  // Read all the lines of the reply from server and print them to Serial
  String text = "";
  while(client.available()){
    String line = client.readStringUntil('\r');
    text += line;
  }
     // Content-Type: text/plain
  String conent = (text.substring(text.lastIndexOf("Content-Type") + 24 + 2));
  Serial.print(conent);
  if(conent.equals("1")){
    turnOn = true;
  }
    Serial.println("closing connection");
  if(turnOn){
    // 点灯処理
  Serial.println("turn on");
    digitalWrite(LIGHT_PIN, HIGH);
  }else{
    // 点灯処理    
    Serial.println("turn off");
    digitalWrite(LIGHT_PIN, LOW);
  }
  // スリープ
    delay(10000);

}