Created
October 24, 2018 09:42
-
-
Save deviceplususer/ac68b934cefd793ac1a92267043ae006 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include | |
#include | |
#include | |
const char *ssid = "WiFiのSSID"; | |
const char *pass = "WiFiのパスワード"; | |
IPAddress ip(子機に割り当てるIPアドレス); | |
IPAddress gateway(デフォルトゲートウェイのIPアドレス); | |
const char* stop_url = "http://本体のIPアドレス/stop"; | |
#define SWITCH 4 | |
#define LED 13 | |
bool is_alarm = false; | |
WebServer server(80); | |
// 親機のアラームがオンになったときの処理 | |
void handleAlarm() { | |
is_alarm = true; | |
digitalWrite(LED, HIGH); | |
Serial.println("Alarm started"); | |
server.send(200, "text/plain", "Alarm started"); | |
} | |
// 無効なURLが指定された場合 | |
void handleNotFound() { | |
String message = "Not Found : "; | |
message += server.uri(); | |
server.send(404, "text/plain", message); | |
} | |
void setup() { | |
Serial.begin(115200); | |
// GPIOの設定 | |
pinMode(SWITCH, INPUT_PULLDOWN); | |
pinMode(LED, OUTPUT); | |
digitalWrite(LED, LOW); | |
// WiFi接続 | |
WiFi.begin(ssid, pass); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
WiFi.config(ip, gateway, WiFi.subnetMask(), IPAddress(8, 8, 8, 8), IPAddress(8, 8, 4, 4)); | |
Serial.println(""); | |
Serial.println("WiFi Connected."); | |
// WebServerの初期化 | |
server.on("/alarm", handleAlarm); | |
server.onNotFound(handleNotFound); | |
server.begin(); | |
} | |
void loop() { | |
int sw; | |
// Webサーバーを動作させる | |
server.handleClient(); | |
// 親機のアラームが鳴っている場合、スイッチが押されたかどうかを判断して、 | |
// スイッチが押されたら親機のアラームを止める | |
if (is_alarm) { | |
sw = digitalRead(SWITCH); | |
if (sw == 1) { | |
Serial.println("Button pushed"); | |
HTTPClient http; | |
http.begin(stop_url); | |
digitalWrite(LED, LOW); | |
int httpCode = http.GET(); | |
delay(200); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment