Discord server has a system to send messages automatically to a channel.
This system uses webhook, so you just have to send a web request to a URL and a message will be written on the selected channel.
https://en.wikipedia.org/wiki/Webhook
This could be used in many ways, for example:
- Make a badge reader using RFID to send a message when someone is at your fablab.
- Display the temperature of a room each hour.
Since this tutorial was made, I create a library on Arduino with examples !
Search Discord on the library manager and you will find it
/*
Send Hello World to Discord using WebHook
*/
#include <Discord_WebHook.h>
Discord_Webhook discord; // Create a Discord_Webhook object
// How to get the Webhook URL
// https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks
String DISCORD_WEBHOOK = "https://discord.com/api/webhooks/id/token";
void setup() {
Serial.begin(115200);
discord.begin(DISCORD_WEBHOOK); // Initialize the Discord_Webhook object
discord.addWiFi("WiFiName","WiFiPassword"); // Add WiFi credentials (you can add multiples WiFi SSID)
discord.connectWiFi(); // Connect to WiFi
discord.send("Hello World"); // Send Hello World to Discord
}
void loop() {
}
Create a WebhookFirst we need to create a webhook in our server:
https://support.discordapp.com/hc/en-us/articles/228383668-Intro-to-Webhooks
Click on your discord servername → Discord Settings Then click on Webhook → Create WebHook
Copy the Webhook URL
I used Arduino Create to write my code, but you can also use the classic IDE.
Here is a link to the code: https://create.arduino.cc/editor/madnerd/429197a3-8a40-4d2c-bc16-1a502cb26cd9/preview
Settings
Settings are inside arduino_secrets.h or the Secrettab
- SECRET_SSID: your Wi-Fi network name
- SECRET_PASS: your Wi-Fi password
- SECRET_WEBHOOK: your Webhook URL without https://discordapp.com
- SECRET_TTS: true to enable text to speech or false to disable it
Upload your code on your Arduino MKR 1010, you should see this message:
You can find more information on how to use Discord webhook here: https://discordapp.com/developers/docs/resources/webhook#execute-webhook
Here is also some information for text-to-speech: https://support.discordapp.com/hc/en-us/articles/212517297-Text-to-Speech-101
To send a message on discord, just type:
discord_send("Hello World");
Library
I used two libraries for this code:
- WiFiNINA: Wi-Fi manager
- ArduinoHttpClient: Web request
Comments