BLYNK
BLYNK.IO       📲 GETTING STARTED       đź“— DOCS       âť“HELP CENTER       👉 SKETCH BUILDER

Seems cant connect to Blynk server

Yes, you can’t use delay with blynk, especially in the loop. Check this out :
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

You should use blynktimer or simpletimer instead.

@PeteKnight @John93 Thank you for the advice
So I should change all the delay() to

timer.setInterval(1000L, sensorDataSent);

what should I replace with the " sensorDataSent", if I just want a delay??

@PeteKnight @John93
Here is my update. I created a new void and change all the delay to simple timer. but nothing changed. the connection is still not stable.

void delaytimer()
{
  int count = 0;
  Serial.print("delay");
  Serial.print(count);
  count++;
}
void sensorDataSend()
{
  timer.setInterval(200L, delaytimer);

  digitalWrite(LED_BUILTIN, HIGH);
  timer.setInterval(20L, delaytimer);
  digitalWrite(LED_BUILTIN, LOW);
  timer.setInterval(20L, delaytimer);

  hum = dht.readHumidity();
  temp = dht.readTemperature();
  //Print temp and humidity values to serial monitor(help checking the data)
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print(" %, Temp: ");
  Serial.print(temp);
  Serial.println(" Celsius");

  if (temp >= 20) //running the motor with sensor
  {
    digitalWrite(led, HIGH); //switch on motor
    timer.setInterval(100L, delaytimer);
  }
  else
  {
    digitalWrite(led, LOW); //switch off motor
    timer.setInterval(100L, delaytimer);
  }
  timer.setInterval(100L, delaytimer); //Delay 2 sec.

  //  sensorValue = analogRead(A5);         // reading sensor from analog pin
  //  Blynk.virtualWrite(V1, sensorValue);  // sending sensor value to Blynk app
}

For your reference here is the full version of my code

//Libraries
#include <DHT.h>;
#include <ESP8266_Lib.h>
//#include <ESP8266WiFi.h>;
#include "WiFiEsp.h"
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

#define DHTPIN 10     // connect the dht output to 10(pwm) 
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define led 8   //connect to led to pin 8-->change to motor later
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

#define BLYNK_TEMPLATE_ID "TMPL5vzGTnYc"
#define BLYNK_DEVICE_NAME "DHT22"
#define BLYNK_PRINT Serial
#include <BlynkSimpleShieldEsp8266.h>
//#include <SimpleTimer.h>
#define EspSerial Serial1
#define ESP8266_BAUD 115200
#define BLYNK_AUTH_TOKEN "***";

//Variables
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value

char ssid[] = "GoCO"; //You can replace the wifi name to your wifi
char pass[] = "***";  //Type password of your wifi.
int status = WL_IDLE_STATUS;     // the Wifi radio's status

char auth[] = "***";
ESP8266 wifi(&EspSerial);
SimpleTimer timer;

//int virtual_pin_value = param.asInt();
//param.asStrng()
//param.asFloat()
//BlynkTimer timer;

void delaytimer()
{
  int count = 0;
  Serial.print("delay");
  Serial.print(count);
  count++;
}
void setup()
{
  // initialize serial for debugging
  Serial.begin(115200);
  // initialize serial for ESP module
  Serial1.begin(115200);
  // initialize ESP module
  WiFi.init(&Serial1);
  pinMode(LED_BUILTIN, OUTPUT);
  dht.begin(); //start running the dht
  pinMode (led, OUTPUT); //set the led(pin8) as output
  pinMode (LED_BUILTIN, OUTPUT); //set the led on board as output
  WifiStatus();
}

void WifiStatus()
{

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    digitalWrite(LED_BUILTIN, LOW); //no on board led if no wifi
    Serial.println("WiFi is not connected");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    digitalWrite(LED_BUILTIN, HIGH);//connecting have on board led
    Serial.print("Connecting to ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  // you're connected now, so print out the data
  Serial.println("You're connected to the network");
  timer.setInterval(1000L, sensorDataSend); //timer will run every sec
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass);
  sensorDataSend();
}

void sensorDataSend()
{
  timer.setInterval(200L, delaytimer);

  digitalWrite(LED_BUILTIN, HIGH);
  timer.setInterval(20L, delaytimer);
  digitalWrite(LED_BUILTIN, LOW);
  timer.setInterval(20L, delaytimer);

  hum = dht.readHumidity();
  temp = dht.readTemperature();
  //Print temp and humidity values to serial monitor(help checking the data)
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print(" %, Temp: ");
  Serial.print(temp);
  Serial.println(" Celsius");

  if (temp >= 20) //running the motor with sensor
  {
    digitalWrite(led, HIGH); //switch on motor
    timer.setInterval(100L, delaytimer);
  }
  else
  {
    digitalWrite(led, LOW); //switch off motor
    timer.setInterval(100L, delaytimer);
  }
  timer.setInterval(100L, delaytimer); //Delay 2 sec.

  //  sensorValue = analogRead(A5);         // reading sensor from analog pin
  //  Blynk.virtualWrite(V1, sensorValue);  // sending sensor value to Blynk app
}
BLYNK_WRITE(V1) // Executes when the value of virtual pin 1 changes
{
  if (param.asInt() == 1)
  {
    // execute this code if the switch widget is now ON
    sensorDataSend();
  }
  else
  {
    // execute this code if the switch widget is now OFF
    digitalWrite(led, LOW);
  }
}
void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer
}

It must be in the beginning of the sketch.

Try this :

/*************************************************************
  WARNING!
    It's very tricky to get it working. Please read this article:
    http://help.blynk.cc/hardware-and-libraries/arduino/esp8266-with-at-firmware

  This example shows how value can be pushed from Arduino to
  the Blynk App.

  WARNING :
  For this example you'll need Adafruit DHT sensor libraries:
    https://github.com/adafruit/Adafruit_Sensor
    https://github.com/adafruit/DHT-sensor-library

  App project setup:
    Value Display widget attached to V5
    Value Display widget attached to V6
 *************************************************************/

// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID           "TMPLxxxxxx"
#define BLYNK_DEVICE_NAME           "Device"
#define BLYNK_AUTH_TOKEN            "YourAuthToken"


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <DHT.h>

char auth[] = BLYNK_AUTH_TOKEN;

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

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 38400

ESP8266 wifi(&EspSerial);

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

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

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

// 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()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  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);
  Blynk.virtualWrite(V6, t);
}

void setup()
{
  // Debug console
  Serial.begin(115200);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run();
  timer.run();
}

@John93 As I said before, i don’t think this change help much but I follow your advice and change it to this

#define BLYNK_TEMPLATE_ID "TMPL5vzGTnYc"
#define BLYNK_DEVICE_NAME "DHT22"
#define BLYNK_AUTH_TOKEN "***";
#define BLYNK_PRINT Serial

//Libraries
#include <DHT.h>;
#include <ESP8266_Lib.h>
//#include <ESP8266WiFi.h>;
#include "WiFiEsp.h"
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

#define DHTPIN 10     // connect the dht output to 10(pwm) 
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define led 8   //connect to led to pin 8-->change to motor later
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

#include <BlynkSimpleShieldEsp8266.h>
//#include <SimpleTimer.h>
#define EspSerial Serial1
#define ESP8266_BAUD 115200

//Variables
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value

char ssid[] = "GoCO"; //You can replace the wifi name to your wifi
char pass[] = "29882992";  //Type password of your wifi.
int status = WL_IDLE_STATUS;     // the Wifi radio's status

char auth[] = "***";
ESP8266 wifi(&EspSerial);
SimpleTimer timer;

//int virtual_pin_value = param.asInt();
//param.asStrng()
//param.asFloat()
//BlynkTimer timer;

void delaytimer()
{
  int count = 0;
  Serial.print("delay");
  Serial.print(count);
  count++;
}
void setup()
{
  // initialize serial for debugging
  Serial.begin(115200);
  // initialize serial for ESP module
  Serial1.begin(115200);
  // initialize ESP module
  WiFi.init(&Serial1);
  pinMode(LED_BUILTIN, OUTPUT);
  dht.begin(); //start running the dht
  pinMode (led, OUTPUT); //set the led(pin8) as output
  pinMode (LED_BUILTIN, OUTPUT); //set the led on board as output
  WifiStatus();
}

void WifiStatus()
{

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    digitalWrite(LED_BUILTIN, LOW); //no on board led if no wifi
    Serial.println("WiFi is not connected");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    digitalWrite(LED_BUILTIN, HIGH);//connecting have on board led
    Serial.print("Connecting to ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  // you're connected now, so print out the data
  Serial.println("You're connected to the network");
  timer.setInterval(1000L, sensorDataSend); //timer will run every sec
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass);
  sensorDataSend();
}

void sensorDataSend()
{
  timer.setInterval(200L, delaytimer);

  digitalWrite(LED_BUILTIN, HIGH);
  timer.setInterval(20L, delaytimer);
  digitalWrite(LED_BUILTIN, LOW);
  timer.setInterval(20L, delaytimer);

  hum = dht.readHumidity();
  temp = dht.readTemperature();
  //Print temp and humidity values to serial monitor(help checking the data)
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print(" %, Temp: ");
  Serial.print(temp);
  Serial.println(" Celsius");

  if (temp >= 20) //running the motor with sensor
  {
    digitalWrite(led, HIGH); //switch on motor
    timer.setInterval(100L, delaytimer);
  }
  else
  {
    digitalWrite(led, LOW); //switch off motor
    timer.setInterval(100L, delaytimer);
  }
  timer.setInterval(100L, delaytimer);
  //  sensorValue = analogRead(A5);         // reading sensor from analog pin
  //  Blynk.virtualWrite(V1, sensorValue);  // sending sensor value to Blynk app
}
BLYNK_WRITE(V1) // Executes when the value of virtual pin 1 changes
{
  if (param.asInt() == 1)
  {
    // execute this code if the switch widget is now ON
    sensorDataSend();
  }
  else
  {
    // execute this code if the switch widget is now OFF
    digitalWrite(led, LOW);
  }
}
void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer
}

and the result is remains the same

14:03:22.700 -> [36283] Connected to WiFi
14:03:58.008 -> [71605] Ready (ping: 14ms).
14:04:58.433 -> [132027] Ready (ping: 13ms).
14:05:33.753 -> [167344] Ready (ping: 28ms).
14:06:09.052 -> [202650] Ready (ping: 14ms).
14:06:44.361 -> [237958] Ready (ping: 13ms).
14:07:19.650 -> [273276] Ready (ping: 13ms).

You’ve totally mis-understood the way in which timers are supposed to be used.
You aren’t meant to be using a timer to simulate a delay.
The main delay in your original code was there to ensure that the sensor was only read once every 2 seconds. In reality, when using a DHT sensor, that really ought to be every 5 seconds.

To achieve this without a delay, a timer is defined which will call the code to read the sensor once every 2 (but preferably 5) seconds.

If you also want to flash an LED on and off when the sensor is read then you’d either use a timeout timer, or a 3rd party library like Ticker.

You can read about timeout timers, along with the correct way to use interval timers, here…

but for now I would simply focus on using a timer to read the sensor, then add-in the LED flashing code later.

Pete.

@PeteKnight I deleted some of the timer and delay.

#define BLYNK_TEMPLATE_ID "TMPL5vzGTnYc"
#define BLYNK_DEVICE_NAME "DHT22"
#define BLYNK_AUTH_TOKEN "";
#define BLYNK_PRINT Serial

//Libraries
#include <DHT.h>;
#include <ESP8266_Lib.h>
//#include <ESP8266WiFi.h>;
#include "WiFiEsp.h"
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

#define DHTPIN 10     // connect the dht output to 10(pwm) 
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define led 8   //connect to led to pin 8-->change to motor later
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

#include <BlynkSimpleShieldEsp8266.h>
//#include <SimpleTimer.h>
#define EspSerial Serial1
#define ESP8266_BAUD 115200

//Variables
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value

char ssid[] = "GoCO"; //You can replace the wifi name to your wifi
char pass[] = "";  //Type password of your wifi.
int status = WL_IDLE_STATUS;     // the Wifi radio's status

char auth[] = "";
ESP8266 wifi(&EspSerial);
SimpleTimer timer;

//int virtual_pin_value = param.asInt();
//param.asStrng()
//param.asFloat()
//BlynkTimer timer;

void delaytimer()
{
  int count = 0;
  Serial.print("delay");
  Serial.print(count);
  count++;
}
void setup()
{
  // initialize serial for debugging
  Serial.begin(115200);
  // initialize serial for ESP module
  Serial1.begin(115200);
  // initialize ESP module
  WiFi.init(&Serial1);
  pinMode(LED_BUILTIN, OUTPUT);
  dht.begin(); //start running the dht
  pinMode (led, OUTPUT); //set the led(pin8) as output
  pinMode (LED_BUILTIN, OUTPUT); //set the led on board as output
  WifiStatus();
}

void WifiStatus()
{

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    digitalWrite(LED_BUILTIN, LOW); //no on board led if no wifi
    Serial.println("WiFi is not connected");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    digitalWrite(LED_BUILTIN, HIGH);//connecting have on board led
    Serial.print("Connecting to ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  // you're connected now, so print out the data
  Serial.println("You're connected to the network");
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass);
  timer.setTimeout(3600000L, [] () {} ); // dummy/sacrificial Function
  timer.setInterval(5000L, sensorDataSend); // call the take_sensor_reading() function every 5 seconds
  sensorDataSend();
}

void sensorDataSend()
{
  //timer.setInterval(200L, delaytimer);

  digitalWrite(LED_BUILTIN, HIGH);
  timer.setTimeout(20, delaytimer);
  digitalWrite(LED_BUILTIN, LOW);
  timer.setTimeout(20, delaytimer);

  hum = dht.readHumidity();
  temp = dht.readTemperature();
  //Print temp and humidity values to serial monitor(help checking the data)
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print(" %, Temp: ");
  Serial.print(temp);
  Serial.println(" Celsius");

  if (temp >= 20) //running the motor with sensor
  {
    digitalWrite(led, HIGH); //switch on motor
  }
  else
  {
    digitalWrite(led, LOW); //switch off motor
  }
  //  sensorValue = analogRead(A5);         // reading sensor from analog pin
  //  Blynk.virtualWrite(V1, sensorValue);  // sending sensor value to Blynk app
}
BLYNK_WRITE(V1) // Executes when the value of virtual pin 1 changes
{
  if (param.asInt() == 1)
  {
    // execute this code if the switch widget is now ON
    sensorDataSend();
  }
  else
  {
    // execute this code if the switch widget is now OFF
    digitalWrite(led, LOW);
  }
}
void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer
}

But it doesnt help, what should I do? am I still misunderstanding sth?

14:48:38.646 -> [255444] Ready (ping: 14ms).
14:49:03.957 -> [280749] Ready (ping: 14ms).

Please post your full serial monitor output from device boot-up.

Pete.

@PeteKnight thank you for your quick reply. here is the serial monitor output

15:17:38.094 -> [WiFiEsp] Initializing ESP module
15:17:39.102 -> [WiFiEsp] >>> TIMEOUT >>>
15:17:44.046 -> [WiFiEsp] >>> TIMEOUT >>>
15:17:44.046 -> [WiFiEsp] End tag not found
15:17:44.046 -> [WiFiEsp] Warning: Unsupported firmware 
15:17:48.063 -> Connecting to GoCO
15:17:53.782 -> [WiFiEsp] Connected to GoCO
15:17:53.782 -> You're connected to the network
15:17:53.782 -> [15685] 
15:17:53.782 ->     ___  __          __
15:17:53.782 ->    / _ )/ /_ _____  / /__
15:17:53.782 ->   / _  / / // / _ \/  '_/
15:17:53.782 ->  /____/_/\_, /_//_/_/\_\
15:17:53.782 ->         /___/ v1.0.1 on Arduino Uno
15:17:53.829 -> 
15:17:54.298 -> [16198] Connecting to GoCO
15:18:07.348 -> [29230] AT version:1.6.2.0(Apr 13 2018 11:10:59)
15:18:07.348 -> SDK version:2.2.1(6ab97 
15:18:13.382 -> [35286] +CIFSR:STAIP,"192.168.1.201"
+CIFSR:STAMAC,"24:a1:60:3d:86:6b"
15:18:13.382 -> 
15:18:13.382 -> [35287] Connected to WiFi
15:18:23.678 -> [45596] Ready (ping: 13ms).
15:18:59.194 -> [81078] Ready (ping: 18ms).
15:19:34.510 -> [116410] Ready (ping: 28ms).

You’re using SoftwareSerial on an Uno and attempting to use a baud rate of 115200, yet SoftwareSerial for AVR devices doesn’t work successfully at rates higher than 9600.

I have no idea what the WiFiEsp library does, but it’s throwing some error messages that you’re ignoring.

Exactly what hardware are you using?

Pete.

@PeteKnight I am currently using an UNO board, ESP-01s(ESP8266)(wifi board) and a AM2302(DHT22) (temperature sensor) and a DC light bulb. The baud rate is set to be 115200 due to the ESP8266. Its need to be run under 115200. I tried to change it back to 9600 but the esp module cannot not be initialize and cannot even connect to wifi.

You need to set your ESP-01 to work at 9600 via an AT command, otherwise this hardware setup won’t work.
If you don’t have the necessary hardware or skills to do this then you could switch to a Mega and use a physical UART, but TBH you’d be better off dumping the whole Arduino mess and going for a NodeMCU or ESP32 with built-in WiFi connectivity.

Pete.

@PeteKnight thx for your advice, I think I have changed the baud rate of ESP to 9600 now.
the serial monitor become

17:23:15.810 -> [WiFiEsp] Initializing ESP module
17:23:19.507 -> [WiFiEsp] Initilization successful - 2.2.1
17:23:19.554 -> Connecting to GoCO
17:23:25.319 -> [WiFiEsp] Connected to GoCO
17:23:25.319 -> You're connected to the network
17:23:25.366 -> [9513] 
17:23:25.412 ->     ___  __          __
17:23:25.412 ->    / _ )/ /_ _____  / /__
17:23:25.412 ->   / _  / / // / _ \/  '_/
17:23:25.459 ->  /____/_/\_, /_//_/_/\_\
17:23:25.506 ->         /___/ v1.0.1 on Arduino Uno
17:23:25.506 -> 
17:23:25.976 -> [10169] Connecting to GoCO
17:23:29.197 -> [13352] AT version:1.6.2.0(Apr 13 2018 11:10:59)
17:23:29.197 -> SDK version:2.2.1(6ab97e9)
17:23:29.244 -> compile time:Sep 10 2019 17:31:08
17:23:29.244 -> OK
17:23:34.621 -> [18800] +CIFSR:STAIP,"192.168.1.201"
17:23:34.667 -> +CIFSR:STAMAC,"24:a1:60:3d:86:6b"
17:23:34.667 -> [18809] Connected to WiFi
17:23:45.393 -> [29575] Ready (ping: 43ms).
17:24:20.971 -> [65149] Ready (ping: 45ms).
17:24:56.547 -> [100723] Ready (ping: 46ms).

and my code changed to

#define BLYNK_TEMPLATE_ID "TMPL5vzGTnYc"
#define BLYNK_DEVICE_NAME "DHT22"
#define BLYNK_AUTH_TOKEN "";
#define BLYNK_PRINT Serial

//Libraries
#include <DHT.h>;
#include <ESP8266_Lib.h>
//#include <ESP8266WiFi.h>;
#include "WiFiEsp.h"
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

#define DHTPIN 10     // connect the dht output to 10(pwm) 
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define led 8   //connect to led to pin 8-->change to motor later
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

#include <BlynkSimpleShieldEsp8266.h>
//#include <SimpleTimer.h>
#define EspSerial Serial1
#define ESP8266_BAUD 9600

//Variables
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value

char ssid[] = "GoCO"; //You can replace the wifi name to your wifi
char pass[] = "";  //Type password of your wifi.
int status = WL_IDLE_STATUS;     // the Wifi radio's status

char auth[] = "";
ESP8266 wifi(&EspSerial);
SimpleTimer timer;

//int virtual_pin_value = param.asInt();
//param.asStrng()
//param.asFloat()
//BlynkTimer timer;

void delaytimer()
{
  int count = 0;
  Serial.print("delay");
  Serial.print(count);
  count++;
}
void setup()
{
  // initialize serial for debugging
  Serial.begin(9600);
  // initialize serial for ESP module
  Serial1.begin(9600);
  // initialize ESP module
  WiFi.init(&Serial1);
  pinMode(LED_BUILTIN, OUTPUT);
  dht.begin(); //start running the dht
  pinMode (led, OUTPUT); //set the led(pin8) as output
  pinMode (LED_BUILTIN, OUTPUT); //set the led on board as output
  WifiStatus();
}

void WifiStatus()
{

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    digitalWrite(LED_BUILTIN, LOW); //no on board led if no wifi
    Serial.println("WiFi is not connected");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    digitalWrite(LED_BUILTIN, HIGH);//connecting have on board led
    Serial.print("Connecting to ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  // you're connected now, so print out the data
  Serial.println("You're connected to the network");
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass);
  timer.setTimeout(3600000L, [] () {} ); // dummy/sacrificial Function
  timer.setInterval(5000L, sensorDataSend); // call the take_sensor_reading() function every 5 seconds
  sensorDataSend();
}

void sensorDataSend()
{
  //timer.setInterval(200L, delaytimer);

  digitalWrite(LED_BUILTIN, HIGH);
  timer.setTimeout(20, delaytimer);
  digitalWrite(LED_BUILTIN, LOW);
  timer.setTimeout(20, delaytimer);

  hum = dht.readHumidity();
  temp = dht.readTemperature();
  //Print temp and humidity values to serial monitor(help checking the data)
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print(" %, Temp: ");
  Serial.print(temp);
  Serial.println(" Celsius");

  if (temp >= 20) //running the motor with sensor
  {
    digitalWrite(led, HIGH); //switch on motor
  }
  else
  {
    digitalWrite(led, LOW); //switch off motor
  }
  //  sensorValue = analogRead(A5);         // reading sensor from analog pin
  //  Blynk.virtualWrite(V1, sensorValue);  // sending sensor value to Blynk app
}
BLYNK_WRITE(V1) // Executes when the value of virtual pin 1 changes
{
  if (param.asInt() == 1)
  {
    // execute this code if the switch widget is now ON
    sensorDataSend();
  }
  else
  {
    // execute this code if the switch widget is now OFF
    digitalWrite(led, LOW);
  }
}
void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer
}

Okay, now I’d recommend that you dump all of the WiFiEsp code and delete void WifiStatus() and put this in your void setup…

Also, you don’t need this…

because you’re not referencing the timers by their IDs.

and, as I said before, I’d dump all of the delaytimer rubbish.

Pete.

@PeteKnight
Am I doing it right?? For the wifiesp code, if I deleted them will it affect the running of esp8266?
The serial moniter become

18:14:59.529 -> [WiFiEsp] Initializing ESP module
18:15:03.233 -> [WiFiEsp] Initilization successful - 2.2.1
18:15:03.233 -> [3699] 
18:15:03.233 ->     ___  __          __
18:15:03.279 ->    / _ )/ /_ _____  / /__
18:15:03.326 ->   / _  / / // / _ \/  '_/
18:15:03.326 ->  /____/_/\_, /_//_/_/\_\
18:15:03.326 ->         /___/ v1.0.1 on Arduino Uno
18:15:03.373 -> 
18:15:03.842 -> [4336] Connecting to GoCO
18:15:07.016 -> [7520] AT version:1.6.2.0(Apr 13 2018 11:10:59)
18:15:07.063 -> SDK version:2.2.1(6ab97e9)
18:15:07.109 -> compile time:Sep 10 2019 17:31:08
18:15:07.109 -> OK
18:15:12.497 -> [12964] +CIFSR:STAIP,"192.168.1.201"
18:15:12.497 -> +CIFSR:STAMAC,"24:a1:60:3d:86:6b"
18:15:12.544 -> [12974] Connected to WiFi
18:15:23.081 -> [23568] Ready (ping: 45ms).
18:15:58.647 -> [59138] Ready (ping: 46ms).

and the code is

#define BLYNK_TEMPLATE_ID "TMPL5vzGTnYc"
#define BLYNK_DEVICE_NAME "DHT22"
#define BLYNK_AUTH_TOKEN "";
#define BLYNK_PRINT Serial

//Libraries
#include <DHT.h>;
#include <ESP8266_Lib.h>
//#include <ESP8266WiFi.h>;
#include "WiFiEsp.h"
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

#define DHTPIN 10     // connect the dht output to 10(pwm) 
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define led 8   //connect to led to pin 8-->change to motor later
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

#include <BlynkSimpleShieldEsp8266.h>
//#include <SimpleTimer.h>
#define EspSerial Serial1
#define ESP8266_BAUD 9600

//Variables
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value

char ssid[] = "GoCO"; //You can replace the wifi name to your wifi
char pass[] = "";  //Type password of your wifi.
int status = WL_IDLE_STATUS;     // the Wifi radio's status

char auth[] = "";
ESP8266 wifi(&EspSerial);
SimpleTimer timer;

//int virtual_pin_value = param.asInt();
//param.asStrng()
//param.asFloat()
//BlynkTimer timer;

void delaytimer()
{
  int count = 0;
  Serial.print("delay");
  Serial.print(count);
  count++;
}
void setup()
{
  // initialize serial for debugging
  Serial.begin(9600);
  // initialize serial for ESP module
  Serial1.begin(9600);
  // initialize ESP module
  WiFi.init(&Serial1);
  pinMode(LED_BUILTIN, OUTPUT);
  dht.begin(); //start running the dht
  pinMode (led, OUTPUT); //set the led(pin8) as output
  pinMode (LED_BUILTIN, OUTPUT); //set the led on board as output
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass);
  //timer.setTimeout(3600000L, [] () {} ); // dummy/sacrificial Function
  timer.setInterval(5000L, sensorDataSend); // call the take_sensor_reading() function every 5 seconds
  sensorDataSend();
  //WifiStatus();
  
}

/*void WifiStatus()
{

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    digitalWrite(LED_BUILTIN, LOW); //no on board led if no wifi
    Serial.println("WiFi is not connected");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    digitalWrite(LED_BUILTIN, HIGH);//connecting have on board led
    Serial.print("Connecting to ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  // you're connected now, so print out the data
  Serial.println("You're connected to the network");

}

*/
void sensorDataSend()
{
  //timer.setInterval(200L, delaytimer);

  digitalWrite(LED_BUILTIN, HIGH);
  timer.setTimeout(20, delaytimer);
  digitalWrite(LED_BUILTIN, LOW);
  timer.setTimeout(20, delaytimer);

  hum = dht.readHumidity();
  temp = dht.readTemperature();
  //Print temp and humidity values to serial monitor(help checking the data)
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print(" %, Temp: ");
  Serial.print(temp);
  Serial.println(" Celsius");

  if (temp >= 20) //running the motor with sensor
  {
    digitalWrite(led, HIGH); //switch on motor
  }
  else
  {
    digitalWrite(led, LOW); //switch off motor
  }
  //  sensorValue = analogRead(A5);         // reading sensor from analog pin
  //  Blynk.virtualWrite(V1, sensorValue);  // sending sensor value to Blynk app
}
BLYNK_WRITE(V1) // Executes when the value of virtual pin 1 changes
{
  if (param.asInt() == 1)
  {
    // execute this code if the switch widget is now ON
    sensorDataSend();
  }
  else
  {
    // execute this code if the switch widget is now OFF
    digitalWrite(led, LOW);
  }
}
void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer
}

What’s the purpose of the blinking builtin led besides some cool blinking? It’s on a device barely looked at and it will keep blinking (5 seconds not!) even if your sensorreading is not working ok…
I would skip that code also and focus on a deadband function for switching your motor on/off. If temp constantly variates between 19.9 and 20.1 your motor will keep switching on and off and will kill it.

@bazzio I think I have deleted that part for blinking the built in led. Also, the previous code is work until I connected to the Blynk server. The temperature is quite constant when I only run it with the motor.

Yes, it should make it work correctly.

This command….

emphasized text[quote=“Neptune, post:28, topic:56463”]
Blynk.begin(auth, wifi, ssid, pass);
[/quote]

will connect to the WiFi then to the Blynk server. Anything you do with setting-up a WiFi connection before that will get in the way.

It would make much more sense for you to start with the Blynk example sketch, and change the ESP baud rate to 9600….

Pete.

You are using simple timer and yet you have commented out the simple timer library?? Surely you would get an compile error?

@PeteKnight I am so sorry. could you specify which library I should delete? I am not sure about that. I am really sorry.