As I said this code controlls a smart house, an LDR, infrared sensor, gas sensor, rain sensor, a DHT 22 and an rfid module.
The particular outputs are :
- When a gas is detected, a led blinks twice then waits 1s, it makes a loop.
- When water is detected by the rain sensor it deploys a sliding roof until it reaches a first end switch, it waits 5s, if there’s no water on it this time it closes untill it reaches the second end switch.
As i did here, the outputs are responding late compared to how fast it were responding before i add the Blynk feature (the blinking output of the gas sensor turning slowly, the motor keeps working 1s after it reaches the end switches, etc…).
I tried different values for the Blynk.timer and it’s still slow, i’m very probably doing something wrong.
The thing i want is to keep the time response low and use this sketch with Blynk.
EDIT : I just corrected the title of the topic, i want to DECREASE the response time to make my outputs answer faster. ^^’
Here is the code :
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <SPI.h>
char ssid[] = "wifi";
char pass[] = "pass";
char auth[] = "auth";
BlynkTimer timer;
//Rain sensor
#include <Arduino.h>
#include <analogWrite.h>
int PL;
#define PL_Pin 39
#define Led_Blanche 27
#define fin_de_course_ext 35
#define fin_de_course_int 34
#define Ouverture 0
#define Fermeture 4
const int Com7 = 2800;
const int Com8 = 3200;
int Fin_int;
int Fin_ext;
unsigned long ref_pluie;
const unsigned long temporisation_moteur = 5000; //Period before the motor answers
const int V = 250; //Motor Speed (PWM)
//RFID
#include <SPI.h>
#include <Wire.h>
#include <MFRC522.h>
#define SS_PIN 13 //Pin SDA sur Pin 33
#define RST_PIN 2 //Pin RST sur Pin 32
#define Led_Verte 12 //Led Verte sur Pin 12
#define Led_Rouge 14 //Led Rouge sur Pin 14
MFRC522 mfrc522(SS_PIN, RST_PIN);
unsigned long ref_rfid;
const unsigned long temps = 3000; //Time the leds stay ON
//LCD
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); //Paramétrage du LCD
//Création de nouveaux caractères
byte deg[] = //Icône Degré
{
B00111,
B00101,
B00111,
B00000,
B00000,
B00000,
B00000,
B00000,
};
byte soleil[] = //Icône Soleil
{
B00100,
B10001,
B01110,
B11111,
B01110,
B10001,
B00100,
B00000,
};
byte lune[] = //Icône Lune
{
B01110,
B11101,
B11010,
B11000,
B11101,
B11111,
B01110,
B00000,
};
byte pluie[] = //Icône Pluie
{
B00110,
B11111,
B11111,
B00000,
B10010,
B00000,
B00101,
B00000,
};
//DHT 22
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
DHT_Unified dht(25, DHT22);
#define Ventilateur_dht 15
unsigned long ref_dht;
const unsigned long IDHT = 2000; //The period between two temperature/humidity measurements from the DHT22
//LDR
int LDR;
#define LDR_Pin 33
#define Relais 5 //Relay on pin 5
const int Com1 = 3200;
const int Com2 = 3400;
//Infrarouge
int INF;
#define INF_Pin 32 //Définition de INF
#define Led_Bleue 16
const int Com3 = 700; //Seuil bas INF
const int Com4 = 1000; //Seuil haut INF
//MQ2
int MQ;
#define MQ_Pin 36
#define Led_Buzzer 17
#define Ventilateur_Mq 26
const int Com5 = 400;
const int Com6 = 600;
int ledstate = 0; //to initialise the Led_Buzzer blinking
int general_state;
unsigned long ref_mq;
const unsigned long Blinkon = 100; //Period MQ outputs are On
const unsigned long Blinkoff = 30; //Period MQ outputs are Off
const unsigned long Blinkoff2 = 1000; //Period MQ outputs are Off 2
void setup(){
Serial.begin(9600);
delay(1000);
Blynk.begin(auth, ssid, pass,IPAddress(IP and port);
timer.setInterval(1000L, myprogram);
// RFID
SPI.begin();
mfrc522.PCD_Init();
// LCD and chars
lcd.init();
lcd.backlight();
lcd.createChar(0, deg);
lcd.createChar(1, soleil);
lcd.createChar(2, lune);
lcd.createChar(3, pluie);
// DHT22
dht.begin();
sensor_t sensor;
dht.temperature().getSensor(&sensor);
dht.humidity().getSensor(&sensor);
// Inputs/Outputs
pinMode(LDR_Pin , INPUT);
pinMode( Relais , OUTPUT); //Définition Pin 5 en sortie // Relais // Sortie LDR Pin 5
pinMode( INF_Pin , INPUT);
pinMode(Led_Bleue, OUTPUT); //Définition Pin 16 en sortie // Led Bleue (Système ON) // Sortie INF Pin 16
pinMode( MQ_Pin , INPUT); //Définition Pin 36 en entrée // Entrée du MQ2
pinMode( Led_Buzzer , OUTPUT); //Définition Pin 17 en sortie // Led Jaune + Buzzer (Système ON) // Sortie MQ2 Pin 17
pinMode(Ventilateur_Mq, OUTPUT); //Définition Pin 26 en sortie // Ventilateur 1 (Système ON) // Sortie MQ2 Pin 26
pinMode(Ventilateur_dht, OUTPUT); //Définition Pin 15 en sortie // Ventilateur 2 (Système ON) // Sortie DHT Pin 15
pinMode( PL_Pin , INPUT); //Définition Pin 39 en entrée // Entrée du Détecteur de pluie // Entrée PL Pin 39
pinMode(fin_de_course_ext, INPUT); //Définition Pin 35 en entrée // Entrée du Fin de Course 1 // Entrée Fin Pin 35
pinMode(fin_de_course_int, INPUT); //Définition Pin 34 en entrée // Entrée du Fin de Course 2 // Entrée Fin1 Pin 34
pinMode( Led_Blanche , OUTPUT); //Définition Pin 27 en sortie // Led Blanche (Système ON) // Sortie PL Pin 27
pinMode( Ouverture , OUTPUT); //Définition Pin 0 en sortie // Déploiement de la terrasse // Sortie PL 1 Pin 0
pinMode( Fermeture , OUTPUT); //Définition Pin 4 en sortie // Retour de la terrasse // Sortie PL 2 Pin 4
pinMode(Led_Verte, OUTPUT); //Définition Pin 12 en sortie // Led Verte (Système ON) // Sortie RFID Pin 12
pinMode(Led_Rouge, OUTPUT); //Définition Pin 14 en sortie // Led Rouge (Système OFF) // Sortie RFID Pin 14
//If the roof is deployed, this will undeploy it
if (Fin_int < 2000)
{
analogWrite(Fermeture, V);
}
else if (Fin_int > 2000)
{
analogWrite(Fermeture, 0);
}
//Initialising millis references
ref_mq = millis();
ref_pluie = millis();
ref_dht = 0 ;
ref_rfid = millis();
ref_blynk = millis();
}
void loop()
{
Blynk.run();
timer.run();
}
void myprogram(){
//LDR
{
LDR = analogRead(LDR_Pin);
Blynk.virtualWrite(V4, LDR);
if (LDR < Com1) //Condition 1 : LDR is in the dark
{
digitalWrite(Relais, HIGH); //Pin 12 HIGH
lcd.setCursor(15,0);
lcd.write(2);
WidgetLED led_LDR(V7);
led_LDR.on();
}
else if (LDR > Com2) //Condition 2 : LDR is exposed to the light
{
digitalWrite(Relais, LOW); //Pin 12 LOW
lcd.setCursor(15,0);
lcd.write(1);
WidgetLED led_LDR(V7);
led_LDR.off();
}
}
//INFRARED
{
INF = analogRead(INF_Pin);
Blynk.virtualWrite(V3, INF);
if (INF > Com4) //Condition 1 : INF detects something
{
digitalWrite(Led_Bleue, HIGH); //Pin 16 HIGH
}
else if (INF < Com3) //Condition 2 : INF doesn't detect anything
{
digitalWrite(Led_Bleue, LOW); //Pin 16 LOW
}
}
//Programme du MQ2
{
MQ = analogRead(MQ_Pin);
if (millis() - ref_blynk >= temps_blynk)
{
Blynk.virtualWrite(V2, MQ);
}
if (MQ >= Com6) //Condition 1 : MQ detects a gaz
{
digitalWrite(Ventilateur_Mq, HIGH);
general_state = 1;
Blynk.notify("Gaz détecté !");
if ((millis() - ref_mq >= Blinkoff2) && ledstate == 0) //Step one, waits 1s
{
ref_mq = millis();
digitalWrite(Led_Buzzer, HIGH); //Out is HIGH
ledstate = 1;
}
else if ((millis() - ref_mq >= Blinkon) && ledstate == 1) //Step two, waits 100ms
{
ref_mq = millis(); //Out is LOW
digitalWrite(Led_Buzzer, LOW);
ledstate = 2;
}
else if ((millis() - ref_mq >= Blinkoff) && ledstate == 2) //Step three; waits 30ms
{
ref_mq = millis();
digitalWrite(Led_Buzzer, HIGH); //Out is HIGH
ledstate = 3;
}
else if ((millis() - ref_mq >= Blinkon) && ledstate == 3) //Step four and the last one, waits 100ms again then comes back to Step one.
{
ref_mq = millis();
digitalWrite(Led_Buzzer, LOW); //Out is LOW
ledstate = 0;
}
}
else if (MQ <= Com5) //Condition 2 : no GAZ
{
ref_mq = millis();
digitalWrite(Ventilateur_Mq, LOW);
digitalWrite(Led_Buzzer, LOW);
ledstate = 0;
general_state = 0;
}
else if (Com5 <= MQ <= Com6) //This part is to create an Hysteresys for the outputs
{
if (general_state == 0) //If the outputs were previously LOW, keep them LOW
{
ref_mq = millis();
digitalWrite(Ventilateur_Mq, LOW);
digitalWrite(Led_Buzzer, LOW);
ledstate = 0;
}
else if (general_state == 1) //If the outputs were previously HIGH, keep them HIGH
{
digitalWrite(Ventilateur_Mq, HIGH);
if ((millis() - ref_mq >= Blinkoff2) && ledstate == 0)
{
ref_mq = millis();
digitalWrite(Led_Buzzer, HIGH);
ledstate = 1;
}
else if ((millis() - ref_mq >= Blinkon) && ledstate == 1)
{
ref_mq = millis();
digitalWrite(Led_Buzzer, LOW);
ledstate = 2;
}
else if ((millis() - ref_mq >= Blinkoff) && ledstate == 2)
{
ref_mq = millis();
digitalWrite(Led_Buzzer, HIGH);
ledstate = 3;
}
else if ((millis() - ref_mq >= Blinkon) && ledstate == 3)
{
ref_mq = millis();
digitalWrite(Led_Buzzer, LOW);
ledstate = 0;
}
}
}
}
//Rain sensor
{
Fin_ext = analogRead (fin_de_course_ext);
Fin_int = analogRead (fin_de_course_int);
if (Fin_ext > 2000) //If i press on the external end swith
{
analogWrite(Ouverture, 0); //Stops the opening of the "garage"
}
if (Fin_int > 2000) //If i press on the internal end swith
{
analogWrite(Fermeture, 0); //Stops the garage from closing
}
{
PL = analogRead(PL_Pin);
if (PL < Com7) //Condition 1 : PL detects rain
{
if (millis() - ref_pluie >= temporisation_moteur)
{
digitalWrite(Led_Blanche, HIGH);
lcd.setCursor(15,1);
lcd.write(3);
if (Fin_ext < 2000)
{
ref_pluie = millis();
analogWrite(Ouverture, V); //Deploys the roof
analogWrite(Fermeture, 0);
}
}
}
else if (PL > Com8) //Condition 2 : No rain
{
if (millis() - ref_pluie >= temporisation_moteur)
{
digitalWrite(Led_Blanche, LOW);
lcd.setCursor(15,1);
lcd.write(' ');
if (Fin_int < 2000)
{
ref_pluie = millis();
analogWrite(Ouverture, 0);
analogWrite(Fermeture, V); //Returns the roof inside
}
}
}
}
}
// DHT22
{
if (millis() - ref_dht >= IDHT)
{
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature))
{
Serial.println(F("Error reading temperature!"));
}
else
{
Serial.print(F("Temperature: "));
Serial.print(event.temperature);
Serial.println(F("°C"));
}
lcd.setCursor(0,0);
lcd.print("Tp. : ");
lcd.print(event.temperature);
lcd.print(" ");
lcd.write(0);
lcd.print("C");
Blynk.virtualWrite(V0, event.temperature);
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("%"));
Serial.println();
lcd.setCursor(0,1);
lcd.print("Hu. : ");
lcd.print(event.relative_humidity);
lcd.print(" ");
lcd.print("%");
Blynk.virtualWrite(V1, event.relative_humidity);
if (event.relative_humidity > 50)
{
digitalWrite(Ventilateur_dht, HIGH);
}
else if (event.relative_humidity < 50)
{
digitalWrite(Ventilateur_dht, LOW);
}
}
ref_dht = millis();
}
}
// RFID :
{
if ( ! mfrc522.PICC_IsNewCardPresent())
{
void();
}
if ( ! mfrc522.PICC_ReadCardSerial()) //If there are no cards on the module
{
if ((millis() - ref_rfid) >= temps) //Counts 3 seconds then sets the Leds OFF
{
digitalWrite(Led_Verte, LOW);
digitalWrite(Led_Rouge, LOW);
}
return;
}
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
content.toUpperCase();
if (content.substring(1) == "Right Card") //If the right card is detected
{
ref_rfid = millis();
digitalWrite(Led_Verte, HIGH);
digitalWrite(Led_Rouge, LOW);
Blynk.notify("Module RFID : Accès Autorisé");
}
else //If the wrong card is detected
{
ref_rfid = millis();
digitalWrite(Led_Verte, LOW);
digitalWrite(Led_Rouge, HIGH);
Blynk.notify("Module RFID : Accès Refusé");
}
}
}
//end