Created
August 19, 2020 12:01
ESP32 code to change Slack status with a button
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
#include <ArduinoJson.h> | |
#include <WiFi.h> | |
#include <HTTPClient.h> | |
const char* ssid = "your-wifi-name"; | |
const char* password = "your-wifi-password"; | |
const char* slackToken = "Bearer your-slack-app-token"; | |
const char* serverName = "https://slack.com/api/users.profile.set"; | |
const int PushButton = 34; | |
void setup() { | |
Serial.begin(115200); | |
pinMode(PushButton, INPUT); | |
WiFi.begin(ssid, password); | |
Serial.println("Connecting"); | |
while(WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("Connected to WiFi network with IP Address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void loop() { | |
int button_state = digitalRead(PushButton); | |
if ( button_state == HIGH ) { | |
//Check WiFi connection status | |
if(WiFi.status()== WL_CONNECTED){ | |
HTTPClient http; | |
http.begin(serverName); | |
http.addHeader("Host", "slack.com"); | |
http.addHeader("Content-Type", "application/json; charset=utf-8"); | |
http.addHeader("Authorization", slackToken); | |
DynamicJsonDocument doc(1024); | |
JsonObject profile = doc.createNestedObject("profile"); | |
profile["status_text"] = "Lunch time!"; | |
profile["status_emoji"] = ":sandwich:"; | |
String requestBody; | |
serializeJson(doc, requestBody); | |
int httpResponseCode = http.POST(requestBody); | |
Serial.print("HTTP Response code: "); | |
Serial.println(httpResponseCode); | |
Serial.println("Slack status updated to Lunch time!"); | |
http.end(); | |
} | |
else { | |
Serial.println("WiFi Disconnected"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment