made it more arduino ide friendly

This commit is contained in:
rintyuu 2024-04-16 17:24:16 -07:00
parent 0c9d5f86b9
commit e44cd9cb94
9 changed files with 370 additions and 15 deletions

30
foo.ino
View file

@ -1,16 +1,16 @@
#include <Discord_WebHook.h>
Discord_Webhook discord;
String DISCORD_WEBHOOK = "https://discord.com/api/webhooks/id/token"; // insert your webhook here
void setup() {
Serial.begin(115200);
discord.begin(DISCORD_WEBHOOK); // init
discord.addWiFi("WiFiName","WiFiPassword"); // update this line to best fit you
discord.connectWiFi();
discord.send("Hello World");
}
void loop() {
#include <Discord_WebHook.h>
Discord_Webhook discord;
String DISCORD_WEBHOOK = "https://discord.com/api/webhooks/id/token"; // insert your webhook here
void setup() {
Serial.begin(115200);
discord.begin(DISCORD_WEBHOOK); // init
discord.addWiFi("WiFiName","WiFiPassword"); // update this line to best fit you
discord.connectWiFi();
discord.send("Hello World");
}
void loop() {
}

View file

@ -0,0 +1,38 @@
/*
Send BME280 sensor value to Discord using WebHook
You need Adafruit_BME280 library
*/
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Discord_WebHook.h>
Discord_Webhook discord;
// 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";
Adafruit_BME280 bme;
void setup() {
Serial.begin(115200);
if(bme.begin(0x76)){ // Sensor address can be 0x76 or 0x77
Serial.println("BME280 init success");
} else {
Serial.println("BME280 init failed");
}
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
}
void loop() {
// Send BME280 sensor value to Discord using WebHook every minute
discord.send("Temperature: " + String(bme.readTemperature()) +
"°C - Humidity: " + String(bme.readHumidity()) +
"% - Pressure: " + String(bme.readPressure() / 100.0F) +
" hPa");
delay(60000);
}

View file

@ -0,0 +1,31 @@
/*
Send Hello World to Discord using WebHook
This example disable serial debug messages
discord.send returns true if the message was sent successfully
*/
#include <Discord_WebHook.h>
Discord_Webhook discord;
// 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.disableDebug(); // Disable debug (no serial message will be send)
discord.addWiFi("WiFiName","WiFiPassword"); // Add WiFi credentials (you can add multiples WiFi SSID)
discord.connectWiFi(); // Connect to WiFi
bool message_sent = discord.send("Hello World"); // Send message
if(message_sent) {
Serial.println("Message sent");
} else {
Serial.println("I AM ERROR");
}
}
void loop() {
}

View file

@ -0,0 +1,22 @@
/*
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() {
}

View file

@ -0,0 +1,24 @@
/*
Send a message with variables to Discord using WebHook
*/
#include <Discord_WebHook.h>
Discord_Webhook discord;
// 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() {
int variable_int = 69; // Int variable (can be float, long etc...)
String variable_string = "SUS"; // String variable
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("When the variable " + String(variable_int) + " is " + variable_string); // Send message
}
void loop() {
}

18
libraries/keywords.txt Normal file
View file

@ -0,0 +1,18 @@
#########################################################
# Syntax Coloring Map for Usini Discord WebHook Library
#########################################################
#########################################################
# Datatypes (KEYWORD1)
#########################################################
Discord_Webhook KEYWORD1
#########################################################
# Methods and Functions (KEYWORD2)
#########################################################
begin KEYWORD2
addWiFi KEYWORD2
connectWiFi KEYWORD2
disableDebug KEYWORD2
setTTS KEYWORD2
send KEYWORD2

View file

@ -0,0 +1,9 @@
name=Discord_WebHook
version=1.0.1
author=Usini
maintainer=Rémi Sarrailh <remi@usini.eu>
sentence=Send message on discord using webhook
paragraph=Arduino Library to make a simple discord bot (sending message only) using webhook, compatible with esp8266, esp32
category=Communication
url=https://github.com/usini/usini_discord_webHook
architectures=*

View file

@ -0,0 +1,139 @@
/*
Discord_WebHook.cpp
Library for sending messages to Discord via WebHook
Copyright (c) 2022 µsini
Author : Rémi Sarrailh
Version : 1.0.0
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include "Discord_WebHook.h"
// Get webhook url into webhook_url
void Discord_Webhook::begin(String webhook_url) {
Discord_Webhook::webhook_url = webhook_url;
}
// Add WiFi credentials using WiFiMulti
void Discord_Webhook::addWiFi(const char *ssid, const char *password) {
if (Discord_Webhook::debug) {
Serial.print("[WIFI] Added ssid:");
Serial.println(ssid);
}
Discord_Webhook::wifi.addAP(ssid, password);
}
// Wait for WiFi connection to established
void Discord_Webhook::connectWiFi() {
WiFi.mode(WIFI_STA);
if (Discord_Webhook::debug) {
Serial.println("[WiFi] Connecting WiFi");
}
// wait for WiFi connection
while ((Discord_Webhook::wifi.run() != WL_CONNECTED)) {
if (Discord_Webhook::debug) {
Serial.print(".");
}
delay(100);
}
if (Discord_Webhook::debug) {
Serial.println("[WiFi] Connected");
}
}
// Set TTS variable
void Discord_Webhook::setTTS() { Discord_Webhook::tts = true; }
// Set debug variable to false
void Discord_Webhook::disableDebug() { Discord_Webhook::debug = false; }
// Send message to Discord, we disable SSL certificate verification for ease of
// use (Warning: this is insecure)
bool Discord_Webhook::send(String content) {
String discord_tts = "false";
if (Discord_Webhook::tts) {
discord_tts = "true";
}
WiFiClientSecure *client = new WiFiClientSecure; // Create a WiFiClientSecure
bool ok = false;
if (client) {
client->setInsecure(); // Disable SSL certificate verification
HTTPClient https; // Create HTTPClient
if (Discord_Webhook::debug) {
Serial.println("[HTTP] Connecting to Discord...");
Serial.println("[HTTP] Message: " + content);
Serial.println("[HTTP] TTS: " + discord_tts);
}
// Begin HTTPS requests
if (https.begin(*client, Discord_Webhook::webhook_url)) {
https.addHeader("Content-Type", "application/json"); // Set request as JSON
// Send POST request
int httpCode = https.POST("{\"content\":\"" + content +
"\",\"tts\":" + discord_tts + "}");
if (httpCode > 0) { // if HTTP code is return
if (httpCode == HTTP_CODE_OK ||
httpCode == HTTP_CODE_MOVED_PERMANENTLY ||
httpCode == HTTP_CODE_NO_CONTENT) {
// Discord webhook has changed and our request is not correct, so it
// will not send response, so we end without getting a response
// https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks
if (Discord_Webhook::debug) {
Serial.println("[HTTP] OK");
}
ok = true;
} else {
if (Discord_Webhook::debug) {
// This should mainly return an error if token or id is invalid
String payload = https.getString();
Serial.print("[HTTP] ERROR: ");
Serial.println(payload);
ok = false;
}
}
https.end();
} else {
if (Discord_Webhook::debug) {
// This will return an error if the server is unreachable
Serial.printf("[HTTP] ERROR: %s\n",
https.errorToString(httpCode).c_str());
ok = false;
}
}
} else {
if (Discord_Webhook::debug) {
// This will return an error if request failed
Serial.printf("[HTTP] Unable to connect\n");
ok = false;
}
}
} else {
if (Discord_Webhook::debug) {
// This shouldn't happen but anyway it's better to check
Serial.println("[HTTP] Unable to create client");
ok = false;
}
}
delete client;
return ok;
}

View file

@ -0,0 +1,74 @@
/*
Discord_WebHook.h
Library for sending messages to Discord via WebHook
Copyright (c) 2022 µsini
Author : Rémi Sarrailh
Version : 1.0.0
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef Discord_WebHook_h
#define Discord_WebHook_h
#include <Arduino.h>
// Define if it is an ESP32 (code should works on new board ESP32-S2/ESP32-C3)
#if defined(ESP32)
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>
//#include "WiFiClientSecureAxTLS.h"
#include <ESP8266HTTPClient.h>
#else
// We still open it as ESP32 for compatibility with later version (not tested)
#warning "Library worked on ESP8266/ESP32 only"
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#endif
class Discord_Webhook {
public:
void begin(String webhook_url);
void addWiFi(const char* ssid, const char* password);
void connectWiFi();
void disableDebug();
void setTTS();
bool send(String content);
private:
#ifdef ESP32
WiFiMulti wifi;
#endif
#ifdef ESP8266
ESP8266WiFiMulti wifi;
#endif
String webhook_url;
bool tts = false;
bool debug = true;
};
#endif