Skip to content

Instantly share code, notes, and snippets.

@deviceplususer
Created October 25, 2018 01:39
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 deviceplususer/a1d3f5c5ceecb05e6bbbfef917c92e62 to your computer and use it in GitHub Desktop.
Save deviceplususer/a1d3f5c5ceecb05e6bbbfef917c92e62 to your computer and use it in GitHub Desktop.
#include <WiFi.h>
#include <WebServer.h>
#include <HTTPClient.h>
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