mirror of
https://github.com/Lemonochrme/clover.git
synced 2025-06-08 16:50:50 +02:00
Added DHT sensor library, Created DHTComponent.
This commit is contained in:
parent
c5d8457801
commit
c4398dbab2
4 changed files with 60 additions and 13 deletions
28
embedded/lib/Component/DHTComponent.cpp
Normal file
28
embedded/lib/Component/DHTComponent.cpp
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
#include "DHTComponent.hpp"
|
||||||
|
|
||||||
|
DHTComponent::DHTComponent(uint8_t type, byte pin)
|
||||||
|
: _pin(pin), _type(type), _dht(nullptr)
|
||||||
|
{}
|
||||||
|
|
||||||
|
DHTComponent::~DHTComponent()
|
||||||
|
{
|
||||||
|
delete _dht;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DHTComponent::setup()
|
||||||
|
{
|
||||||
|
_dht = new DHT_Unified(_pin, _type);
|
||||||
|
_dht->begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
float DHTComponent::getHumidity()
|
||||||
|
{
|
||||||
|
_dht->humidity().getEvent(&_event);
|
||||||
|
return _event.relative_humidity;
|
||||||
|
}
|
||||||
|
|
||||||
|
float DHTComponent::getTemperature()
|
||||||
|
{
|
||||||
|
_dht->temperature().getEvent(&_event);
|
||||||
|
return _event.temperature;
|
||||||
|
}
|
23
embedded/lib/Component/DHTComponent.hpp
Normal file
23
embedded/lib/Component/DHTComponent.hpp
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#ifndef _HEADER_COMPONENT_DHT
|
||||||
|
#define _HEADER_COMPONENT_DHT
|
||||||
|
#include <Adafruit_Sensor.h>
|
||||||
|
#include <DHT.h>
|
||||||
|
#include <DHT_U.h>
|
||||||
|
|
||||||
|
class DHTComponent
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DHTComponent(uint8_t type, byte pin);
|
||||||
|
~DHTComponent();
|
||||||
|
void setup();
|
||||||
|
float getHumidity();
|
||||||
|
float getTemperature();
|
||||||
|
|
||||||
|
private:
|
||||||
|
byte _pin;
|
||||||
|
uint8_t _type;
|
||||||
|
DHT_Unified* _dht;
|
||||||
|
sensors_event_t _event;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // _HEADER_COMPONENT_LED
|
|
@ -19,5 +19,6 @@ lib_deps =
|
||||||
ArduinoJson
|
ArduinoJson
|
||||||
olikraus/U8g2@^2.35.7
|
olikraus/U8g2@^2.35.7
|
||||||
seeed-studio/Grove - Chainable RGB LED@^1.0.0
|
seeed-studio/Grove - Chainable RGB LED@^1.0.0
|
||||||
|
adafruit/DHT sensor library@^1.4.6
|
||||||
extra_scripts =
|
extra_scripts =
|
||||||
pre:scripts/dotenv-var.py
|
pre:scripts/dotenv-var.py
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include "ServerHandler.hpp"
|
#include "ServerHandler.hpp"
|
||||||
#include "Component.hpp"
|
#include "Component.hpp"
|
||||||
#include "LedComponent.hpp"
|
#include "LedComponent.hpp"
|
||||||
|
#include "DHTComponent.hpp"
|
||||||
#include "Screen.hpp"
|
#include "Screen.hpp"
|
||||||
|
|
||||||
#ifdef SSID_CLOVER
|
#ifdef SSID_CLOVER
|
||||||
|
@ -16,6 +17,7 @@
|
||||||
|
|
||||||
Component humidity(ComponentType::Analog, PIN_A0);
|
Component humidity(ComponentType::Analog, PIN_A0);
|
||||||
LedComponent led(D8,D7,2);
|
LedComponent led(D8,D7,2);
|
||||||
|
DHTComponent airSensor(DHT11,D3);
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
|
@ -25,10 +27,8 @@ void setup()
|
||||||
|
|
||||||
Serial.print("Connected to WiFi. IP address: ");
|
Serial.print("Connected to WiFi. IP address: ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
pinMode(D5, OUTPUT);
|
|
||||||
digitalWrite(D5, LOW);
|
|
||||||
led.setup();
|
led.setup();
|
||||||
|
airSensor.setup();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
|
@ -37,8 +37,8 @@ void loop()
|
||||||
auto& serverHandler = ServerHandler::GetInstance();
|
auto& serverHandler = ServerHandler::GetInstance();
|
||||||
auto& dataHandler = DataHandler::GetInstance();
|
auto& dataHandler = DataHandler::GetInstance();
|
||||||
auto& screen = Display::Screen::GetInstance();
|
auto& screen = Display::Screen::GetInstance();
|
||||||
led.setColor(0,{255,0,0});
|
led.setColor(0,{32,0,0});
|
||||||
led.setColor(1,{0,255,0});
|
led.setColor(1,{0,32,0});
|
||||||
|
|
||||||
// Could not connect after setup: Showing screen failure
|
// Could not connect after setup: Showing screen failure
|
||||||
if(!serverHandler.isConnected())
|
if(!serverHandler.isConnected())
|
||||||
|
@ -66,8 +66,8 @@ void loop()
|
||||||
// Data gathered from various sensors
|
// Data gathered from various sensors
|
||||||
// 0 -> air(0), 0-300 -> dry(20), 300-700 -> humid (580), 700-950 -> water(940)
|
// 0 -> air(0), 0-300 -> dry(20), 300-700 -> humid (580), 700-950 -> water(940)
|
||||||
auto soilHumidityData = static_cast<float>(std::any_cast<int>(humidity.getValue()));
|
auto soilHumidityData = static_cast<float>(std::any_cast<int>(humidity.getValue()));
|
||||||
auto airTemperatureData = random(150, 300) / 10.0;
|
auto airTemperatureData = airSensor.getTemperature();
|
||||||
auto airHumidityData = random(0, 1000) / 10.0;
|
auto airHumidityData = airSensor.getHumidity();
|
||||||
auto lightData = random(0, 1000) / 10.0;
|
auto lightData = random(0, 1000) / 10.0;
|
||||||
|
|
||||||
// Updating the data handler
|
// Updating the data handler
|
||||||
|
@ -79,18 +79,13 @@ void loop()
|
||||||
// Screen showing
|
// Screen showing
|
||||||
screen.loop(soilHumidityData,airTemperatureData,airHumidityData,lightData);
|
screen.loop(soilHumidityData,airTemperatureData,airHumidityData,lightData);
|
||||||
|
|
||||||
|
// TODO: Add LedComponent management
|
||||||
if (soilHumidityData < 550) {
|
if (soilHumidityData < 550) {
|
||||||
Serial.println("Soil humidity low. Please water the plant.");
|
Serial.println("Soil humidity low. Please water the plant.");
|
||||||
digitalWrite(D5, HIGH);
|
|
||||||
} else if (soilHumidityData >= 550 && soilHumidityData <= 680) {
|
} else if (soilHumidityData >= 550 && soilHumidityData <= 680) {
|
||||||
Serial.println("Idle...");
|
Serial.println("Idle...");
|
||||||
digitalWrite(D5, LOW);
|
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Soil too wet.");
|
Serial.println("Soil too wet.");
|
||||||
digitalWrite(D5, LOW);
|
|
||||||
delay(400);
|
|
||||||
digitalWrite(D5, HIGH);
|
|
||||||
delay(400);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
serverHandler.loop();
|
serverHandler.loop();
|
||||||
|
|
Loading…
Add table
Reference in a new issue