From 36a1fe4d910895844232ab7d223ab89958f24dca Mon Sep 17 00:00:00 2001 From: Clement Date: Sat, 24 Jun 2023 20:10:25 +0200 Subject: [PATCH] =?UTF-8?q?feat=20:=20lecteur=20capteur=20temp=C3=A9rature?= =?UTF-8?q?=20(DHT22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IOT/include/Program.h | 9 +++++- IOT/lib/Capteur/include/Capteur.h | 49 ++++++++++++++++++++++++++++++ IOT/lib/Capteur/include/HumiTemp.h | 38 +++++++++++++++++++++++ IOT/lib/Capteur/src/Capteur.cpp | 13 ++++++++ IOT/lib/Capteur/src/HumiTemp.cpp | 34 +++++++++++++++++++++ IOT/platformio.ini | 2 ++ IOT/src/Program.cpp | 3 ++ 7 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 IOT/lib/Capteur/include/Capteur.h create mode 100644 IOT/lib/Capteur/include/HumiTemp.h create mode 100644 IOT/lib/Capteur/src/Capteur.cpp create mode 100644 IOT/lib/Capteur/src/HumiTemp.cpp diff --git a/IOT/include/Program.h b/IOT/include/Program.h index 66e4a79..7f57f9b 100644 --- a/IOT/include/Program.h +++ b/IOT/include/Program.h @@ -1,7 +1,9 @@ #ifndef PROGRAM_H #define PROGRAM_H -#include "Arduino.h" +#include +#include "Capteur.h" +#include "HumiTemp.h" class Program { public: @@ -14,6 +16,11 @@ public: * Program main loop */ void loop(); +private: + + //TODO: faire commentaire + Capteur* DHT; + }; #endif diff --git a/IOT/lib/Capteur/include/Capteur.h b/IOT/lib/Capteur/include/Capteur.h new file mode 100644 index 0000000..1844cdc --- /dev/null +++ b/IOT/lib/Capteur/include/Capteur.h @@ -0,0 +1,49 @@ +#ifndef CAPTEUR_H +#define CAPTEUR_H + +#include + +class Capteur{ +public: + + /** + * @brief Construct a new Capteur object + * + * @param[in] type type de mesure lue (T/H, W, D,...) + */ + Capteur(String type); + + /** + * @brief tar le capteur si il a besoins d'être tarré + * + * @param[in] val *opt* si le capteur a besoins d'une valeur de ref pour être tarré + * @return true si la tarre a bien réussi (ou si il n'a pas besoins de tarre) + * @return false erreur durrant la tar + */ + virtual bool tar(int val = 0); + + /** + * @brief lit la valeur du capteur + * + * @return String retour la valeur + */ + virtual String read() = 0; + + /** + * @brief retourne le type de valeur lue + * + * @return String + */ + String getValType(); + +protected: + /** + * @brief type de mesure lue (T/H, W, D,...) + * + */ + String type; + +}; + + +#endif // CAPTEUR_H diff --git a/IOT/lib/Capteur/include/HumiTemp.h b/IOT/lib/Capteur/include/HumiTemp.h new file mode 100644 index 0000000..09a937e --- /dev/null +++ b/IOT/lib/Capteur/include/HumiTemp.h @@ -0,0 +1,38 @@ +#ifndef HUMI_TEMP_H +#define HUMI_TEMP_H + +#include +#include +#include "Capteur.h" + +class HumiTemp : public Capteur{ + +public: + + /** + * @brief Construct a new Humi Temp object + * + * @param pin pin du capteur dht + * @param type type de capteur dht (11,22,...) + */ + HumiTemp(int pin, uint8_t type); + + /** + * @brief lit le capteur d'humi/temp + * + * @return String valeur format "XX/YY" X% et Y°C + */ + String read(); + + //TODO: faire commentaire + int getTemp(); + int getHumi(); + +private: + + DHT* capteur; + +}; + + +#endif //HUMI_TEMP_H diff --git a/IOT/lib/Capteur/src/Capteur.cpp b/IOT/lib/Capteur/src/Capteur.cpp new file mode 100644 index 0000000..b54b9e4 --- /dev/null +++ b/IOT/lib/Capteur/src/Capteur.cpp @@ -0,0 +1,13 @@ +#include "../include/Capteur.h" + +Capteur::Capteur(String type){ + this->type = type; +} + +bool Capteur::tar(int val){ + return true; +} + +String Capteur::getValType(){ + return this->type; +} \ No newline at end of file diff --git a/IOT/lib/Capteur/src/HumiTemp.cpp b/IOT/lib/Capteur/src/HumiTemp.cpp new file mode 100644 index 0000000..5f98982 --- /dev/null +++ b/IOT/lib/Capteur/src/HumiTemp.cpp @@ -0,0 +1,34 @@ +#include "../include/HumiTemp.h" + +HumiTemp::HumiTemp(int pin, uint8_t type): + Capteur("H/T"){ + this->capteur = new DHT(pin, type); + this->capteur->begin(); +} + +int HumiTemp::getTemp(){ + int temp = this->capteur->readTemperature(); + if(isnan(temp)){ + Serial.println(" DHT reading failed "); + return -1; + } + return temp; +} + +int HumiTemp::getHumi(){ + int hum = this->capteur->readHumidity(); + if(isnan(hum)){ + Serial.println(" DHT reading failed "); + return -1; + } + return hum; +} + +String HumiTemp::read(){ + String sortie = ""; + + sortie += String(this->getHumi()); + sortie += "/"; + sortie += String(this->getTemp()); + return sortie; +} diff --git a/IOT/platformio.ini b/IOT/platformio.ini index 329b7c7..f1d6117 100644 --- a/IOT/platformio.ini +++ b/IOT/platformio.ini @@ -52,6 +52,8 @@ upload_speed = 921600 lib_deps = ; example: ; erropix/ESP32 AnalogWrite@0.2 + adafruit/DHT sensor library@^1.4.4 + adafruit/Adafruit Unified Sensor@^1.1.9 ; required by DHT ; Checker settings check_tool = clangtidy, cppcheck diff --git a/IOT/src/Program.cpp b/IOT/src/Program.cpp index e412959..13a068b 100644 --- a/IOT/src/Program.cpp +++ b/IOT/src/Program.cpp @@ -3,8 +3,11 @@ Program::Program() { // Startup Serial.begin(MONITOR_SPEED); + this->DHT = new HumiTemp(D4, DHT22); } void Program::loop() { // Loop + delay(2000); + Serial.println(this->DHT->read()); }