From 908ca96c398f1ab6f60b37a778d9091231e46b7b Mon Sep 17 00:00:00 2001 From: Clement Date: Sat, 5 Aug 2023 11:55:21 +0200 Subject: [PATCH] add test call --- include/Program.h | 9 ++++++- lib/DiscordAPI/include/DiscordAPI.h | 37 +++++++++++++++++++++++++++++ lib/DiscordAPI/src/DiscordAPI.cpp | 28 ++++++++++++++++++++++ src/Program.cpp | 21 ++++++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 lib/DiscordAPI/include/DiscordAPI.h create mode 100644 lib/DiscordAPI/src/DiscordAPI.cpp diff --git a/include/Program.h b/include/Program.h index 66e4a79..1f143ef 100644 --- a/include/Program.h +++ b/include/Program.h @@ -1,7 +1,11 @@ #ifndef PROGRAM_H #define PROGRAM_H -#include "Arduino.h" +#include +#include + +#include "DiscordAPI.h" + class Program { public: @@ -14,6 +18,9 @@ public: * Program main loop */ void loop(); + +private: + DiscordAPI* discord; }; #endif diff --git a/lib/DiscordAPI/include/DiscordAPI.h b/lib/DiscordAPI/include/DiscordAPI.h new file mode 100644 index 0000000..4739af1 --- /dev/null +++ b/lib/DiscordAPI/include/DiscordAPI.h @@ -0,0 +1,37 @@ +#ifndef DISCORD_API_H +#define DISCORD_API_H + +#include +#include +#include +#include + +class DiscordAPI{ +public: + /** + * @brief Construct a new Discord API object + * @param hookUrl url of the discord webhook + */ + DiscordAPI(String hookUrl); + + bool sendMessage(String message); +private: + + /** + * @brief webhook URL + */ + String hookUrl; + + /** + * @brief http client + * + */ + HTTPClient* httpClient; + + //WiFiClient* wifiClient; + + BearSSL::WiFiClientSecure* wifiClient; +}; + + +#endif //DISCORD_API_H \ No newline at end of file diff --git a/lib/DiscordAPI/src/DiscordAPI.cpp b/lib/DiscordAPI/src/DiscordAPI.cpp new file mode 100644 index 0000000..931708b --- /dev/null +++ b/lib/DiscordAPI/src/DiscordAPI.cpp @@ -0,0 +1,28 @@ +#include "../include/DiscordAPI.h" + +DiscordAPI::DiscordAPI(String hookUrl){ + this->hookUrl = hookUrl; + + this->httpClient = new HTTPClient(); + this->wifiClient = new BearSSL::WiFiClientSecure; + this->wifiClient->setInsecure(); +} + +bool DiscordAPI::sendMessage(String message){ + bool sortie = true; + + //TODO: faire test si le wifi est bien connecter + + + this->httpClient->begin(*this->wifiClient, this->hookUrl); + this->httpClient->addHeader("Content-Type", "application/json"); + + int resp = this->httpClient->POST("{\"content\":\""+ message +"\"}"); + if(resp != 204){ + sortie = false; + Serial.print("sending message error code : "); + Serial.println(resp); + } + this->httpClient->end(); + return sortie; +} \ No newline at end of file diff --git a/src/Program.cpp b/src/Program.cpp index e412959..0c5e066 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -3,6 +3,27 @@ Program::Program() { // Startup Serial.begin(MONITOR_SPEED); + + WiFi.begin(WSSID, PASS); + + Serial.print("Connecting to "); + Serial.print(WSSID); Serial.println(" ..."); + + int i = 0; + while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect + delay(1000); + Serial.print(++i); Serial.print(' '); + } + + Serial.println('\n'); + Serial.println("Connection established!"); + Serial.print("IP address:\t"); + Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer + + + + this->discord = new DiscordAPI(DISCORD_HOOK); + Serial.println(this->discord->sendMessage("hello the world !!")); } void Program::loop() {