viernes, 4 de diciembre de 2020

Sensor de humedad y temperatura

 #InnovaEx20_21
Programando con Arduino y con Blynk para realizar una aplicació en el ordenador para poder leer los datos de nuestra estación.

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "ZKdt0FnuMGG92IuTHv-PEd4oOkV1XST7";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Fwifi";
char pass[] = "fgm120576n";

#define DHTPIN 2          // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11 defino el sensor que utilizo
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);//mi sensor dht y la variable que ya existe se llama
//DHT en mayuscula asociada a ese pin 2 y a es sensor
BlynkTimer timer;//temporiza cada cuanto tiempo envío información en la nube
// una vez al minuto o así

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()//hace una funcion que no retorna valir y lo manda a la nube
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

 int humedadTierra;
 int ht;
 humedadTierra = analogRead(A0);
 ht=map(humedadTierra,0,1023,100,0);//mapeado desde datos recibidos pasados a porcentajes
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);//vinculo el valor a un puerto virtual
  Blynk.virtualWrite(V6, t);//lo mismo
  Blynk.virtualWrite(V7, ht);//llevo a la nube el valor de humedad de tierra
}

void setup()
{
  // Debug console es decir controlar el funcionamiento por dentro
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);//inicio conectividad con mi token y wifi
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);//mil mili segundos es dc cada segundo ejecutas la funiones y lo envías a la nube
}

void loop()
{
  Blynk.run();//empieza el prograama
  timer.run();
}




 

No hay comentarios:

Publicar un comentario