Send Message to Slack Channel using Incoming Webhooks and ESP8266 NodeMCU

Send Message to Slack Channel using Incoming Webhooks and ESP8266 NodeMCU

Slack is a messaging service that allows to communicate with team members. Slack allows creating channels where a team members can send messages and share files. Incoming Webhooks is one of the ways to send messages from a custom application to a Slack channel.

This tutorial shows how to send a message to a Slack channel using Incoming Webhooks and ESP8266 NodeMCU development board.

Components

No.ComponentQuantity
1.ESP8266 NodeMCU + Micro USB cable1

Incoming Webhooks integration

In order to use the Incoming Webhooks, we need to create a Slack app. Follow these steps:

  1. Sign in to Slack and create an app.
  2. Provide an app name and choose a workspace to associate with the app. Click "Create App".
  3. Under the section of "Add features and functionality" click "Incoming Webhooks".
  4. In the next screen, turn on the switch to activate Incoming Webhooks.
  5. Then extra options will appear. Click "Add New Webhook to Workspace".
  6. In the new screen, select a channel and click "Allow". You will be redirected back to Incoming Webhooks settings.
Incoming Webhook URL for ESP8266 NodeMCU
  1. You can see a Webhook URL which look something like this:
https://hooks.slack.com/services/TXXXXXXXXXX/BXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX
  1. Copy a Webhook URL. It will be used to send a message to a channel that selected in the setup. In this case, general channel has been selected.

Get fingerprint of an SSL certificate

In order to send a GET request using HTTPS protocol from ESP8266 NodeMCU to the server, we need to get the fingerprint of an SSL certificate. If you use Chrome browser, then follow these steps:

  1. Open a browser and enter the URL address of a Slack website.
  2. Click on the lock icon in the address bar and choose "Certificate" option.
Get Slack Site's Fingerprint for ESP8266 NodeMCU (Step 1)
  1. In the new window, select the "Details" tab and find "Thumbprint" field in the list. Copy the thumbprint. It will be used in the code.
Get Slack Site's Fingerprint for ESP8266 NodeMCU (Step 2)

Note: fingerprint should be updated when the certificate expires.

Code

We define constants to store Wi-Fi network SSID and password, a Webhook URL, and fingerprint of an SSL certificate.

In the setup function, serial communication is initialized. We set the station (STA) mode in order to connect to a Wi-Fi network.

In the loop function, we generate the random number from 1 to 100 (inclusive). Messages to a Slack channel are send in a JSON format using POST requests.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char *WIFI_SSID = "YOUR WIFI NETWORK NAME";
const char *WIFI_PASSWORD = "YOUR WIFI PASSWORD";

const char *URL = "https://hooks.slack.com/services/XX/XX/XX";
const char *FINGERPRINT = "71f8c380fe88b8ab308b3fe15eb8f6769878cc30";

WiFiClientSecure client;
HTTPClient httpsClient;

void setup()
{
    Serial.begin(9600);

    WiFi.mode(WIFI_STA);

    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("Connected");

    client.setFingerprint(FINGERPRINT);
}

void loop()
{
    int value = random(1, 101);
    String data = "{\"text\":\"" + String(value) + "\"}";

    httpsClient.begin(client, URL);
    httpsClient.addHeader("Content-Type", "application/json");
    httpsClient.POST(data);
    httpsClient.end();

    delay(5000);
}

Testing

Open a Slack channel to see messages posted by ESP8266 NodeMCU board.

Messages Posted by ESP8266 NodeMCU to Slack Channel

Leave a Comment

Cancel reply

Your email address will not be published.