Skip to main content
ESPBoards

Send Messages from ESP32 to Discord Channel with Webhooks

Send messages from esp32 micro-controller to your Discord channel, using Discord Webhooks. Send simple messages or formatted messages using Discord Embeds.


Discord has evolved far beyond its gaming roots and simple chatting. Emerging as a dynamic platform for communities of all kinds to connect, collaborate, and communicate in real-time together with bots, makes it an invaluable tool for fostering engagement and building communities around shared interests.

Now, imagine if your ESP32, with its capabilities to interact with the physical world, could seamlessly communicate with Discord. This opens up a world of possibilities. Suddenly, you can receive notifications, and updates, or even control your ESP32-powered devices directly from Discord.

In this post, we'll explore how to make this a reality, walking through the process of setting up your ESP32 to send messages to Discord servers.

Setting up Discord Bot #

Following the instructions in Discord's support page "Intro to Webhooks", create a new Webhook in your Discord Chanel.

Next to the channel name, click the settings cog

Select "Integrations", and click on "Webhooks" and "New Hook". A new Hook should appear below.

Click on it and copy the URL by clicking the "Copy Webhook URL" button.

Keep this URL by hand, as you will need it in a moment.

Preparing the ESP32 #

You can find a full code example in the GitHub Repository.

Create a folder named discord_message. This will be the main Arduino project's directory.

Secrets #

Create secrets.h file in the root project directory. Copy the following code:

#define WIFI_SSID "<REPLACE_WITH_YOUR_WIFI_SSID>"
#define WIFI_PASS "<REPLACE_WITH_YOUR_WIFI_PASSWORD>"
#define DISCORD_WEBHOOK "<REPLACE_WITH_DISCORD_WEBHOOK_URL>"
#define DISCORD_TTS "false"
  1. Fill in your WiFi Credentials in WIFI_SSID and WIFI_PASS
  2. Paste the Discord Webhook URL you copied earlier. It should be in this format: https://discord.com/api/webhooks/xxx/zzz
  3. If you want to enable Discord's text-to-speech for the messages, everyone who has tts enabled will hear it.

Discord App SSL/TLS Certificate #

Create discordCert.h file in the root project directory and copy the following code:

/*
* Get current discordapp.com certificate:
* openssl s_client -showcerts -connect discordapp.com:443
*/

const char* DISCORD_CERT = R"(
-----BEGIN CERTIFICATE-----
MIIDzTCCArWgAwIBAgIQCjeHZF5ftIwiTv0b7RQMPDANBgkqhkiG9w0BAQsFADBa
MQswCQYDVQQG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D7fa86JXWGBPzI6UAPxGezQxPk1H
goE6y/SJXQ7vTQ1unBuCJN0yJV0ReFEQPaA1IwQvZW+cwdFD19Ae8zFnWSfda9J1
CZMRJCQUzym+5iPDuI9yP+kHyCREU3qzuWFloUwOxkgAyXVjBYdwRVKD05WdRerw
6DEdfgkfCv4+3ao8XnTSrLE=
-----END CERTIFICATE-----
)";

Please note this is discordapp.com certificate at the time of writing this post. If the certificate is changed in the meantime, you can get a new certificate with this command:

openssl s_client -showcerts -connect discordapp.com:443

ESP32 Code to Send a Message to Discord #

Create discord.h file in the root directory of your project. Copy the following code:

#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include "discordCert.h"

const char ssid[] = WIFI_SSID;
const char pass[] = WIFI_PASS;
const String discord_webhook = DISCORD_WEBHOOK;
const String discord_tts = DISCORD_TTS;

WiFiMulti WiFiMulti;

void connectWIFI() {
WiFiMulti.addAP(ssid, pass);
WiFi.mode(WIFI_STA);
while ((WiFiMulti.run() != WL_CONNECTED)) {
Serial.print(".");
}
Serial.println("WiFi Connected");
}

void sendDiscord(String content, String embedJson) {
WiFiClientSecure *client = new WiFiClientSecure;
if (client) {
client -> setCACert(DISCORD_CERT);
{
HTTPClient https;
if (https.begin(*client, discord_webhook)) {
https.addHeader("Content-Type", "application/json");

String jsonPayload = "{\"content\":\"" + content + "\",\"tts\":" + discord_tts + ",\"embeds\": [" + embedJson + "]}";
int httpCode = https.POST(jsonPayload);

if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = https.getString();
Serial.print("HTTP Response: ");
Serial.println(payload);
}
} else {
Serial.print("HTTP Post failed: ");
Serial.println(https.errorToString(httpCode).c_str());
}

https.end();
}
}

delete client;
}
}

void sendDiscordMessage(String content) {
sendDiscord(content, "");
}

void sendDiscordEmbeds(String embeds) {
sendDiscord("", embeds);
}

This code provides 3 functions:

  • sendDiscordMessage(String content): Send a simple message to Discord
  • sendDiscordEmbeds(String embeds): Send a message with Embeds to Discord
  • sendDiscord(String content, String embedJson): Send a simple text and embeds in one message

The main sendDiscord() function sets a Discord app certificate to WiFiClientSecure, sets the Discord message content and finally sends the message to the Discord Webhook.

Send the Messsage from ESP32 to your Discord Channel #

Create discord_message.ino file in the root directory of your project. Add the following code:

#include "secrets.h"
#include "discord.h"

String message = "";
String embedJson = "";

void setup() {
Serial.begin(9600);
connectWIFI();
// sendDiscordMessage(message);
// sendDiscordEmbeds(embedJson);
// sendDiscord(message, embedJson);
}

void loop() {
}

As you can see, this is the main Arduino project's file. It simply begins the Serial connection and connects the ESP32 board to a WiFi network. If you uncomment any of the commented lines, it will send a message to Discord on ESP32 setup - after it boots and connects to the WiFi network. To resend the message again with this example, you can either reboot the ESP32 or move the sendDiscord...() function to loop() or the appropriate part of your application's code.

Simple Discord Message from ESP32 #

To send a simple message to Discord Channel, uncomment the sendDiscordMessage(message); line and set the message variable to any message you want to send to Discord.

- // sendDiscordMessage(message);
+ sendDiscordMessage(message);

For example, if we set the message as follows:

String message = "Hello from ESP32!";

And upload the code, after the ESP32 boots and successfully connects to the WiFi network, it will send a message to the Discord Webhook. After a few seconds, you should see the message in Discord:

Discord Embeds with ESP32 #

Discord embeds are a way to enhance the appearance and interactivity of messages sent in Discord. They allow users to include rich media, such as images, videos, and formatted text, in their messages. Embeds can be used to display information in a visually appealing format.

embedJson is an array of embeds and can contain up to 10 embeds in one message.

For example, if we set the embedJson as follows:

String embedJson = R"({
"author": {
"name": "Temperature Sensor",
"icon_url": "https://picsum.photos/200"
},
"title": ":thermometer: Temperature too high! **28.32°C**",
"color": 16711680,
"footer": {
"text": "",
"icon_url": "https://picsum.photos/200"
}
})";

Uncomment the line sendDiscordEmbeds(embedJson); and upload the code.

- // sendDiscordEmbeds(embedJson);
+ sendDiscordEmbeds(embedJson);

The same as previously after the ESP32 boots and connects to the WiFi network, ESP32 will send a message to Discord, but this time with Embeds. With the above configuration, it would look like this:

Combine Discord Message and Discord Embeds #

If you want to send both simple text and Discord Embeds in one message, fill in both message and embedJson variables and uncomment the following line: sendDiscord(message, embedJson);.

- // sendDiscord(message, embedJson);
+ sendDiscord(message, embedJson);

This will send a message that would look like this:

Other ESP32 Discord Message Examples #

Spotify #

String embedJson = R"({
"title": "New song added!",
"color": 2021216,
"thumbnail": {
"url": "https://picsum.photos/400"
},
"fields": [
{
"name": "Track",
"value": "Scars To Your Beautiful",
"inline": true
},
{
"name": "Artist",
"value": "Alessia Cara",
"inline": true
},
{
"name": "Album",
"value": "Know-It-All",
"inline": true
}
],
"footer": {
"text": "Added Februrary 5, 2018 at 11:48AM",
"icon_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/1/19/Spotify_logo_without_text.svg/200px-Spotify_logo_without_text.svg.png"
}
})";

This example could be used for an application that integrates ESP32 and Spotify and sends the updates to Discord.

Other Embeds Features #

String embedJson = R"({
"author": {
"name": "Birdie♫",
"url": "https://www.reddit.com/r/cats/",
"icon_url": "https://picsum.photos/200"
},
"title": "Title lorem ipsum something very nice",
"url": "https://google.com/",
"description": "Text message. You can use Markdown here. *Italic* **bold** __underline__ ~~strikeout~~ [hyperlink](https://google.com) `code`",
"color": 15258703,
"fields": [
{
"name": "Text",
"value": "More text",
"inline": true
},
{
"name": "Even more text",
"value": "Yup",
"inline": true
},
{
"name": "Use `\"inline\": true` parameter, if you want to display fields in the same line.",
"value": "okay..."
},
{
"name": "Thanks!",
"value": "You're welcome :wink:"
}
],
"thumbnail": {
"url": "https://picsum.photos/600"
},
"image": {
"url": "https://picsum.photos/1200/600"
},
"footer": {
"text": "Woah! So cool! :smirk:",
"icon_url": "https://picsum.photos/200"
}
})";

This example demonstrates most of the features of Discord Embeds. You can find more details about Discord Embeds here.

Additional Resources #

Conclusion #

In this post we have created an example program for ESP32 that sends a message to Discord channel, using Discord Webhooks. We have tested different scenarios, such as sending a simple text and sending Discord Embeds, which allow better message formatting. This example can be used as an example for a more complex application, which would send a message to Discord when sensors connected to ESP32 detect anomalies or similar scenarios.