created some reading methods
This commit is contained in:
parent
ec56181c08
commit
6b56b3f9b6
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
|
#include <SPI.h>
|
||||||
#include <BH1750.h>
|
#include <BH1750.h>
|
||||||
#include <Adafruit_Sensor.h>
|
#include <Adafruit_Sensor.h>
|
||||||
#include <DHT.h>
|
#include <DHT.h>
|
||||||
@ -21,3 +22,8 @@ extern int loopCapacitiveSoilMoistureSensor();
|
|||||||
// light
|
// light
|
||||||
extern void setupLightSensor();
|
extern void setupLightSensor();
|
||||||
extern int readLightSensorValue();
|
extern int readLightSensorValue();
|
||||||
|
|
||||||
|
// temperature & humidity
|
||||||
|
extern void setupTemperatureSensor();
|
||||||
|
extern int readHumidity();
|
||||||
|
extern int readTemperature();
|
||||||
@ -2,66 +2,31 @@
|
|||||||
|
|
||||||
#define DHTPIN 4
|
#define DHTPIN 4
|
||||||
|
|
||||||
#define DHTTYPE DHT11 // DHT 11
|
#define DHTTYPE DHT11
|
||||||
|
|
||||||
|
|
||||||
DHT_Unified dht(DHTPIN, DHTTYPE);
|
DHT_Unified dht(DHTPIN, DHTTYPE);
|
||||||
|
|
||||||
uint32_t delayMS;
|
|
||||||
|
|
||||||
void setupTemperatureSensor() {
|
void setupTemperatureSensor() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
// Initialize device.
|
|
||||||
dht.begin();
|
dht.begin();
|
||||||
Serial.println(F("DHT11 Unified Sensor Example"));
|
Serial.println(F("DHT11 Unified Sensor Ready"));
|
||||||
// Print temperature sensor details.
|
|
||||||
sensor_t sensor;
|
sensor_t sensor;
|
||||||
dht.temperature().getSensor(&sensor);
|
dht.temperature().getSensor(&sensor);
|
||||||
Serial.println(F("------------------------------------"));
|
|
||||||
Serial.println(F("Temperature Sensor"));
|
|
||||||
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
|
|
||||||
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
|
|
||||||
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
|
|
||||||
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
|
|
||||||
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
|
|
||||||
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
|
|
||||||
Serial.println(F("------------------------------------"));
|
|
||||||
// Print humidity sensor details.
|
|
||||||
dht.humidity().getSensor(&sensor);
|
|
||||||
Serial.println(F("Humidity Sensor"));
|
|
||||||
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
|
|
||||||
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
|
|
||||||
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
|
|
||||||
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%"));
|
|
||||||
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%"));
|
|
||||||
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%"));
|
|
||||||
Serial.println(F("------------------------------------"));
|
|
||||||
// Set delay between sensor readings based on sensor details.
|
|
||||||
delayMS = sensor.min_delay / 1000;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loopTemperatureSensor() {
|
// Get humidity event and its value.
|
||||||
// Delay between measurements.
|
int readHumidity(){
|
||||||
delay(delayMS);
|
|
||||||
// Get temperature event and print its value.
|
sensors_event_t event;
|
||||||
|
dht.humidity().getEvent(&event);
|
||||||
|
return event.relative_humidity;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get temperature event and its value.
|
||||||
|
int readTemperature(){
|
||||||
sensors_event_t event;
|
sensors_event_t event;
|
||||||
dht.temperature().getEvent(&event);
|
dht.temperature().getEvent(&event);
|
||||||
if (isnan(event.temperature)) {
|
return event.temperature;
|
||||||
Serial.println(F("Error reading temperature!"));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Serial.print(F("Temperature: "));
|
|
||||||
Serial.print(event.temperature);
|
|
||||||
Serial.println(F("°C"));
|
|
||||||
}
|
|
||||||
// Get humidity event and print its value.
|
|
||||||
dht.humidity().getEvent(&event);
|
|
||||||
if (isnan(event.relative_humidity)) {
|
|
||||||
Serial.println(F("Error reading humidity!"));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Serial.print(F("Humidity: "));
|
|
||||||
Serial.print(event.relative_humidity);
|
|
||||||
Serial.println(F("%"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user