Skip to content

Instantly share code, notes, and snippets.

@mugifly
Created April 26, 2019 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mugifly/0e76507dbe447cf6a859c3076b386cb0 to your computer and use it in GitHub Desktop.
Save mugifly/0e76507dbe447cf6a859c3076b386cb0 to your computer and use it in GitHub Desktop.
ESP8266 + Triple Signal Lights
// ESP8266 + 積層信号灯 (3色)
// https://gist.github.com/mugifly/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <timer.h>
// 設定 - Wi-Fi
#define WLAN_SSID "XXXXXX"
#define WLAN_KEY "XXXXXX"
// 設定 - Wi-Fi接続状態の確認間隔
#define WLAN_CHECK_INTERVAL_MSEC 10000
// 設定 - HTTPサーバのポート
#define HTTP_PORT 80
// 設定 - 三色灯の接続ピン
#define LIGHT_RED 2
#define LIGHT_YELLOW 13
#define LIGHT_GREEN 14
// ----
// サーバのインスタンス
ESP8266WebServer httpServer(HTTP_PORT);
// タイマーのインスタンス
auto timer = timer_create_default();
void setup () {
Serial.begin(9600);
// ピンのモードを設定
pinMode(LIGHT_RED, OUTPUT);
pinMode(LIGHT_YELLOW, OUTPUT);
pinMode(LIGHT_GREEN, OUTPUT);
// Wi-Fi アクセスポイントへ接続
connectWiFi();
// サーバのエンドポイントを設定 - GET /
httpServer.on("/", []() {
httpServer.send(200, "text/plain", "ESP 積層信号灯");
});
// サーバのエンドポイントを設定 - GET /:color/:value
httpServer.on("/red/on", onHttpSwitchRequest);
httpServer.on("/red/off", onHttpSwitchRequest);
httpServer.on("/yellow/on", onHttpSwitchRequest);
httpServer.on("/yellow/off", onHttpSwitchRequest);
httpServer.on("/green/on", onHttpSwitchRequest);
httpServer.on("/green/off", onHttpSwitchRequest);
// TTTPサーバの待ち受けを開始
httpServer.begin();
// Wi-Fi の接続状態を確認するためのタイマーを設定
timer.every(WLAN_CHECK_INTERVAL_MSEC, checkWifiStatus);
}
void connectWiFi () {
// Wi-Fi アクセスポイントへ接続
Serial.print("Connecting to " + String(WLAN_SSID) + "...");
WiFi.mode(WIFI_STA);
WiFi.begin(WLAN_SSID, WLAN_KEY);
// 接続完了まで待機
int wait_counter = 0;
while (WiFi.status() != WL_CONNECTED) {
if (20 < wait_counter) { // 一定時間経過したときは
// 一旦スリープ状態へ移行させる
Serial.println("\nRetrying...");
wait_counter = 0;
ESP.deepSleep(5 * 1000 * 1000, WAKE_RF_DEFAULT);
delay(1000);
}
Serial.print(".");
wait_counter++;
delay(1000);
}
Serial.println("Done.");
// IPアドレスを表示
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
bool checkWifiStatus (void *) {
// Wi-Fi の接続状態を確認
if (WiFi.status() != WL_CONNECTED) { // 接続済みでなければ
Serial.println("Wi-Fi has been disconnected.");
connectWiFi();
}
// タイマーを継続
return true;
}
void onHttpSwitchRequest () {
// リクエストのURLを取得
const String uri = httpServer.uri();
Serial.println("Request received on " + uri);
// 制御したい色を取得
const String color = uri.substring(1, uri.indexOf('/', 1));
Serial.println("Color: " + color);
// オンにするのか・オフにするのかを取得
const String value = uri.substring(uri.lastIndexOf('/') + 1);
Serial.println("Value: " + value);
// 色に応じて制御すべきピンを判定
int light_pin = -1;
if (color == "red") {
light_pin = LIGHT_RED;
} else if (color == "yellow") {
light_pin = LIGHT_YELLOW;
} else if (color == "green") {
light_pin = LIGHT_GREEN;
}
// 出力すべき値を判定
int light_value = LOW;
if (value == "on") {
light_value = HIGH;
}
// エラー処理
if (light_pin == -1) {
httpServer.send(400, "text/plain", "Invalid Request");
return;
}
// ピンへ出力
digitalWrite(light_pin, light_value);
httpServer.send(200, "text/plain", "Turn on the " + color + " light");
}
void loop() {
timer.tick();
httpServer.handleClient();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment