From 908ca96c398f1ab6f60b37a778d9091231e46b7b Mon Sep 17 00:00:00 2001 From: Clement Date: Sat, 5 Aug 2023 11:55:21 +0200 Subject: [PATCH 1/6] 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() { -- 2.47.1 From 46d3edef1d622090d9df44e6388d56821c63b58a Mon Sep 17 00:00:00 2001 From: Clement Date: Fri, 11 Aug 2023 20:57:50 +0200 Subject: [PATCH 2/6] feat: add check if wifi connected --- include/Program.h | 2 +- lib/DiscordAPI/include/DiscordAPI.h | 11 ++++++----- lib/DiscordAPI/src/DiscordAPI.cpp | 7 ++++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/include/Program.h b/include/Program.h index 1f143ef..6449382 100644 --- a/include/Program.h +++ b/include/Program.h @@ -2,7 +2,7 @@ #define PROGRAM_H #include -#include +#include #include "DiscordAPI.h" diff --git a/lib/DiscordAPI/include/DiscordAPI.h b/lib/DiscordAPI/include/DiscordAPI.h index 4739af1..824b898 100644 --- a/lib/DiscordAPI/include/DiscordAPI.h +++ b/lib/DiscordAPI/include/DiscordAPI.h @@ -5,6 +5,7 @@ #include #include #include +#include class DiscordAPI{ public: @@ -16,7 +17,7 @@ public: bool sendMessage(String message); private: - + /** * @brief webhook URL */ @@ -24,14 +25,14 @@ private: /** * @brief http client - * */ HTTPClient* httpClient; - //WiFiClient* wifiClient; - + /** + * @brief wifi client (for https) + */ BearSSL::WiFiClientSecure* wifiClient; }; -#endif //DISCORD_API_H \ No newline at end of file +#endif //DISCORD_API_H diff --git a/lib/DiscordAPI/src/DiscordAPI.cpp b/lib/DiscordAPI/src/DiscordAPI.cpp index 931708b..fec6ff7 100644 --- a/lib/DiscordAPI/src/DiscordAPI.cpp +++ b/lib/DiscordAPI/src/DiscordAPI.cpp @@ -11,8 +11,9 @@ DiscordAPI::DiscordAPI(String hookUrl){ bool DiscordAPI::sendMessage(String message){ bool sortie = true; - //TODO: faire test si le wifi est bien connecter - + if(WiFi.status() != WL_CONNECTED){ + return false; + } this->httpClient->begin(*this->wifiClient, this->hookUrl); this->httpClient->addHeader("Content-Type", "application/json"); @@ -25,4 +26,4 @@ bool DiscordAPI::sendMessage(String message){ } this->httpClient->end(); return sortie; -} \ No newline at end of file +} -- 2.47.1 From 0762c38c471ceea0c8948d36a71479881bd65731 Mon Sep 17 00:00:00 2001 From: Clement Date: Fri, 11 Aug 2023 21:38:00 +0200 Subject: [PATCH 3/6] add formated sending message --- lib/DiscordAPI/include/DiscordAPI.h | 19 ++++++++++++++++++- lib/DiscordAPI/src/DiscordAPI.cpp | 13 +++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/DiscordAPI/include/DiscordAPI.h b/lib/DiscordAPI/include/DiscordAPI.h index 824b898..dd29aa6 100644 --- a/lib/DiscordAPI/include/DiscordAPI.h +++ b/lib/DiscordAPI/include/DiscordAPI.h @@ -15,7 +15,24 @@ public: */ DiscordAPI(String hookUrl); - bool sendMessage(String message); + /** + * @brief send a message to the discord webhook + * @param trame trame a envoyer + * @return true la trame est bien envoyée + * @return false la trame n'est pas envoyée + */ + bool sendMessage(String trame); + + /** + * @brief envoie un embed discord avec les horaires d'ouverture et de fermeture du lab + * + * @param hStart heure d'ouverture + * @param hEnd heure de fermeture + * @return true la trame est bien envoyée + * @return false la trame n'est pas envoyée + */ + bool sendheure(String hStart, String hEnd); + private: /** diff --git a/lib/DiscordAPI/src/DiscordAPI.cpp b/lib/DiscordAPI/src/DiscordAPI.cpp index fec6ff7..86a28b7 100644 --- a/lib/DiscordAPI/src/DiscordAPI.cpp +++ b/lib/DiscordAPI/src/DiscordAPI.cpp @@ -6,9 +6,11 @@ DiscordAPI::DiscordAPI(String hookUrl){ this->httpClient = new HTTPClient(); this->wifiClient = new BearSSL::WiFiClientSecure; this->wifiClient->setInsecure(); + + randomSeed(analogRead(A0)); } -bool DiscordAPI::sendMessage(String message){ +bool DiscordAPI::sendMessage(String trame){ bool sortie = true; if(WiFi.status() != WL_CONNECTED){ @@ -18,7 +20,7 @@ bool DiscordAPI::sendMessage(String message){ this->httpClient->begin(*this->wifiClient, this->hookUrl); this->httpClient->addHeader("Content-Type", "application/json"); - int resp = this->httpClient->POST("{\"content\":\""+ message +"\"}"); + int resp = this->httpClient->POST(trame); if(resp != 204){ sortie = false; Serial.print("sending message error code : "); @@ -27,3 +29,10 @@ bool DiscordAPI::sendMessage(String message){ this->httpClient->end(); return sortie; } + + +bool DiscordAPI::sendheure(String hStart, String hEnd){ + int color = random(16777215); + String trame = "{\"embeds\": [{\"title\": \"Le Lab est ouvert !\",\"description\": \"Le Lab est ouvert de **"+ hStart +"** à **"+ hEnd +"**\",\"color\": \"" + color + "\"}]}"; + return this->sendMessage(trame); +} -- 2.47.1 From 1acf1821e6b2950fcacc1aac1a00186d252bc743 Mon Sep 17 00:00:00 2001 From: Clement Date: Fri, 18 Aug 2023 18:51:52 +0200 Subject: [PATCH 4/6] add test heure --- src/Program.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Program.cpp b/src/Program.cpp index 0c5e066..4083710 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -5,7 +5,7 @@ Program::Program() { Serial.begin(MONITOR_SPEED); WiFi.begin(WSSID, PASS); - + Serial.print("Connecting to "); Serial.print(WSSID); Serial.println(" ..."); @@ -16,7 +16,7 @@ Program::Program() { } Serial.println('\n'); - Serial.println("Connection established!"); + Serial.println("Connection established!"); Serial.print("IP address:\t"); Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer @@ -24,6 +24,8 @@ Program::Program() { this->discord = new DiscordAPI(DISCORD_HOOK); Serial.println(this->discord->sendMessage("hello the world !!")); + delay(1000); + Serial.println(this->discord->sendheure("10h", "18h")); } void Program::loop() { -- 2.47.1 From 95161bc3828eb8d04f14ac817c2b5a75acb0917c Mon Sep 17 00:00:00 2001 From: Clement Date: Fri, 18 Aug 2023 19:00:26 +0200 Subject: [PATCH 5/6] fix typo --- lib/DiscordAPI/src/DiscordAPI.cpp | 2 +- src/Program.cpp | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/DiscordAPI/src/DiscordAPI.cpp b/lib/DiscordAPI/src/DiscordAPI.cpp index 86a28b7..e8dd87c 100644 --- a/lib/DiscordAPI/src/DiscordAPI.cpp +++ b/lib/DiscordAPI/src/DiscordAPI.cpp @@ -31,7 +31,7 @@ bool DiscordAPI::sendMessage(String trame){ } -bool DiscordAPI::sendheure(String hStart, String hEnd){ +bool DiscordAPI::sendHeure(String hStart, String hEnd){ int color = random(16777215); String trame = "{\"embeds\": [{\"title\": \"Le Lab est ouvert !\",\"description\": \"Le Lab est ouvert de **"+ hStart +"** à **"+ hEnd +"**\",\"color\": \"" + color + "\"}]}"; return this->sendMessage(trame); diff --git a/src/Program.cpp b/src/Program.cpp index 4083710..9dbdbea 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -23,9 +23,8 @@ Program::Program() { this->discord = new DiscordAPI(DISCORD_HOOK); - Serial.println(this->discord->sendMessage("hello the world !!")); delay(1000); - Serial.println(this->discord->sendheure("10h", "18h")); + Serial.println(this->discord->sendHeure("10h", "18h")); } void Program::loop() { -- 2.47.1 From 32498c546e7e89edf29773bf976fe0881783d124 Mon Sep 17 00:00:00 2001 From: Clement Date: Fri, 18 Aug 2023 19:08:27 +0200 Subject: [PATCH 6/6] fix typo --- lib/DiscordAPI/include/DiscordAPI.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/DiscordAPI/include/DiscordAPI.h b/lib/DiscordAPI/include/DiscordAPI.h index dd29aa6..638e06a 100644 --- a/lib/DiscordAPI/include/DiscordAPI.h +++ b/lib/DiscordAPI/include/DiscordAPI.h @@ -31,7 +31,7 @@ public: * @return true la trame est bien envoyée * @return false la trame n'est pas envoyée */ - bool sendheure(String hStart, String hEnd); + bool sendHeure(String hStart, String hEnd); private: -- 2.47.1