Seems cant connect to Blynk server

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.

@proietti Not sure what do you mean for this

Have you looked at the example sketch I linked to?

Pete.

@PeteKnight yup, when I try to compile the code. this line got error

 EspSerial.begin(ESP8266_BAUD);

it said

Arduino: 1.8.15 (Windows 10), Board: "Arduino Uno"

dht22_test:23:15: error: 'EspSerial' was not declared in this scope

 ESP8266 wifi(&EspSerial);

               ^~~~~~~~~

C:\Users\user\Desktop\dht22_test\dht22_test.ino:23:15: note: suggested alternative: 'Serial'

 ESP8266 wifi(&EspSerial);

               ^~~~~~~~~

               Serial

C:\Users\user\Desktop\dht22_test\dht22_test.ino: In function 'void setup()':

dht22_test:74:3: error: 'EspSerial' was not declared in this scope

   EspSerial.begin(ESP8266_BAUD);

   ^~~~~~~~~

C:\Users\user\Desktop\dht22_test\dht22_test.ino:74:3: note: suggested alternative: 'Serial'

   EspSerial.begin(ESP8266_BAUD);

   ^~~~~~~~~

   Serial

exit status 1

'EspSerial' was not declared in this scope



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

@PeteKnight I have redesigned the code now, is this better?

// generated by mBlock5 for <your product>
// codes make you happy

//#include "src/OfflineBroadcast.h"
//#include "DHT_sensor_library-1.4.1/DHT.h"
//#include "Adafruit_Sensor-master/Adafruit_Sensor.h"
#define BLYNK_TEMPLATE_ID "TMPL5vzGTnYc"
#define BLYNK_DEVICE_NAME "DHT22"
#define BLYNK_AUTH_TOKEN "nfxC29MO9ja1n6hzopoqMg2EA56Znw6N";

#define BLYNK_PRINT Serial

#include <DHT.h>

#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Arduino.h>
DHT dht(10, DHT22);

float WIFI_ON_OFF = 0;
float Fan_On_Off = 0;

#define ESP8266_BAUD 9600
SoftwareSerial EspSerial(6, 7);
ESP8266 wifi(&EspSerial);

char ssid[] = "GoCO";
char pass[] = "29882992";
char auth[] = " nfxC29MO9ja1n6hzopoqMg2EA56Znw6N";

//OfflineBroadcast broadcaster;
void Fan_on() {
  digitalWrite(8, 1);

  //broadcaster.callOK();
}
void Fan_off() {
  digitalWrite(8, 0);

  //broadcaster.callOK();
}
void No_wifi() {
  while (1) {
    if ((dht.readTemperature() > 20)  ||  (dht.readTemperature() == 20.000000)) {
      Fan_on();

    } else {
      Fan_off();

    }

    //      _loop();
  }

  //broadcaster.callOK();
}
BLYNK_WRITE(V6) {
  Fan_On_Off = param.asDouble();
}

void Wifi_On() {
  while (1) {
    if (Fan_On_Off == 1.000000) {
      Fan_on();

    } else {
      Fan_off();

    }

    // _loop();
  }

  //broadcaster.callOK();
}

void _delay(float seconds) {
  long endTime = millis() + seconds * 1000;
  while (millis() < endTime) _loop();
}

void setup() {
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  //delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
  Serial.begin(9600);
  pinMode(8, OUTPUT);
  dht.begin();
  WIFI_ON_OFF = 0;
  Fan_On_Off = 0;
  WIFI_ON_OFF = 1;
  while (1) {
    if (WIFI_ON_OFF == 1.000000) {
      Wifi_On();

    } else {
      No_wifi();

    }

    //_loop();
  }
  /*
    broadcaster.on(String("On"),broadcastHandler);

    broadcaster.on(String("Off"),broadcastHandler1);

    broadcaster.on(String("Wifi Off"),broadcastHandler2);

    broadcaster.on(String("Wifi On"),broadcastHandler3);
  */
}

void _loop() {
  //broadcaster.loop();
}

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

However it show this time

12:38:08.695 ->     ___  __          __
12:38:08.695 ->    / _ )/ /_ _____  / /__
12:38:08.740 ->   / _  / / // / _ \/  '_/
12:38:08.786 ->  /____/_/\_, /_//_/_/\_\
12:38:08.786 ->         /___/ v1.0.1 on Arduino Uno
12:38:08.831 -> 
12:38:09.297 -> [587] Connecting to GoCO
12:38:10.322 -> [1602] ESP is not responding

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();
}

I’m assuming that you didn’t comment-out the Mega section an un-comment the Uno section.

Pete.

He did but he’s not using the right pins, he should use (2, 3) instead of (6, 7)

1 Like