#include <Arduino.h>
#include <ArduinoJson.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <M5Stack.h>
// Wifi Settings
#define WIFI_SSID "[ご自身の環境のWifiSSID]"
#define WIFI_PASS "[ご自身の環境のWifiPassowrd]"
// Slack OAuth Access Token
#define TOKEN "Bearer [ご自身のSlackAppのxoxp-で始まるToken]"
#define SLACK_ENDPOINT "https://slack.com/api/users.profile.set"
const char* ca = \
"-----BEGIN CERTIFICATE-----\n" \
"MII...\n" \ // CAの中身については、M5Stack/M5StickのBasicHTTPClientスケッチ等から取得下さい
"-----END CERTIFICATE-----\n";
WiFiMulti wifiMulti;
HTTPClient http;
char buffer[255];
void InitializeLcd() {
// text print
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 10);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setTextSize(1);
// Menu
M5.Lcd.printf("PressA: Working\n");
M5.Lcd.printf("PressB: CoffeeBreak\n");
M5.Lcd.printf("PressC: MTG\n");
}
void UpdateSlackStatus(String text, String emoji) {
// CLS
InitializeLcd();
// wait for WiFi connection
if((wifiMulti.run() == WL_CONNECTED)) {
HTTPClient http;
//HTTPS
http.begin(SLACK_ENDPOINT, ca);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", TOKEN);
// create JSON
DynamicJsonDocument doc(1024);
JsonObject profile = doc.createNestedObject("profile");
profile["status_text"] = text;
profile["status_emoji"] = emoji;
profile["status_expiration"] = "0";
serializeJson(doc, buffer, sizeof(buffer));
// start connection and send HTTP header
int httpCode = http.POST((uint8_t*)buffer, strlen(buffer));
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
M5.Lcd.printf("[HTTP] GET... code: %d\n", httpCode);
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
M5.Lcd.println(payload);
}
} else {
M5.Lcd.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
}
// the setup routine runs once when M5Stack starts up
void setup() {
// initialize the M5Stack object
M5.begin();
dacWrite(25, 0); // Speaker Off
InitializeLcd();
/*
Power chip connected to gpio21, gpio22, I2C device
Set battery charging voltage and current
If used battery, please call this function in your project
*/
M5.Power.begin();
// WifiSetup
wifiMulti.addAP(WIFI_SSID, WIFI_PASS);
}
// the loop routine runs over and over again forever
void loop(){
// update button state
M5.update();
// Button Pressed
if (M5.BtnA.wasReleased()) {
M5.Lcd.print('Working');
UpdateSlackStatus("Working", ":floppy_disk:");
} else if (M5.BtnB.wasReleased()) {
M5.Lcd.print('CoffeeBreak');
UpdateSlackStatus("CoffeeBreak", ":java:");
} else if (M5.BtnC.wasReleased()) {
M5.Lcd.print('MTG');
UpdateSlackStatus("Meating", ":writing_hand:");
}
}