# SENSORES

# Temperatura

# Biblioteca DHTesp 11 / 22 / 21

# Importaciones, pines, instancias y variables

#include "DHTesp.h"   // Bibliotecas
#define DHT_PIN 15    // Configuración PIN sensor

DHTesp dhtSensor;     // Instancia sensor

float temperatura;    // Variables
float humedad;

# Función/es

void dataDHT () {
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  temperatura = dhtSensor.getTemperature();
  humedad = dhtSensor.getHumidity();
  Serial.println("Temperatura: " + String(data.temperature, 2) + "°C");
  Serial.println("Humedad: " + String(data.humidity, 1) + "%");
  Serial.println("---");
  delay(1000);
}

# void setup()

  Serial.begin(115200);
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);

# void loop()

dataDHT();

# DHTesp.hpp

#include "DHTesp.h"   // Bibliotecas
#define DHT_PIN 15    // Configuración PIN sensor

DHTesp dhtSensor;     // Instancia sensor

float temperatura;    // Variables

float humedad;

void dataDHT () {
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  temperatura = dhtSensor.getTemperature();
  humedad = dhtSensor.getHumidity();
  Serial.println("Temperatura: " + String(data.temperature, 2) + "°C");
  Serial.println("Humedad: " + String(data.humidity, 1) + "%");
  Serial.println("---");
  delay(1000);
}

void iniciarDHTesp(){
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}

# Biblioteca DHT 11 / 22 / 21

# Importaciones, pines, instancias y variables

#include "DHT.h"            // Bibliotecas
#define DHTTYPE DHT22       // Configuración sensor DHT11 - DHT21
#define DHTPIN 15           // Pin sensor

DHT dht(DHTPIN, DHTTYPE);   // Instancia de la biblioteca

float temperatura;          // Variables
float humedad;

# Función/es

void leerSensoresDHT(){     // Funciónes/Métodos

   // La lectura del sensor tarda 250 milisegundos
  temperatura = dht.readTemperature(); 
  humedad = dht.readHumidity();
  
   if (isnan(humedad) || isnan(temperatura)) {
      Serial.println("¡Error de lectura en el sensor DHT!");
      return;
   }
}

# void setup()

  Serial.begin(115200);
  dht.begin();

# void loop()

  leerSensoresDHT();
   Serial.print("Humedad: ");
   Serial.print(humedad);
   Serial.println(" %\t");
   Serial.print("Temperatura: ");
   Serial.print(temperatura);
   Serial.println(" ºC ");
   delay(1000);

# DHT.hpp

#include "DHT.h"            // Biblioteca
#define DHTTYPE DHT22       // Configuración sensor DHT11 - DHT21
#define DHTPIN 15           // Pin sensor
DHT dht(DHTPIN, DHTTYPE);   // Instancia de la biblioteca

float temperatura;          // Variables
float humedad;

void leerSensoresDHT(){     // Función/Método

   // La lectura del sensor tarda 250 milisegundos
  temperatura = dht.readTemperature(); 
  humedad = dht.readHumidity();
  
   if (isnan(humedad) || isnan(temperatura)) {
      Serial.println("¡Error de lectura en el sensor DHT!");
      return;
   }
}

void iniciarDHT() {
  dht.begin();
}

# Biblioteca BME280

Sketch > Include Library > Manage Libraries => adafruit bme280 + Adafruit_Sensor library

Adafruit BME280 (opens new window)

Adafruit Sensor library (opens new window)

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

/*#include <SPI.h>
#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5*/

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI

unsigned long delayTime;

void setup() {
  Serial.begin(9600);
  Serial.println(F("BME280 test"));

  bool status;

  // default settings
  // (you can also pass in a Wire library object like &Wire2)
  status = bme.begin(0x76);  
  if (!status) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }

  Serial.println("-- Default Test --");
  delayTime = 1000;

  Serial.println();
}


void loop() { 
  printValues();
  delay(delayTime);
}

void printValues() {
  Serial.print("Temperature = ");
  Serial.print(bme.readTemperature());
  Serial.println(" *C");
  
  // Convert temperature to Fahrenheit
  /*Serial.print("Temperature = ");
  Serial.print(1.8 * bme.readTemperature() + 32);
  Serial.println(" *F");*/
  
  Serial.print("Pressure = ");
  Serial.print(bme.readPressure() / 100.0F);
  Serial.println(" hPa");

  Serial.print("Approx. Altitude = ");
  Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  Serial.println(" m");

  Serial.print("Humidity = ");
  Serial.print(bme.readHumidity());
  Serial.println(" %");

  Serial.println();
}

# Sensor de movimiento PIR

/*********
  Rui Santos
  Complete project details at http://randomnerdtutorials.com  
*********/

// constants won't change. Used here to set a pin number :
const int ledPin =  26;      // the number of the LED pin

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

# Sensor Ultrasonidos HC-SR04

const int trigPin = 5;
const int echoPin = 18;

//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701

long duration;
float distanceCm;
float distanceInch;

void setup() {
  Serial.begin(115200); // Starts the serial communication
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}

void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  
  // Calculate the distance
  distanceCm = duration * SOUND_SPEED/2;
  
  // Convert to inches
  distanceInch = distanceCm * CM_TO_INCH;
  
  // Prints the distance in the Serial Monitor
  Serial.print("Distance (cm): ");
  Serial.println(distanceCm);
  Serial.print("Distance (inch): ");
  Serial.println(distanceInch);
  
  delay(1000);
}