Last active
March 1, 2022 04:18
-
-
Save ebith/11ef21603e4dabb989705989239a14e5 to your computer and use it in GitHub Desktop.
M5 ATOM(ESP32)でインターホンが押されたらDiscordに通知を飛ばすやつ
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
#define FASTLED_INTERNAL | |
#include <Arduino.h> | |
#include <WiFi.h> | |
#include <HttpClient.h> | |
#include <FastLED.h> | |
#include <ArduinoJson.h> | |
const int NUM_LEDS = 1; | |
const int LED_PIN = 27; | |
static CRGB leds[NUM_LEDS]; | |
const int BUTTON_PIN = 39; | |
bool BUTTON_STATE = false; | |
const int DOORBELL_PIN = 32; | |
const int interval = 30 * 1000; | |
unsigned long prev = 0; | |
void setup() { | |
pinMode(DOORBELL_PIN, INPUT_PULLUP); | |
// LED | |
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); | |
FastLED.setBrightness(1); | |
leds[0] = CRGB::Black; | |
FastLED.show(); | |
// Button | |
pinMode(BUTTON_PIN, INPUT_PULLUP); | |
} | |
void notify(const char* url, const char* content) { | |
DynamicJsonDocument doc(JSON_OBJECT_SIZE(3)); | |
doc["username"] = "Doorbell"; | |
doc["avatar_url"] = "https://i.imgur.com/6YSCfLa.png"; | |
doc["content"] = content; | |
String json; | |
serializeJson(doc, json); | |
HTTPClient http; | |
http.begin(url); | |
http.addHeader("Content-Type", "application/json"); | |
http.POST(json); | |
http.end(); | |
} | |
void loop() { | |
unsigned long time = millis(); | |
if (WiFi.status() == WL_CONNECTED) { | |
if (digitalRead(BUTTON_PIN) == LOW && !BUTTON_STATE) { | |
notify(DISCORD_WEBHOOK_URL, "<@" DISCORD_USER_ID "> リビングで呼んでるぞ"); | |
BUTTON_STATE = true; | |
} else if (digitalRead(BUTTON_PIN) == HIGH && BUTTON_STATE) { | |
BUTTON_STATE = false; | |
} else if (digitalRead(DOORBELL_PIN) == LOW) { | |
delay(30); | |
if (digitalRead(DOORBELL_PIN) == LOW) { | |
if ((time - prev) > interval) { | |
notify(DISCORD_WEBHOOK_URL, "<@" DISCORD_USER_ID "> ピンポン鳴ったぞ"); | |
prev = millis(); | |
} | |
} | |
} | |
} else { | |
leds[0] = CRGB::Red; | |
FastLED.show(); | |
WiFi.begin(WIFI_SSID, WIFI_PASS); | |
int wifiReconnectCount = 0; | |
while (WiFi.status() != WL_CONNECTED) { | |
if (wifiReconnectCount >= 200) { ESP.restart(); } | |
wifiReconnectCount++; | |
delay(100); | |
} | |
leds[0] = CRGB::Green; | |
FastLED.show(); | |
} | |
} |
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
; PlatformIO Project Configuration File | |
; | |
; Build options: build flags, source filter | |
; Upload options: custom upload port, speed and extra flags | |
; Library options: dependencies, extra library storages | |
; Advanced options: extra scripting | |
; | |
; Please visit documentation for the other options and examples | |
; https://docs.platformio.org/page/projectconf.html | |
[env:m5stick-c] | |
platform = espressif32 | |
board = m5stick-c | |
framework = arduino | |
monitor_speed = 115200 | |
board_build.f_cpu = 80000000L | |
lib_deps = | |
bblanchon/ArduinoJson@^6.17.2 | |
fastled/FastLED@^3.3.3 | |
build_flags = | |
-D WIFI_SSID='"${sysenv.WIFI_SSID}"' | |
-D WIFI_PASS='"${sysenv.WIFI_PASS}"' | |
-D DISCORD_WEBHOOK_URL='"${sysenv.DISCORD_WEBHOOK_URL}"' | |
-D DISCORD_USER_ID='"${sysenv.DISCORD_USER_ID}"' |
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
$env:WIFI_SSID = "**********" | |
$env:WIFI_PASS = "**********" | |
$env:DISCORD_WEBHOOK_URL = "https://discordapp.com/api/webhooks/**********" | |
$env:DISCORD_USER_ID = "**********" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment