Skip to content

Instantly share code, notes, and snippets.

@moyashipan
Last active February 13, 2020 16:27
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 moyashipan/23973845886c5279f522bc71eab29d91 to your computer and use it in GitHub Desktop.
Save moyashipan/23973845886c5279f522bc71eab29d91 to your computer and use it in GitHub Desktop.
距離センサーでホットカーペット上に猫がいるのを検知してIFTTTにトリガーするやつ
#include <HTTPClient.h>
const char SSID[] = "YOUR_WIFI_SSID";
const char PASSWORD[] = "YOUR_WIFI_PASSWORD";
const char IFTTT_WEBHOOK_URL_ON[] = "https://maker.ifttt.com/trigger/cat_bed_on/with/key/***************_******";
const char IFTTT_WEBHOOK_URL_OFF[] = "https://maker.ifttt.com/trigger/cat_bed_out/with/key/***************_******";
const int RECV_PIN = 34;
const int LED_PIN = 2;
const int AVE_LENGTH = 100;
bool last_has_cat = false; // ひとつ前の検出結果
bool stabled_has_cat = false; // チャタリングを無視した現在の状態
int stable_count = 0;
const int MAX_STABLE_COUNT = 3;
void setup() {
Serial.begin(115200);
while (!Serial);
pinMode(LED_PIN, OUTPUT);
WiFi.mode(WIFI_STA);
wifiDisconnect(true);
while (!wifiConnect());
Serial.println("Ready");
}
void loop() {
delay(1000);
int total = 0;
for (int i = 0; i < AVE_LENGTH; i++) {
total += receiveDistance();
}
float ave = (float)total / AVE_LENGTH;
Serial.println(ave);
bool has_cat = ave > 900; // この範囲内なら猫がいる
bool stabled_changed = false;
if (has_cat == last_has_cat) {
if (stable_count < MAX_STABLE_COUNT) {
stable_count++;
bool stabled = (stable_count == MAX_STABLE_COUNT);
stabled_changed = stabled && (has_cat != stabled_has_cat);
}
} else {
stable_count = 0;
if (has_cat) {
digitalWrite(LED_PIN, HIGH);
delay(100);
digitalWrite(LED_PIN, LOW);
delay(100);
digitalWrite(LED_PIN, HIGH);
delay(100);
digitalWrite(LED_PIN, LOW);
} else {
digitalWrite(LED_PIN, HIGH);
delay(100);
digitalWrite(LED_PIN, LOW);
}
}
last_has_cat = has_cat;
if (stabled_changed) {
Serial.println("Cat state changed");
// 送信する
Serial.println("Sending...");
post(has_cat);
digitalWrite(LED_PIN, HIGH);
delay(200);
digitalWrite(LED_PIN, LOW);
delay(100);
digitalWrite(LED_PIN, HIGH);
delay(200);
digitalWrite(LED_PIN, LOW);
Serial.println("Completed");
stabled_has_cat = has_cat;
}
}
int receiveDistance() {
int reading = analogRead(RECV_PIN);
return reading;
}
void post(bool state) {
while (!wifiConnect());
// IFTTTへ送信
String payload = String("") + "{}";
httpPostJson(state ? IFTTT_WEBHOOK_URL_ON : IFTTT_WEBHOOK_URL_OFF, payload);
}
void httpPostJson(const char url[], const String payload) {
HTTPClient client;
client.begin(url);
client.addHeader("Content-Type", "application/json");
client.POST(payload);
}
#include <WiFi.h>
bool wifiConnect() {
wifiDisconnect(true);
WiFi.begin(SSID, PASSWORD);
Serial.print("WiFi connecting");
int count = 0;
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
digitalWrite(LED_PIN, HIGH);
delay(10);
digitalWrite(LED_PIN, LOW);
delay(90);
if (++count > 100) {
wifiDisconnect(true);
WiFi.begin(SSID, PASSWORD);
Serial.println("");
Serial.print("reconnecting");
count = 0;
}
}
Serial.println(" connected");
return true;
}
bool wifiDisconnect(bool val) {
WiFi.disconnect(val);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment