ESP8266 can't connect with the blynk IOT

Here is my full code:

#define BLYNK_TEMPLATE_ID "TMPL5SbCVWTab"
#define BLYNK_TEMPLATE_NAME "MSc Project"
#define BLYNK_AUTH_TOKEN "Z_db3VeO9M0qAExWVbtcDuab-8vxhVpV"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>

// WiFi Credentials
char auth[] = BLYNK_AUTH_TOKEN; 
char ssid[] = "Maidul's iPhone";  
char pass[] = "00000000"; 

// Define pin connections
#define TRIG_PIN 9
#define ECHO_PIN 10
#define MQ2_PIN A0
#define LM35_PIN 11
#define BUZZER1 6
#define BUZZER2 7
#define SERVO_PIN 5
#define RELAY1 2
#define RELAY2 3
#define RELAY3 4
#define RELAY4 8

Servo doorServo;
BlynkTimer timer;
bool doorOpen = false;

void setup() {
    Serial.begin(9600);
    Blynk.begin(auth, ssid, pass);
    
    pinMode(TRIG_PIN, OUTPUT);
    pinMode(ECHO_PIN, INPUT);
    pinMode(MQ2_PIN, INPUT);
    pinMode(LM35_PIN, INPUT);
    pinMode(BUZZER1, OUTPUT);
    pinMode(BUZZER2, OUTPUT);
    pinMode(RELAY1, OUTPUT);
    pinMode(RELAY2, OUTPUT);
    pinMode(RELAY3, OUTPUT);
    pinMode(RELAY4, OUTPUT);
    
    doorServo.attach(SERVO_PIN);
    doorServo.write(0);  // Initial position
    
    timer.setInterval(1000L, checkSensors);  // Run every 1 sec
}

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

void checkSensors() {
    checkUltrasonic();
    checkGasSensor();
    checkTemperature();
}

void checkUltrasonic() {
    digitalWrite(TRIG_PIN, LOW);
    delayMicroseconds(2);
    digitalWrite(TRIG_PIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIG_PIN, LOW);
    
    long duration = pulseIn(ECHO_PIN, HIGH, 30000);
    if (duration == 0) return;
    
    int distance = duration * 0.034 / 2;
    Blynk.virtualWrite(V1, distance);
    
    if (distance < 10 && !doorOpen) {
         doorOpen = true;
        doorServo.write(90);
    
        delay(1000);
        doorServo.write(0);
         doorOpen = false;
    }
}

void checkGasSensor() {
    int gasValue = analogRead(MQ2_PIN);
    Blynk.virtualWrite(V4, gasValue);
    
    if (gasValue > 300) {
        digitalWrite(BUZZER1, HIGH);
       
    } else {
        digitalWrite(BUZZER1, LOW);
    }
}

void checkTemperature() {
    int tempValue = analogRead(LM35_PIN);
    float temperature = (tempValue * 5.0 * 100.0) / 1024.0;
    Blynk.virtualWrite(V3, temperature);
    
    static bool relayState = false;
    if (temperature > 30 && !relayState) {
        digitalWrite(RELAY1, HIGH);
        
        relayState = true;
    } else if (temperature < 28 && relayState) {
        digitalWrite(RELAY1, LOW);
        relayState = false;
    }
}

//Controls Relays
BLYNK_WRITE(V5) { digitalWrite(RELAY2, param.asInt()); }
BLYNK_WRITE(V6) { digitalWrite(RELAY3, param.asInt()); }
BLYNK_WRITE(V7) { digitalWrite(RELAY4, param.asInt()); }

The problem is this code is not working properly. Please fix the issue. The device ESP8266 can’t connect with the blynk IOT. The device is showing offline. The serial monitor is showing nothing.

@maidulmomin75 Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

I am new here. But I want to learn more. Thanks for your instruction.
I followed you and updated the post.

You’re trying to do far too much with the ESP8266 board that you’re using, as it doesn’t have enough useable pins for your needs.
You’re also using some special flash pins (GPIO6-11), some strapping pins, and some pins that are normally used for serial communication.
Read this for more info…

You should consider using an ESP32 board, and if you do then take care to choose an analog pin that is connected to ADC1.

Pete.

Thank you Pete for your majesty.

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL5SbCVWTab"
#define BLYNK_TEMPLATE_NAME "MSc Project"
#define BLYNK_AUTH_TOKEN "Z_db3VeO9M0qAExWVbtcDuab-8vxhVpV"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char ssid[] = "Maidul's iPhone";      // WiFi name
char pass[] = "00000000";             // WiFi password

String inputData = "";

void setup() {
  Serial.begin(9600);  // Communicate with Arduino
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);  // Connect to Blynk
}

void loop() {
  Blynk.run();

  while (Serial.available()) {
    char c = Serial.read();
    if (c == '\n') {
      parseData(inputData);  // Send to Blynk
      inputData = "";
    } else {
      inputData += c;
    }
  }
}

void parseData(String data) {
  // Expected format: "D:12,T:27,G:320"
  int d = data.indexOf("D:");
  int t = data.indexOf("T:");
  int g = data.indexOf("G:");

  int dist = data.substring(d + 2, t - 1).toInt();
  int temp = data.substring(t + 2, g - 1).toInt();
  int gas  = data.substring(g + 2).toInt();

  Blynk.virtualWrite(V1, dist);
  Blynk.virtualWrite(V3, temp);
  Blynk.virtualWrite(V4, gas);

  Serial.print("Parsed — Distance: "); Serial.print(dist);
  Serial.print(" Temp: "); Serial.print(temp);
  Serial.print(" Gas: "); Serial.println(gas);
}

I used this code to established the connection with Blynk IoT. But The device shows offline. I can’t connect the esp8266 with Blynk.

I’d try a simple example sketch first, and take note of what you see in your serial monitor.

Pete.

Nothing Shows in the serial monitor

It won’t with either of the sketches you’ve posted, because the first one uses the same pins as the UART and the second one is using the UART for incoming data from some sort of peripheral.

You also need to ensure that your serial monitor baud rate matches your serial.begin() command.

Pete.


I used 9600baud rate.

Yes, but your sketch is using the UART for something else other than serial monitor debugging. That’s why you need to try one of the simple standard sketches that are added to your Examples when you install the Blynk library.

Pete.

May you recommend which sketches can I use to solve this issue?

Look at the examples, choose one.

Pete.