Blynk, ESP - 01S and Arduino

I have a problem with the connection of blynk with the arduino uno and connection with esp-01 S Series, It keeps on connecting and disconnecting the blynk. Im using 1.2.0 Blynk Library 9600 baud is working for me I already reconfigure my ESP - 01S
here’s the code

#define BLYNK_TEMPLATE_ID "****"
#define BLYNK_TEMPLATE_NAME "****"
#define BLYNK_AUTH_TOKEN "****"

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

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "WifiKo";
char pass[] = "******";
LiquidCrystal_I2C lcd(0x27, 16, 2);

SoftwareSerial EspSerial(2, 3); // RX, TX
#define MQ3_PIN A0       // MQ-3 Alcohol Sensor pin
#define BUTTON_PIN 12    // Push button connected to pin 12
#define LED_PIN 13       // Motor LED connected to pin 13
#define BRAKE_LIGHT_PIN 8 // Brake light LED connected to pin 8
#define BUZZER_PIN 9     // Buzzer connected to pin 9
#define ALCOHOL_THRESHOLD 300
#define ESP8266_BAUD 9600

bool ledState = false;  // Keeps track of the LED state (on/off)
bool lastButtonState = HIGH; // Previous state of the button
bool buttonPressed = false; // Flag to detect button press

ESP8266 wifi(&EspSerial);
BlynkTimer timer;

// Function to read alcohol level and send it to Blynk
void readAlcoholSensor() {
  int alcoholValue = analogRead(MQ3_PIN);

  // Print alcohol level to Serial Monitor
  Serial.print("Alcohol Level: ");
  Serial.println(alcoholValue);

  // Display alcohol level on the LCD
  lcd.setCursor(0, 0); // Set cursor to the first row
  lcd.print("ALCOHOL: ");
  lcd.print(alcoholValue);
  lcd.print("  ");

  // Send alcohol level to Blynk
  Blynk.virtualWrite(V1, alcoholValue);

  // Check alcohol level
  if (alcoholValue > ALCOHOL_THRESHOLD) {
    digitalWrite(LED_PIN, LOW);  // Turn off the Motor LED (simulate motor OFF)
    digitalWrite(BRAKE_LIGHT_PIN, HIGH); // Turn on the Brake Light LED
    digitalWrite(BUZZER_PIN, HIGH); // Turn on the Buzzer
    lcd.setCursor(0, 1);
    lcd.print("ALC HIGH! STOP");
    Serial.println("Motor Stopped: Alcohol Detected");
    Blynk.logEvent("alcohol_high", "High alcohol level detected!");
    delay(2000); // Pause for 2 seconds to display the message
    digitalWrite(BUZZER_PIN, LOW); // Turn off the Buzzer
  }
}

void setup() {
  // Debug console
  Serial.begin(9600);
  lcd.begin();  // Initialize the LCD
  lcd.print("Alcohol Sensor");
  delay(2000); // Display for 2 seconds
  lcd.clear();

  // Initialize button, LED, and buzzer pins
  pinMode(BUTTON_PIN, INPUT_PULLUP); // Use internal pull-up resistor
  pinMode(LED_PIN, OUTPUT);          // Set Motor LED pin as output
  pinMode(BRAKE_LIGHT_PIN, OUTPUT);  // Set Brake Light LED pin as output
  pinMode(BUZZER_PIN, OUTPUT);       // Set Buzzer pin as output

  lcd.print("Ready");
  delay(1000);
  lcd.clear();

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

  // Connect to Wi-Fi and Blynk
  Blynk.begin(auth, wifi, ssid, pass, "blynk.cloud", 80);

  // Setup a timer to read the alcohol sensor every 1 second
  timer.setInterval(1000L, readAlcoholSensor);
}

void loop() {
  Blynk.run();    // Run Blynk
  timer.run();    // Run the timer
}

@CodEmbi 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 edited now pete sorry I was new to this :smile:

How are you powering your ESP-01 ?

When you compile your sketch, what percentage of the available memory does the compiler say has been used?

What does your serial monitor show when the device connects and then disconnects? (Post the serial monitor text, not a screenshot, and don’t forget the triple backticks).

Have you tried a simpler sketch, without all of the additional hardware and its associated code?

Pete.

Im using the 3.3v of arduino to power up the ESP-01.

Sketch Compile: Sketch uses 21042 bytes (65%) of program storage space. Maximum is 32256 bytes.
Global variables use 1243 bytes (60%) of dynamic memory, leaving 805 bytes for local variables. Maximum is 2048 bytes.

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

 #StandWithUkraine    https://bit.ly/swua


[4759] Connecting to WifiKo
[7946] AT version:1.7.4.0(Jul  8 2020 15:53:04)
SDK version:3.0.5-dev(52383f9)
compile time:Aug 28 2020 14:37:33
OK
[13341] +CIFSR:STAIP,"192.168.17.235"
+CIFSR:STAMAC,"80:7d:3a:13:3c:65"
[13351] Connected to WiFi
[30584] Login timeout
[53218] Login timeout
[78655] Login timeout
[102004] Login timeout


I have a previous code working but the moment I added the blynk.logEvent everything change the blynk going Online and Offline I thought the baud rate of ESP-01s was reset again then I tried to communicate with AT commands but still it was running on 9600 but the default baud rate after I bought this ESP-01s was 115200

No Blynk connection is being made, according to your serial output.

Pete.

So what will be the best fix for that pete the blynk on my mobile was keep connecting and disconnecting

What exactly does that mean?
Are you using your phone as a hotspot for the device?

It looks like your device can’t see the internet, despite being connected to WiFi.
If you’re using a home internet setup then rebooting your router would be the first thing to try.

Pete.

yah Im using mobile hotspot, I have a update regarding to my problem this code is working but when I add blynk.logEvent the same problem will appear again the On and Off like that. I downgraded my Blynk library to 0.6

#define BLYNK_TEMPLATE_ID "TMPL6_dbV9R37"
#define BLYNK_TEMPLATE_NAME "VA"
#define BLYNK_AUTH_TOKEN "3wCHyfiL675hNB9VY2dnLOozXOZlJBgD"

// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
  
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <LiquidCrystal_I2C.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "WifiKo";
char pass[] = "87654321";
LiquidCrystal_I2C lcd(0x27, 16, 2); 

#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX
#define MQ3_PIN A0       // MQ-3 Alcohol Sensor pin
#define BUTTON_PIN 12    // Push button connected to pin 12
#define LED_PIN 13       // Motor LED connected to pin 13
#define BRAKE_LIGHT_PIN 8 // Brake light LED connected to pin 8
#define BUZZER_PIN 9     // Buzzer connected to pin 9
#define ALCOHOL_THRESHOLD 300
#define ESP8266_BAUD 9600


bool ledState = false;  // Keeps track of the LED state (on/off)
bool lastButtonState = HIGH; // Previous state of the button
bool buttonPressed = false; // Flag to detect button press

ESP8266 wifi(&EspSerial);

void setup() {
  // Debug console
  Serial.begin(9600);
  lcd.begin();  // No need to specify the size when using LiquidCrystal_I2C
  lcd.print("Alcohol Sensor");
  delay(2000); // Display for 2 seconds
  lcd.clear();


  // Initialize button, LED, and buzzer pins
  pinMode(BUTTON_PIN, INPUT_PULLUP); // Use internal pull-up resistor
  pinMode(LED_PIN, OUTPUT);          // Set Motor LED pin as output
  pinMode(BRAKE_LIGHT_PIN, OUTPUT);  // Set Brake Light LED pin as output
  pinMode(BUZZER_PIN, OUTPUT);       // Set Buzzer pin as output

  lcd.print("Ready");
  delay(1000);
  lcd.clear();

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

  // Connect to Wi-Fi and Blynk
  Blynk.begin(auth, wifi, ssid, pass, "blynk.cloud", 80);
}
void loop() {
  // Read alcohol level from the MQ-3 sensor
  int alcoholValue = analogRead(MQ3_PIN);

  // Print alcohol level to Serial Monitor
  Serial.print("Alcohol Level: ");
  Serial.println(alcoholValue);

  // Display alcohol level on the LCD
  lcd.setCursor(0, 0); // Set cursor to the first row
  lcd.print("ALCOHOL\:");
  lcd.print(alcoholValue);
  lcd.print("  ");

  Blynk.virtualWrite(V1, alcoholValue);

  // Check alcohol level
  if (alcoholValue > ALCOHOL_THRESHOLD) {
     
    // If alcohol is detected above the threshold
    digitalWrite(LED_PIN, LOW);  // Turn off the Motor LED (simulate motor OFF)
    digitalWrite(BRAKE_LIGHT_PIN, HIGH); // Turn on the Brake Light LED
    digitalWrite(BUZZER_PIN, HIGH); // Turn on the Buzzer
    lcd.setCursor(0, 1);
    lcd.print("ALC HIGH! STOP");
    Serial.println("Motor Stopped: Alcohol Detected");
    delay(2000); // Pause for 2 seconds to display the message
    digitalWrite(BUZZER_PIN, LOW); // Turn off the Buzzer
  } else {
    // Alcohol level below threshold, check button state
    if (digitalRead(BUTTON_PIN) == LOW && lastButtonState == HIGH) {
      // Detect button press (change from HIGH to LOW)
      delay(50);  // Debounce delay
      buttonPressed = true;
    }

    if (buttonPressed) {
      // Toggle the LED state when button is pressed
      ledState = !ledState;
      digitalWrite(LED_PIN, ledState ? HIGH : LOW);  // Update LED based on state
      if (ledState) {
        digitalWrite(BRAKE_LIGHT_PIN, LOW); // Turn off the Brake Light LED
        lcd.setCursor(0, 1);
        lcd.print("Motor: ON      ");
        Serial.println("Motor Running");
        Blynk.virtualWrite(V2, 255); // Send motor status to Virtual Pin 2
      Blynk.virtualWrite(V3, 0);   // Send motor status to Virtual Pin 3
      } else {
        digitalWrite(BRAKE_LIGHT_PIN, HIGH); // Turn on the Brake Light LED
        lcd.setCursor(0, 1);
        lcd.print("Motor: OFF     ");
        Serial.println("Motor Stopped");
         Blynk.virtualWrite(V2, 0);    // Send motor status to Virtual Pin 2
      Blynk.virtualWrite(V3, 255);  // Send motor status to Virtual Pin 3
      }
      buttonPressed = false; // Reset button press state
    }

    lastButtonState = digitalRead(BUTTON_PIN);  // Update the button state
  }

  delay(500); // Update every 500ms
}

You need to be running the latest version which is 1.3.2
Library version 0.6x is not compatible with Blynk IoT.

Your latest code is a massive step backwards. It has a cluttered void loop and delays, neither of which are compatible with Blynk.

I suspect that your issue is that you’re using your phone hotspot, as your earlier serial output indicated that there was no internet connection available for the device.

Thee are certainly issues with your Blynk.logEvent code logic, as it could easily result in you reaching your 24 hour limit of 100 events, but it won’t cause your device to fail to connect.

Pete.