[HELP] Blynk won't stay connected while using DHT22

Hi All,

I’m trying to make a controller with some sensors on it like the PIR and DHT22. I’ll also will be adding an ssd1306 oled. Everything works pretty well without the DHT22 sensor with random disconnects ever few minutes, but as soon as I start getting the DHT data and sending it to the App i starts disconnecting every few seconds.

Here is the code:

#define BLYNK_DEBUG
#define BLYNK_PRINT Serial 
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <WiFiManager.h> 
#include <ESP8266WebServer.h>
#include <BlynkSimpleEsp8266.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <SimpleTimer.h>

#include "fauxmoESP.h"
fauxmoESP fauxmo;

#define VSWITCH1 V1  //Use a unique virtual pin for each device using the same token / dashboard
#define VSWITCH2 V2
#define VSWITCH3 V5
#define VSWITCH4 V4

//#define pirPin 5 // PIR (Motion Sensor Pin
int pirPin = D0;
int pirState = LOW;
int val = 0;
//WidgetLED MotionLED(V12);
    

// DHT Sensor
#include <DHT.h>
#define DHTPIN D2 //pin gpio 12 in sensor
#define DHTTYPE DHT22   // DHT 22 Change this if you have a DHT11
DHT dht(DHTPIN, DHTTYPE);

char auth[] = "YOUR_BLYNK_TOKEN"; //Get token from Blynk

//on/off callbacks
void turnOn1();
void turnOff1();
void turnOn2();
void turnOff2();
void turnOn3();
void turnOff3();
void turnOn4();
void turnOff4();

/*WemoManager wemoManager;
WemoSwitch *switch1 = NULL;
WemoSwitch *switch2 = NULL;
WemoSwitch *switch3 = NULL;
WemoSwitch *switch4 = NULL;*/

boolean LampState = 0;
boolean SwitchReset = true;   //Flag indicating that the hardware button has been released

const int SWITCH1 = D4;           //On / Off indicator SWITCH1. Onboard SWITCH1 is 13 on Sonoff
const int SWITCH2 = D5;
const int SWITCH3 = D6;
const int SWITCH4 = D7;

float takeLowTime = millis();
long unsigned int pause = 5000;

void checkMotion(){
  float timeNow = millis() - takeLowTime;
  if(timeNow > pause){
    takeLowTime = millis();
    int val = 0;
    val = digitalRead(pirPin);
    Serial.print("Val: ");
    Serial.print(val);
    Serial.print(" - timeNow: ");
    Serial.print(timeNow);
    Serial.print(" - takeLowtime: ");
    Serial.println(takeLowTime);
    
    if(val == HIGH){
      Serial.println("LED: ON");
      //MotionLED.on();
      if(pirState == LOW){
        Serial.println("Motion detected!");
        pirState = HIGH;
      }
    } else {
      Serial.println("LED: OFF");
      if(pirState == HIGH){
//        MotionLED.off();
        Serial.println("Motion ended!");
        pirState = LOW;
      }
    }
  }
}


//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//                        Switch Functions
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Toggle the switch 1 off
void turnOff1() {
    Serial.println("Switch 1 turn off ...");
    digitalWrite(SWITCH1, LOW);
    LampState = 0;
    Blynk.virtualWrite(VSWITCH1, LOW);      // Sync the Blynk button widget state
}

// Toggle the switch 1 on
void turnOn1() {
    Serial.println("Switch 1 turn on ...");
    digitalWrite(SWITCH1, HIGH);
    LampState = 1;
    Blynk.virtualWrite(VSWITCH1, HIGH);     // Sync the Blynk button widget state
    delay(1000);
    turnOff1();
}

// Toggle the switch 2 off
void turnOff2() {
    Serial.println("Switch 2 turn off ...");
    digitalWrite(SWITCH2, LOW);
    Blynk.virtualWrite(VSWITCH2, LOW);      // Sync the Blynk button widget state
}

// Toggle the switch 2 on
void turnOn2() {
    Serial.println("Switch 2 turn on ...");
    digitalWrite(SWITCH2, HIGH);
    LampState = 1;
    Blynk.virtualWrite(VSWITCH2, HIGH);     // Sync the Blynk button widget state
    delay(1000);
    turnOff2();
}

// Toggle the switch 3 off
void turnOff3() {
    Serial.println("Switch 3 turn off ...");
    digitalWrite(SWITCH3, LOW);
    LampState = 0;
    Blynk.virtualWrite(VSWITCH3, LOW);      // Sync the Blynk button widget state
}

// Toggle the switch 3 on
void turnOn3() {
    Serial.println("Switch 3 turn on ...");
    digitalWrite(SWITCH3, HIGH);
    LampState = 1;
    Blynk.virtualWrite(VSWITCH3, HIGH);     // Sync the Blynk button widget state
    delay(1000);
    turnOff3();
}

// Toggle the switch 4 off
void turnOff4() {
    Serial.println("Switch 4 turn off ...");
    digitalWrite(SWITCH4, LOW);
    LampState = 0;
    Blynk.virtualWrite(VSWITCH4, LOW);      // Sync the Blynk button widget state
}


// Toggle the switch 4 on
void turnOn4() {
    Serial.println("Switch 4 turn on ...");
    digitalWrite(SWITCH4, HIGH);
    LampState = 1;
    Blynk.virtualWrite(VSWITCH4, HIGH);     // Sync the Blynk button widget state
    delay(1000);
    turnOff4();
}



// Handle switch changes originating on the Blynk 
BLYNK_WRITE(VSWITCH1){
  int SwitchStatus = param.asInt();
  Serial.print("Blynk switch activated");
  
  if (SwitchStatus){
    turnOn1();
  } else {
    turnOff1();
  }
}

BLYNK_WRITE(VSWITCH2){
  int SwitchStatus = param.asInt();
  Serial.print("Blynk switch activated");
  
  if (SwitchStatus){
    turnOn2();
  } else {
    turnOff2();
  }
}

BLYNK_WRITE(VSWITCH3){
  int SwitchStatus = param.asInt();
  Serial.print("Blynk switch activated");
  
  if (SwitchStatus){
    turnOn3();
  } else {
    turnOff3();
  }
}

BLYNK_WRITE(VSWITCH4){
  int SwitchStatus = param.asInt();
  Serial.print("Blynk switch activated");
  
  if (SwitchStatus){
    turnOn4();
  } else {
    turnOff4();
  }
}

void checkTemp(){
  float h = dht.readHumidity();
  float t = dht.readTemperature();
    
 Blynk.virtualWrite(10, t); // virtual pin 
 Blynk.virtualWrite(11, h); // virtual pin

  Serial.print("Temp: ");
  Serial.print(t);
  Serial.print("Hum: ");
  Serial.println(h);
}

void HWLoop(){
  checkMotion();
  checkTemp();
}
SimpleTimer timer;

void initPIR(){
  Serial.println("Calibrating sensor");
  for(int i=0; i<60; i++){
    Serial.print(".");
    delay(1000);
  }
  Serial.println("Done");
  delay(1000);
}

void setup()      
{
  Serial.begin(115200);
  pinMode(pirPin, INPUT);
  digitalWrite(pirPin, LOW);
  pinMode(SWITCH1, OUTPUT);
  pinMode(SWITCH2, OUTPUT);
  pinMode(SWITCH3, OUTPUT);
  pinMode(SWITCH4, OUTPUT);
  delay(10);
  digitalWrite(SWITCH1, LOW);
  digitalWrite(SWITCH2, LOW);
  digitalWrite(SWITCH3, LOW);
  digitalWrite(SWITCH4, LOW);
  //initPIR();
  WiFiManager wifi;   //WiFiManager intialization.
  wifi.autoConnect("SVController"); //Create AP, if necessary

  // Fauxmo
  fauxmo.addDevice("M B Temp Up");
  fauxmo.addDevice("M B Temp Down");
  fauxmo.addDevice("M B Mode");
  fauxmo.addDevice("M B Power");

  fauxmo.onMessage([](unsigned char device_id, const char * device_name, bool state) {
        Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF");
        if(device_name == "M B Temp Up"){
          if(state){
            turnOn1();
          } else {
            turnOff1();
          }
        } else if(device_name == "M B Temp Down"){
          if(state){
            turnOn2();
          } else {
            turnOff2();
          }
        } else if(device_name == "M B Mode"){
          if(state){
            turnOn3();
          } else {
            turnOff3();
          }
        } else if(device_name == "M B Power"){
          if(state){
            turnOn4();
          } else {
            turnOff4();
          }
        }
    });

  Blynk.config(auth);
  ArduinoOTA.begin();

  timer.setInterval(100, HWLoop);
}

void loop()
{
  //wemoManager.serverLoop();
  Blynk.run();
  fauxmo.handle();
  ArduinoOTA.handle();
  timer.run();
}

Here are some logs:
Preformatted text
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/ v0.4.4 on NodeMCU

[11687] Free RAM: 39800
[11692] Connecting to blynk-cloud.com:8442
[12135] <[02|00|01|00] BLYNK_TOKEN
Val: 1 - timeNow: 12303.00 - takeLowtime: 12565.00
LED: ON
Motion detected!
[12840] Cmd skipped:20
[12840] Cmd skipped:20
Temp: 21.30Hum: 38.30
[12841] >[00|00|01|00]È
[12841] Ready (ping: 276ms).
[12841] <[11|00|03|00]Hver[00]0.4.4[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]NodeMCU[00]build[00]Jan 28 2017 03:45:45[00]
[13282] <[14|00|04|00|0C]vw[00]10[00]21.300
[13752] <[14|00|05|00|0C]vw[00]11[00]38.300
Temp: 21.30Hum: 38.30
[14182] >[00|00|03|00]È
[14183] <[14|00|06|00|0C]vw[00]10[00]21.300
[14613] <[14|00|07|00|0C]vw[00]11[00]38.300
Temp: 21.30Hum: 38.30
[15318] <[14|00|08|00|0C]vw[00]10[00]20.500
[15748] <[14|00|09|00|0C]vw[00]11[00]38.500
[20749] Cmd error
Temp: 20.50Hum: 38.50
[20750] Connecting to blynk-cloud.com:8442
Val: 1 - timeNow: 21819.00 - takeLowtime: 34384.00
LED: ON
[34659] Cmd skipped:20
[34659] Cmd skipped:20
Temp: 20.50Hum: 38.50
[34659] Connecting to blynk-cloud.com:8442
Val: 1 - timeNow: 14274.00 - takeLowtime: 48658.00
LED: ON
[48933] Cmd skipped:20
[48933] Cmd skipped:20
Temp: 20.50Hum: 38.50
[48933] Connecting to blynk-cloud.com:8442
Val: 1 - timeNow: 14374.00 - takeLowtime: 63032.00
LED: ON
[63303] Cmd skipped:20
[63303] Cmd skipped:20
Temp: nanHum: nan
[63303] Connecting to blynk-cloud.com:8442
Val: 1 - timeNow: 6449.00 - takeLowtime: 69481.00
LED: ON
[69753] Cmd skipped:20
[69753] Cmd skipped:20
Temp: nanHum: nan
[69753] Connecting to blynk-cloud.com:8442
[70261] <[02|00|01|00] BLYNK_TOKEN
[75262] Cmd error
Val: 1 - timeNow: 5782.00 - takeLowtime: 75263.00
LED: ON
[75538] Cmd skipped:20
[75538] Cmd skipped:20
Temp: 20.50Hum: 38.50
[75538] Connecting to blynk-cloud.com:8442
Val: 1 - timeNow: 14217.00 - takeLowtime: 89480.00
LED: ON
[89755] Cmd skipped:20
[89755] Cmd skipped:20
Temp: 20.50Hum: 38.50
[89756] Connecting to blynk-cloud.com:8442
[90229] <[02|00|01|00] BLYNK_TOKEN
[90741] Cmd skipped:20
[90741] Cmd skipped:20
Temp: 20.50Hum: 38.50
[90742] >[00|00|01|00]È
[90742] Ready (ping: 2ms).
[90742] <[11|00|18|00]Hver[00]0.4.4[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]NodeMCU[00]build[00]Jan 28 2017 03:45:45[00]
[91253] <[14|00|19|00|0C]vw[00]10[00]20.500
[91765] <[14|00|1A|00|0C]vw[00]11[00]38.500
Temp: 20.50Hum: 38.50
[92280] >[00|00|18|00]È
[92555] <[14|00|1B|00|0C]vw[00]10[00]20.500
[92970] <[14|00|1C|00|0C]vw[00]11[00]38.500
Temp: 20.50Hum: 38.50
[93382] <[14|00|1D|00|0C]vw[00]10[00]20.500
[93794] <[14|00|1E|00|0C]vw[00]11[00]38.500
Temp: 20.50Hum: 38.50
[94206] <[14|00|1F|00|0C]vw[00]10[00]20.500
[94618] <[14|00] [00|0C]vw[00]11[00]38.500
Temp: 20.50Hum: 38.50
Val: 1 - timeNow: 5551.00 - takeLowtime: 95031.00
LED: ON
[95306] <[14|00]![00|0C]vw[00]10[00]20.500
[100307] Cmd error
[100310] Cmd skipped:20
Temp: 20.50Hum: 38.50
[100311] Connecting to blynk-cloud.com:8442
Val: 1 - timeNow: 18647.00 - takeLowtime: 113678.00
LED: ON
[113954] Cmd skipped:20
[113954] Cmd skipped:20
Temp: 20.50Hum: 38.50
[113954] Connecting to blynk-cloud.com:8442
[114395] <[02|00|01|00] BLYNK_TOKEN
[119396] Cmd error
Val: 1 - timeNow: 5719.00 - takeLowtime: 119397.00
LED: ON
[119672] Cmd skipped:20
[119672] Cmd skipped:20
Temp: 20.50Hum: 38.50
[119673] Connecting to blynk-cloud.com:8442

Why do you need to call the HWLOOP function 10 times every second?

A: you don’t.

How can the DHT have time to work in such a short time?

A: it can’t.

I remember when I first started using these sensors the docs stated to request data from the sensors at most every two seconds or they were prone to errors and lock-ups that could take a power cycle to fix.

I’ve tried different times all the way up to once every 10 seconds, but with the same issues. Is there a specific time for each sensor? Also when I run the code with Blynk taken out it runs fine.

PS, forgot to mention that I’m using the NodeMCU v1.0 ESP-12E in case that helps.

Here are more logs after I disabled the sensors and only left the buttons working:

[57512] Connecting to blynk-cloud.com:8442
[57956] <[02|00|01|00] BLYNK_TOKEN
[58400] >[00|00|01|00]È
[58400] Ready (ping: 1ms).
[58401] <[11|00|02|00]Hver[00]0.4.4[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]NodeMCU[00]build[00]Jan 28 2017 14:32:48[00]
[63406] Cmd error
[63409] Login timeout
[63409] Connecting to blynk-cloud.com:8442
[78154] Connecting to blynk-cloud.com:8442
[79087] <[02|00|01|00] BLYNK_TOKEN
[79510] >[00|00|01|00]È
[79510] Ready (ping: 1ms).
[79510] <[11|00|03|00]Hver[00]0.4.4[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]NodeMCU[00]build[00]Jan 28 2017 14:32:48[00]
[79937] >[14]0F[00|05]
[79937] >vr[00]10
[79937] >[00|00|03|00]È
[80012] >[14]jÍ[00|05]
[80012] >vr[00]11
[81240] >[14]"x[00|05]
[81240] >vr[00]11
[81241] >[14]Mù[00|05]
[81242] >vr[00]10
[82467] >[14|03|09|00|05]
[82467] >vr[00]10
[82998] >[14]T7[00|06]
[82999] >vw[00]1[00]1
Blynk switch activatedSwitch 1 turn on …
[82999] <[14]T7[00|06]vw[00]1[00]1
Switch 1 turn off …
[84460] <[14]T7[00|06]vw[00]1[00]0
[89461] Cmd error
[89462] Login timeout
[89462] Connecting to blynk-cloud.com:8442
[89883] <[02|00|01|00] BLYNK_TOKEN
[90297] >[00|00|01|00]È
[90297] Ready (ping: 1ms).
[90297] <[11|00|04|00]Hver[00]0.4.4[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]NodeMCU[00]build[00]Jan 28 2017 14:32:48[00]
[95302] Cmd error
[95305] Login timeout
[95306] Connecting to blynk-cloud.com:8442
[95697] <[02|00|01|00] BLYNK_TOKEN
[96186] >[00|00|01|00]È
[96186] Ready (ping: 101ms).
[96186] <[11|00|05|00]Hver[00]0.4.4[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]NodeMCU[00]build[00]Jan 28 2017 14:32:48[00]
[101191] Cmd error
[101195] Login timeout
[101195] Connecting to blynk-cloud.com:8442
[115152] Connecting to blynk-cloud.com:8442
[129376] Connecting to blynk-cloud.com:8442
[135776] Connecting to blynk-cloud.com:8442
[142050] Connecting to blynk-cloud.com:8442
[148300] Connecting to blynk-cloud.com:8442
[154549] Connecting to blynk-cloud.com:8442
[160874] Connecting to blynk-cloud.com:8442
[167124] Connecting to blynk-cloud.com:8442
[167559] <[02|00|01|00] BLYNK_TOKEN
[172560] Cmd error
[172564] Connecting to blynk-cloud.com:8442
[178698] Connecting to blynk-cloud.com:8442
[184947] Connecting to blynk-cloud.com:8442
[191223] Connecting to blynk-cloud.com:8442
[191626] <[02|00|01|00] BLYNK_TOKEN
[196627] Cmd error
[196630] Connecting to blynk-cloud.com:8442
[202796] Connecting to blynk-cloud.com:8442
[203207] <[02|00|01|00] BLYNK_TOKEN
[203616] >[00|00|01|00]È
[203616] Ready (ping: 1ms).
[203616] <[11|00|06|00]Hver[00]0.4.4[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]NodeMCU[00]build[00]Jan 28 2017 14:32:48[00]
[204028] >[00|00|06|00]È
[214029] <[06|00|07|00|00]
[214436] >[00|00|07|00]È
[224437] <[06|00|08|00|00]
[224844] >[00|00|08|00]È
[234845] <[06|00|09|00|00]
[235347] >[00|00|09|00]È
[245348] <[06|00|0A|00|00]
[245792] >[00|00|0A|00]È
[255793] <[06|00|0B|00|00]
[256237] >[00|00|0B|00]È
[266237] <[06|00|0C|00|00]
[266645] >[00|00|0C|00]È
[276646] <[06|00|0D|00|00]
[277054] >[00|00|0D|00]È
[287055] <[06|00|0E|00|00]
[287469] >[00|00|0E|00]È
[297470] <[06|00|0F|00|00]
[302470] Cmd error
[302472] Connecting to blynk-cloud.com:8442
[316139] Connecting to blynk-cloud.com:8442
[322539] Connecting to blynk-cloud.com:8442
[328788] Connecting to blynk-cloud.com:8442
[335038] Connecting to blynk-cloud.com:8442
[341287] Connecting to blynk-cloud.com:8442
[347537] Connecting to blynk-cloud.com:8442
[347938] <[02|00|01|00] BLYNK_TOKEN
[352939] Cmd error
[352943] Connecting to blynk-cloud.com:8442
[353453] <[02|00|01|00] BLYNK_TOKEN
[353930] >[00|00|01|00]È
[353930] Ready (ping: 1ms).
[353930] <[11|00|10|00]Hver[00]0.4.4[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]NodeMCU[00]build[00]Jan 28 2017 14:32:48[00]
[354366] >[00|00|10|00]È
[364367] <[06|00|11|00|00]
[364798] >[00|00|11|00]È
[374799] <[06|00|12|00|00]
[379799] Cmd error

The disconnects are happening randomly and whenever I click a button.

@Robert_Haddad millis() are unsigned long not float. When you have made the change post your revised code and Serial Monitor output.

@Costas thanks for that, it stayed stable for a long time with out the DHT22 (checkTemp) function, but after I activated it, I’m getting disconnects. I can’t see where its going wrong. Here is the log and the code is below it:

    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.4 on NodeMCU

[20684] Free RAM: 39840
[20688] Connecting to blynk-cloud.com:8442
[21108] <[02|00|01|00] 0ddec6fd1016421d910f15a4c4938a14
[21518] >[00|00|01|00]È
[21518] Ready (ping: 1ms).
[21518] <[11|00|01|00]Hver[00]0.4.4[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]NodeMCU[00]build[00]Jan 28 2017 15:30:46[00]
[21932] >[00|00|01|00]È
[22962] <[14|00|02|00|0C]vw[00]10[00]21.700
[23413] <[14|00|03|00|0C]vw[00]11[00]43.200
Temp: 21.70Hum: 43.20
[24962] <[14|00|04|00|0C]vw[00]10[00]21.700
[25371] <[14|00|05|00|0D]vw[00]11[00]43.0000
Temp: 21.70Hum: 43.00
[26962] <[14|00|06|00|0C]vw[00]10[00]21.700
[27370] <[14|00|07|00|0D]vw[00]11[00]43.0000
Temp: 21.70Hum: 43.00
[28962] <[14|00|08|00|0C]vw[00]10[00]21.700
[33963] Cmd error
[33964] Cmd skipped:20
Temp: 21.70Hum: 43.00
[33965] Connecting to blynk-cloud.com:8442
[47651] Cmd skipped:20
[47652] Cmd skipped:20
Temp: 21.70Hum: 43.00
[47652] Connecting to blynk-cloud.com:8442
[61976] Cmd skipped:20
[61976] Cmd skipped:20
Temp: 21.70Hum: 42.90
[61976] Connecting to blynk-cloud.com:8442

Here’s the code:

#define BLYNK_DEBUG
#define BLYNK_PRINT Serial 
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <WiFiManager.h> 
#include <ESP8266WebServer.h>
#include <BlynkSimpleEsp8266.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <SimpleTimer.h>
#include "math.h"

#include "fauxmoESP.h"
fauxmoESP fauxmo;

#define VSWITCH1 V1  //Use a unique virtual pin for each device using the same token / dashboard
#define VSWITCH2 V2
#define VSWITCH3 V5
#define VSWITCH4 V4

//#define pirPin 5 // PIR (Motion Sensor Pin
int pirPin = D0;
int pirState = LOW;
int val = 0;
//WidgetLED MotionLED(V12);


// DHT Sensor
#include <DHT.h>
#define DHTPIN D2 //pin gpio 12 in sensor
#define DHTTYPE DHT22   // DHT 22 Change this if you have a DHT11
DHT dht(DHTPIN, DHTTYPE);

char auth[] = "0ddec6fd1016421d910f15a4c4938a14"; //Get token from Blynk

//on/off callbacks
void turnOn1();
void turnOff1();
void turnOn2();
void turnOff2();
void turnOn3();
void turnOff3();
void turnOn4();
void turnOff4();

boolean LampState = 0;
boolean SwitchReset = true;   //Flag indicating that the hardware button has been released

const int SWITCH1 = D4;           //On / Off indicator SWITCH1. Onboard SWITCH1 is 13 on Sonoff
const int SWITCH2 = D5;
const int SWITCH3 = D6;
const int SWITCH4 = D7;

unsigned long takeLowTime = millis();
unsigned long pause = 5000;

void checkMotion(){
  unsigned long timeNow = millis() - takeLowTime;
  if(timeNow > pause){
    takeLowTime = millis();
    int val = 0;
    val = digitalRead(pirPin);
    Serial.print("Val: ");
    Serial.print(val);
    Serial.print(" - timeNow: ");
    Serial.print(timeNow);
    Serial.print(" - takeLowtime: ");
    Serial.println(takeLowTime);
    
    if(val == HIGH){
      Serial.println("LED: ON");
      //MotionLED.on();
      if(pirState == LOW){
        Serial.println("Motion detected!");
        pirState = HIGH;
      }
    } else {
      Serial.println("LED: OFF");
      if(pirState == HIGH){
//        MotionLED.off();
        Serial.println("Motion ended!");
        pirState = LOW;
      }
    }
  }
}


//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//                        Switch Functions
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Toggle the switch 1 off
void turnOff1() {
    Serial.println("Switch 1 turn off ...");
    digitalWrite(SWITCH1, LOW);
    LampState = 0;
    Blynk.virtualWrite(VSWITCH1, LOW);      // Sync the Blynk button widget state
}

// Toggle the switch 1 on
void turnOn1() {
    Serial.println("Switch 1 turn on ...");
    digitalWrite(SWITCH1, HIGH);
    LampState = 1;
    Blynk.virtualWrite(VSWITCH1, HIGH);     // Sync the Blynk button widget state
    delay(1000);
    turnOff1();
}

// Toggle the switch 2 off
void turnOff2() {
    Serial.println("Switch 2 turn off ...");
    digitalWrite(SWITCH2, LOW);
    Blynk.virtualWrite(VSWITCH2, LOW);      // Sync the Blynk button widget state
}

// Toggle the switch 2 on
void turnOn2() {
    Serial.println("Switch 2 turn on ...");
    digitalWrite(SWITCH2, HIGH);
    LampState = 1;
    Blynk.virtualWrite(VSWITCH2, HIGH);     // Sync the Blynk button widget state
    delay(1000);
    turnOff2();
}

// Toggle the switch 3 off
void turnOff3() {
    Serial.println("Switch 3 turn off ...");
    digitalWrite(SWITCH3, LOW);
    LampState = 0;
    Blynk.virtualWrite(VSWITCH3, LOW);      // Sync the Blynk button widget state
}

// Toggle the switch 3 on
void turnOn3() {
    Serial.println("Switch 3 turn on ...");
    digitalWrite(SWITCH3, HIGH);
    LampState = 1;
    Blynk.virtualWrite(VSWITCH3, HIGH);     // Sync the Blynk button widget state
    delay(1000);
    turnOff3();
}

// Toggle the switch 4 off
void turnOff4() {
    Serial.println("Switch 4 turn off ...");
    digitalWrite(SWITCH4, LOW);
    LampState = 0;
    Blynk.virtualWrite(VSWITCH4, LOW);      // Sync the Blynk button widget state
}

// Toggle the switch 4 on
void turnOn4() {
    Serial.println("Switch 4 turn on ...");
    digitalWrite(SWITCH4, HIGH);
    LampState = 1;
    Blynk.virtualWrite(VSWITCH4, HIGH);     // Sync the Blynk button widget state
    delay(1000);
    turnOff4();
}



// Handle switch changes originating on the Blynk 
BLYNK_WRITE(VSWITCH1){
  int SwitchStatus = param.asInt();
  Serial.println("Blynk switch activated");
  
  if (SwitchStatus){
    turnOn1();
  } else {
    turnOff1();
  }
}

BLYNK_WRITE(VSWITCH2){
  int SwitchStatus = param.asInt();
  Serial.println("Blynk switch activated");
  
  if (SwitchStatus){
    turnOn2();
  } else {
    turnOff2();
  }
}

BLYNK_WRITE(VSWITCH3){
  int SwitchStatus = param.asInt();
  Serial.println("Blynk switch activated");
  
  if (SwitchStatus){
    turnOn3();
  } else {
    turnOff3();
  }
}

BLYNK_WRITE(VSWITCH4){
  int SwitchStatus = param.asInt();
  Serial.println("Blynk switch activated");
  
  if (SwitchStatus){
    turnOn4();
  } else {
    turnOff4();
  }
}

void checkTemp(){
  float h = dht.readHumidity();
  
  float t = dht.readTemperature();
  if(!isnan(h) && !isnan(t)){
    Blynk.virtualWrite(10, t); // virtual pin 
    Blynk.virtualWrite(11, h); // virtual pin

    Serial.print("Temp: ");
    Serial.print(t);
    Serial.print("Hum: ");
    Serial.println(h);
  }
}

void HWLoop(){
  //checkMotion();
  checkTemp();
}
SimpleTimer timer;

void initPIR(){
  Serial.println("Calibrating sensor");
  for(int i=0; i<60; i++){
    Serial.print(".");
    delay(1000);
  }
  Serial.println("Done");
  delay(1000);
}

void setup()      
{
  Serial.begin(115200);
  pinMode(pirPin, INPUT);
  digitalWrite(pirPin, LOW);
  pinMode(SWITCH1, OUTPUT);
  pinMode(SWITCH2, OUTPUT);
  pinMode(SWITCH3, OUTPUT);
  pinMode(SWITCH4, OUTPUT);
  delay(10);
  digitalWrite(SWITCH1, LOW);
  digitalWrite(SWITCH2, LOW);
  digitalWrite(SWITCH3, LOW);
  digitalWrite(SWITCH4, LOW);
  //initPIR();
  WiFiManager wifi;   //WiFiManager intialization.
  wifi.autoConnect("SVController"); //Create AP, if necessary

  // Fauxmo
  fauxmo.addDevice("M B Temp Up");
  fauxmo.addDevice("M B Temp Down");
  fauxmo.addDevice("M B Mode");
  fauxmo.addDevice("M B Power");

  fauxmo.onMessage([](unsigned char device_id, const char * device_name, bool state) {
        Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF");
        if(device_name == "M B Temp Up"){
          if(state){
            turnOn1();
          } else {
            turnOff1();
          }
        } else if(device_name == "M B Temp Down"){
          if(state){
            turnOn2();
          } else {
            turnOff2();
          }
        } else if(device_name == "M B Mode"){
          if(state){
            turnOn3();
          } else {
            turnOff3();
          }
        } else if(device_name == "M B Power"){
          if(state){
            turnOn4();
          } else {
            turnOff4();
          }
        }
    });

  Blynk.config(auth);
  ArduinoOTA.begin();

  timer.setInterval(2000, HWLoop);
}

void loop()
{
  Blynk.run();
  fauxmo.handle();
  ArduinoOTA.handle();
  timer.run();
}

Thank you all for your continued help :slight_smile:

@Robert_Haddad,
Avoid all your delays…it will help Blynk to keep doing its magic for you…:wink:

Thanks for the suggestion, but it made no difference