Newbie Question - How can my code be connected to Blynk using a ESP8226

Briefcase on my issue faced:

Below are my project code drafted by me, the the system is to basically detect seismic vibration and the temperature and push notifications using the Blynk IOT app. Before implementing and adding Blynk Wifi connection code, the code was running fine and being projected in the Serial Monitor. After adding the Wifi codes, the serial monitor is being projected with the Wifi Ping and connection status only and the circuit was not working at all. Can i know anyway to solve this issue?

Original Project Code:

#define RED_LED_PIN 5
#define GREEN_LED_PIN 6
#define BUZZER_PIN 4

#include "DHT.h"
#define DHT22_PIN 3
DHT dht22(DHT22_PIN, DHT22);

int Vibration_signal = 7; //Define the Digital Input on the Arduino for the sensor signal
int Sensor_State = 1;

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

void setup() {
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(Vibration_signal, INPUT); //Set pin as input
  Serial.begin(9600); // Start the serial communication
  dht22.begin();
}
 
void loop() {
  // read humidity
  float humi  = dht22.readHumidity();
  // read temperature as Celsius
  float tempC = dht22.readTemperature();
  // read temperature as Fahrenheit
  float tempF = dht22.readTemperature(true);
  
  Serial.println("===========================================================");
  Serial.print("Earthquake status : ");
  Sensor_State = digitalRead(Vibration_signal);
  if (Sensor_State == 1) {
    Serial.println("Seismic Activity Detected!");
    digitalWrite(RED_LED_PIN, HIGH);
    digitalWrite(GREEN_LED_PIN, LOW);
    tone(BUZZER_PIN, 1500);
    delay(1000);
  }
  else {
    Serial.println("No Seismic Activity");
    digitalWrite(GREEN_LED_PIN, HIGH);
    digitalWrite(RED_LED_PIN, LOW);
    noTone(BUZZER_PIN);
    delay(500);
  }
  delay(50);

  Serial.print("Environment status: ");
  if (tempC >= 35 && tempC < 40) {
    Serial.println("High Temperature Warning!");
    digitalWrite(RED_LED_PIN, HIGH);
    digitalWrite(GREEN_LED_PIN, LOW);
    tone(BUZZER_PIN, 1500);
    delay(1000);
  }
  else if (tempC >= 40 ) {
    Serial.println("Heat Wave Alert!");
    digitalWrite(RED_LED_PIN, HIGH);
    digitalWrite(GREEN_LED_PIN, LOW);
    tone(BUZZER_PIN, 1500);
    delay(1000);
  }
  else {
    Serial.println("Normal");
    digitalWrite(GREEN_LED_PIN, HIGH);
    digitalWrite(RED_LED_PIN, LOW);
    noTone(BUZZER_PIN);
    delay(500);
  }
  delay(50);

  if (isnan(humi) || isnan(tempC) || isnan(tempF)) {
    Serial.println("Failed to read from DHT22 sensor!");
  } else {
    Serial.print("Humidity: ");
    Serial.print(humi);
    Serial.print("%");

    Serial.print("  |  "); 

    Serial.print("Temperature: ");
    Serial.print(tempC);
    Serial.print("°C ~ ");
    Serial.print(tempF);
    Serial.println("°F");
  }
  delay(50);
}

Project Code after adding Blynk WiFi Config:

#define RED_LED_PIN 5
#define GREEN_LED_PIN 6
#define BUZZER_PIN 4

#define BLYNK_TEMPLATE_ID "-"
#define BLYNK_TEMPLATE_NAME "-"
#define BLYNK_AUTH_TOKEN "-"

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

#include "DHT.h"
#define DHT22_PIN 3
DHT dht22(DHT22_PIN, DHT22);

int Vibration_signal = 7; //Define the Digital Input on the Arduino for the sensor signal
int Sensor_State = 1;

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

char auth[] = "-";
char ssid[] = "-";
char pass[] = "-";
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

void setup() {
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(Vibration_signal, INPUT); //Set pin as input
  Serial.begin(9600); // Start the serial communication
  dht22.begin();
  delay(10);
}
 
void loop() {
  Blynk.run();
  // read humidity
  float humi  = dht22.readHumidity();
  // read temperature as Celsius
  float tempC = dht22.readTemperature();
  // read temperature as Fahrenheit
  float tempF = dht22.readTemperature(true);
  
  Serial.println("===========================================================");
  Serial.print("Earthquake status : ");
  Sensor_State = digitalRead(Vibration_signal);
  if (Sensor_State == 1) {
    Serial.println("Seismic Activity Detected!");
    digitalWrite(RED_LED_PIN, HIGH);
    digitalWrite(GREEN_LED_PIN, LOW);
    tone(BUZZER_PIN, 1500);
    Blynk.logEvent("earthquake_alert");
    delay(1000);
  }
  else {
    Serial.println("No Seismic Activity");
    digitalWrite(GREEN_LED_PIN, HIGH);
    digitalWrite(RED_LED_PIN, LOW);
    noTone(BUZZER_PIN);
    delay(500);
  }
  delay(50);

  Serial.print("Environment status: ");
  if (tempC >= 35 && tempC < 40) {
    Serial.println("High Temperature Warning!");
    digitalWrite(RED_LED_PIN, HIGH);
    digitalWrite(GREEN_LED_PIN, LOW);
    tone(BUZZER_PIN, 1500);
    Blynk.logEvent("high_temperature_warning");
    delay(1000);
  }
  else if (tempC >= 40 ) {
    Serial.println("Heat Wave Alert!");
    digitalWrite(RED_LED_PIN, HIGH);
    digitalWrite(GREEN_LED_PIN, LOW);
    tone(BUZZER_PIN, 1500);
    Blynk.logEvent("heat_wave_alert");
    delay(1000);
  }
  else {
    Serial.println("Normal");
    digitalWrite(GREEN_LED_PIN, HIGH);
    digitalWrite(RED_LED_PIN, LOW);
    noTone(BUZZER_PIN);
    delay(500);
  }
  delay(50);

  // check if any reads failed
  if (isnan(humi) || isnan(tempC) || isnan(tempF)) {
    Serial.println("Failed to read from DHT22 sensor!");
  } else {
    Serial.print("Humidity: ");
    Serial.print(humi);
    Serial.print("%");

    Serial.print("  |  "); 

    Serial.print("Temperature: ");
    Serial.print(tempC);
    Serial.print("°C ~ ");
    Serial.print(tempF);
    Serial.println("°F");
  }
  delay(50);
}

What hardware are you using and how is it wired and powered?

What exactly does your serial monitor show? (copy/paste the text and post with triple backticks tge same way you did with your code).

Why do you have two serial.begin statements in your void setup?

You also need to read this…

Pete.

I am running with Adruino UNO with a ESP8226 Wifi Shield, DHT22, Vibration sensor, LED and Buzzer and powered direct from the laptop. The circuit has no issue, just when I added in the code for Blynk WiFi Connection, then i cant obtain the output i want.

Below is the serial monitor output from the original project code:

===========================================================
Earthquake status : No Seismic Activity
Environment status: Normal
Humidity: 83.80%  |  Temperature: 31.50°C ~ 88.70°F

Below is the output get after adding the Blynk config into the code:

[9] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.3.2 on Arduino Uno

 #StandWithUkraine    https://bit.ly/swua


[647] Connecting to HomeWiFi
[13724] AT version:2.2.0.0(b097cdf - ESP8266 - Jun 17 2021 12:57:45)
SDK version:v3.4-22-g967752e2
compile time(6800286):Aug  4 2021 17:34:06
Bin version:2.2.0(Cytron_ESP-12F_WROM0)

O

[14883] +CIFSR:STAIP,"---"
+CIFSR:STAMAC,"---"
[14894] Connected to WiFi
[25722] Ready (ping: 64ms).
[57095] Ready (ping: 103ms).
[88175] Ready (ping: 53ms).
[119345] Ready (ping: 100ms).
[156376] Login timeout
[237309] Ready (ping: 103ms).

After the code was added, the original output has been gone and cant be able to project it together with the code in the serial output. Which the original output code cant be projected together in the serial montor.

Looks like a power supply issue to me.
When the ESP8266 starts to transmit WiFi signals it draws more power, and that could be what is causing the Blynk connection to drop and the server to time it out.

Pete.

The time out looks to be an connection issue, but it was solved out right after the wiring were reconnected. And for it seems to be connected to the Blynk as well. But the issue is my original output for the temperature, humidity and vibration sensor was not displaying on the serial monitor.

Blynk has a heartbeat timeout period. If the device and server don’t talk successfully for a period of time (usually around 45 seconds).
A normal connection will have one ping message. Because you’re seeing multiple pings it means that the connection is being dropped then re-established, until it eventually times-out. As I said earlier, this looks like insufficient power to run the board, its peripherals and the WiFi transmission circuit at the same time.
Having blocking delays in your code will also contribute to these timeout issues, as communication between the device and Blynk is impossible during the delay period.

What are you seeing that supports this belief?

That’s probably because the device isn’t actually connecting to the Blynk server and therefore not progressing beyond the Blynk.begin() command, but it’s difficult to say because you haven’t provided sufficient information.

Pete.

Because the Blynk Developer console was connected to my device and was labeling online. And my hotspot device was displaying that the ESP 8226 is connected.

Everything work well from the wifi connection side, but the main issue is the original functional code was not running. Hmmmm…

Your serial monitor is the most reliable source of information. What does it show regarding your connection now that you’ve disconnected then reconnected everything?

Pete.

Wait ya. Let me rerun the program again ya. and below is the result:

[9] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.3.2 on Arduino Uno

 #StandWithUkraine    https://bit.ly/swua


[647] Connecting to HomeWiFi
[13724] AT version:2.2.0.0(b097cdf - ESP8266 - Jun 17 2021 12:57:45)
SDK version:v3.4-22-g967752e2
compile time(6800286):Aug  4 2021 17:34:06
Bin version:2.2.0(Cytron_ESP-12F_WROM0)

O

[14883] +CIFSR:STAIP,"---"
+CIFSR:STAMAC,"---"
[14894] Connected to WiFi
[57095] Ready (ping: 103ms).
[119345] Ready (ping: 100ms).
[237309] Ready (ping: 103ms).

I seems fine from the serial monitor for now. i dont think there is any issue on the code, just that the combination within this code with the original code didnt work well. The Blynk Iot Cloud is detecting the ESP8226, but the circuit just didnt run.

No it doesnt!.

As I said, you should see one ping message only. This is a problem…

I’ll say it again, I think this is a power supply issue.

Pete.

But the power supply i am using a port from the laptop. Any suggestion from your side? Cuz the ESP 8226 wifi shield i am using is an addition top on board on the adruino.

The link of the wifi shield:

Try powering the Uno via the barrel connector. It accepts 9-20v DC

Pete.

Hi Pete,

I have changed the power supply. and below is my output. But how do i link my vibration sensor, temperature and humidity output code to this?

 ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.3.2 on Arduino Uno

 #StandWithUkraine    https://bit.ly/swua


[659] Connecting to Kah Seng
[13749] AT version:2.2.0.0(b097cdf - ESP8266 - Jun 17 2021 12:57:45)
SDK version:v3.4-22-g967752e2
compile time(6800286):Aug  4 2021 17:34:06
Bin version:2.2.0(Cytron_ESP-12F_WROM0)
O

[15601] +CIFSR:STAIP,"---.---.----.----"
+CIFSR:STAMAC,"c8:c9:a3:43:2f:d1"
[15613] Connected to WiFi
[26284] Ready (ping: 94ms).

Are you still running your original Blynk code?
If so then read this…

If not then post your code.

Pete.