Skip to content

Instantly share code, notes, and snippets.

@ebith

ebith/main.cpp

Created Jun 7, 2020
Embed
What would you like to do?
ATOM Liteでhttp postしてwebhookを叩いて通知を飛ばすやつ
#define FASTLED_INTERNAL
#include <M5Atom.h>
#include <WiFi.h>
#include <ArduinoJson.h>
#include <HttpClient.h>
const int pin = 32;
const char* ssid = "**********";
const char* password = "**********";
const char* url = "https://discordapp.com/api/webhooks/**********";
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);
Serial.println(http.getString());
http.end();
}
void setup() {
M5.begin(true, false, true);
pinMode(pin, INPUT_PULLUP);
Serial.println();
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
if (M5.Btn.wasPressed()) {
notify(url, "<@**********> リビングで呼んでるぞ");
} else if (digitalRead(pin) == LOW) {
notify(url, "<@**********> ピンポン鳴ったぞ");
}
delay(50);
M5.update();
} else {
M5.dis.drawpix(0, 0x001000);
WiFi.begin(ssid, password);
int wifiReconnectCount = 0;
while (WiFi.status() != WL_CONNECTED) {
if (wifiReconnectCount >= 300) { ESP.restart(); }
wifiReconnectCount++;
delay(100);
}
Serial.println(WiFi.localIP());
M5.dis.drawpix(0, 0x100000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.