Estación de control domótico con BLYNK 2.0
https://www.youtube.com/watch?v=ivau7HK7P-g
#include <Adafruit_AHT10.h>
Adafruit_AHT10 aht;
#define BLYNK_TEMPLATE_ID "TMPLqj02Xu1t"
#define BLYNK_TEMPLATE_NAME "ESTACION DE CONTROL DOMOTICO"
const byte led1 = D4;
const byte led2 = D5;
const byte led3 = D6;
#define boton1 D3 //D3
int estado1 = HIGH;
int estado2 = 0;
int estado3 = 0;
int buttonState = 0;
int lastButtonState = HIGH; // la lectura anterior del pin de entrada
unsigned long lastDebounceTime = 0; // la última vez que se activó el pin de salida
unsigned long debounceDelay = 50;
long previousMillis = 0;
long tmedicion = 2000;
int contador = 1;
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
//#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI
#include "BlynkEdgent.h"
BLYNK_CONNECTED() {
Blynk.syncVirtual(V1);
Blynk.syncVirtual(V2);
Blynk.syncVirtual(V3);
}
void setup()
{
Serial.begin(115200);
delay(100);
Serial.println("Adafruit AHT10 demo!");
if (! aht.begin()) {
Serial.println("Could not find AHT10? Check wiring");
while (1) delay(10);
}
Serial.println("AHT10 found");
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (boton1, INPUT_PULLUP);
digitalWrite (led1, LOW);
digitalWrite (led2, LOW);
digitalWrite (led3, LOW);
BlynkEdgent.begin();
Blynk.virtualWrite(V1, estado1);
Blynk.virtualWrite(V2, estado2);
Blynk.virtualWrite(V3, estado3);
}
void loop() {
BlynkEdgent.run();
if(contador == 1){
previousMillis = millis(); //Solo pasara si en boton esta en HIGH. Si esta en LOW el bucle no hara esta accion.
contador = 0;
}
if(millis() - previousMillis >= tmedicion) { // Mientras (estado = 1) millis aumentara con cada pasada del bucle y previusMillis se mantendra
sensors_event_t humidity, temp;
aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" degrees C");
Blynk.virtualWrite(V4, temp.temperature);
Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% rH");
Blynk.virtualWrite(V5, humidity.relative_humidity);
contador = 1;
}
int reading = digitalRead(boton1);
if (reading != lastButtonState) {
// restablecer el temporizador antirrebote
lastDebounceTime = millis(); //la última vez que se activó el pin de salida
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// cualquiera que sea la lectura, ha estado allí por más tiempo que el rebote
//así que tómelo como el estado actual real:
// si el estado del botón ha cambiado:
if (reading != buttonState) {
buttonState = reading;
// solo alternar el LED si el nuevo estado del botón es ALTO
if (buttonState == HIGH) {
estado1 = !estado1;
// Encender la luz:
digitalWrite(led1, estado1);
Blynk.virtualWrite(V1, estado1);
Serial.println("LED 1 OPERADO");
}
}
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}
BLYNK_WRITE(V1){
int estado1 = param.asInt();
digitalWrite (led1, estado1);
lastButtonState = estado1;
}
BLYNK_WRITE(V2){
int estado2 = param.asInt();
digitalWrite (led2, estado2);
}
BLYNK_WRITE(V3){
int estado3 = param.asInt();
digitalWrite (led3, estado3);
}
Comentarios
Publicar un comentario