Connecting to blynk.cloud:80 -esp8266

Hi everyone, i need some help, i’m doing my ioT project, which is a gas leak detector that uses the ESP8266 microcontroller(nodeMCU) and the Blynk IoT platform to notify the user of potential gas leaks. However, i cannot fix this error. I really need your help. Thank you!

I chose to base my final IoT project on this gas leakage monitoring system from (https://srituhobby.com/iot-based-gas-leakage-monitoring-system/)

I used Blynk latest version (1.2.0)

This is my code:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "xxxxxxx";// Enter your Auth token
char ssid[] = "xxxxxxx";//Enter your WIFI SSIS
char pass[] = "xxxxxx";//Enter your WIFI password
BlynkTimer timer;
int pinValue = 0;

#define Buzzer D5
#define Yellow D6
#define Green D7
#define Sensor A0

void setup() {
  Serial.begin(9600);
  pinMode(Yellow, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(Buzzer, OUTPUT);
  pinMode(Sensor, INPUT);
  Blynk.begin(auth, ssid, pass);

  timer.setInterval(100L, notifiaction);
}
BLYNK_WRITE(V0) {
  pinValue = param.asInt();
}

void notifiaction() {
  int sensor = analogRead(Sensor);
  Serial.println(sensor);
  sensor = map(sensor, 0, 1024, 0, 100);
  if (pinValue == 1) {
    if (sensor <= 50) {
      digitalWrite(Yellow, HIGH);
      digitalWrite(Green, LOW);
      digitalWrite(Buzzer, LOW);

     } else if (sensor > 50) {
       Blynk.logEvent("Warning! Gas leak detected");
      digitalWrite(Yellow, LOW);
      digitalWrite(Green, HIGH);
      digitalWrite(Buzzer, HIGH);


    }
     Blynk.virtualWrite(V1, sensor);

   } else {
     digitalWrite(Yellow, LOW);
    digitalWrite(Buzzer, LOW);
    digitalWrite(Green, LOW);
  }
}

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

I was able to connect to the WiFi, but I’m not able to connect to the Blynk cloud.

     [8027] Connecting to blynk.cloud:80
     [8357] Ready (ping: 84ms).
     2
     3
     3
     3
     5
     6
     3
     2
     5

You appear to be misunderstanding the serial monitor message.
This…

tells you that the device is successfully connected to the Blynk server and that the ping time from the device to the Blynk server is 84ms.

Does the device show as Online in the web console?

You appear to be missing the Template ID and Template Name lines of code from your sketch.

You also have a logic issue with your log event code, as you will quickly exceed the 100 events per 24 hour period limit. You should read this for more info…

Pete.

The device appears to be online in the web console, but the status device for blynk ioT (android device) is offline.

I changed the coding by adding a template ID and a template name, as well as fixing the log event code.

Current code:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "xxxxxxxxxxx";// Enter your Auth token
char ssid[] = "xxxxxxxxxxx";//Enter your WIFI SSID
char pass[] = "xxxxxxxxxxx";//Enter your WIFI password
char template_id[] = "xxxxxxxxxxx"; // Enter your Template ID
char template_name[] = "xxxxxxxxxxx"; // Enter your Template Name

BlynkTimer timer;
int pinValue = 0;

#define Buzzer D5
#define Yellow D6
#define Green D7
#define Sensor A0

void setup() {

  Serial.begin(9600);
     pinMode(Yellow, OUTPUT); 
     pinMode(Green, OUTPUT);
     pinMode(Buzzer, OUTPUT);
     pinMode(Sensor, INPUT);
     Blynk.begin(auth, ssid, pass);

  timer.setInterval(10000L, notifiaction);
  }

 BLYNK_WRITE(V0) {
   pinValue = param.asInt();
 }

   void notifiaction() {
      int sensor = analogRead(Sensor);
      Serial.println(sensor); 
      sensor = map(sensor, 0, 1024, 0, 100);

      if (pinValue == 1) {
          if (sensor <= 50) {
              digitalWrite(Yellow, HIGH);
              digitalWrite(Green, LOW);
              digitalWrite(Buzzer, LOW);
      } else if (sensor > 50) {
     
      Blynk.logEvent("gas_leak_detected)",  String("Gas concentration level is ") + reading); // trigger the notification      
      alert_sent = true; // set the flag to indicate that an alert has been sent for this over-temp situation
      digitalWrite(Yellow, LOW);
      digitalWrite(Green, HIGH);
      digitalWrite(Buzzer, HIGH);
      }

      Blynk.virtualWrite(V1, sensor);

      } else {
      alert_sent = false; // reset (clear) the flag so that a future over-temp situation will trigger an alert
      digitalWrite(Yellow, LOW);
      digitalWrite(Buzzer, LOW);
      digitalWrite(Green, LOW);
      }

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

I suspect that you’ve somehow managed to create a device in the app that is different from the device in the web console, or that you have created two different Blynk accounts and are logged-in to a different one in the app.
You’d need to provide significantly more information and probably screenshots of both the web console and the app if you wnat assistance unpicking that part of the problem.

Not really. In the web console Device > Device info screen you’ll see three lines of firmware configuration code.
These should be copied and pasted at the VERY TOP of your sketch.

You should then delete this line:

and replace this line:

with:
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

You’ve introduced a variable called alert_sent bit it’s type and scope are not defined, so the sketch will fail to compile.
When you fix this by declaring it as a global variable of an appropriate type, and setting its initial value to false then you need to include an if test that will check the status of this variable before sending an alert.

Pete.

I have changed some few thing in the code.

Current code:

#define BLYNK_TEMPLATE_ID "xxxxx"
#define BLYNK_DEVICE_NAME "xxxxx"
#define BLYNK_AUTH_TOKEN "xxxxx"
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char ssid[] = "xxxxx";
char pass[] = "xxxxx";

BlynkTimer timer;
int pinValue = 0;

#define Buzzer D5
#define Yellow D6
#define Green D7
#define Sensor A0

void setup() {
  Serial.begin(9600);
  pinMode(Yellow, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(Buzzer, OUTPUT);
  pinMode(Sensor, INPUT);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(10000L, notifiaction);
  }

BLYNK_WRITE(V0) {
 pinValue = param.asInt();

}

void notifiaction() {
 int sensor = analogRead(Sensor);
 Serial.print("Temperature = ");
 Serial.println(sensor);
 sensor = map(sensor, 0, 1024, 0, 100);
  if (pinValue == 1) {
     if (sensor <= 50) {
        digitalWrite(Yellow, HIGH);
        digitalWrite(Green, LOW);
        digitalWrite(Buzzer, LOW);

    } else if (sensor > 50) {
       Blynk.logEvent("gas_leak_detected",  String("The gas concentration value is ") + sensor);  
      bool alert_sent = true;
      Serial.println("Blynk Alert Notification sent!");
      Serial.println();
      digitalWrite(Yellow, LOW);
      digitalWrite(Green, HIGH);
      digitalWrite(Buzzer, HIGH);
     }

    Blynk.virtualWrite(V1, sensor);
    } else {
   digitalWrite(Yellow, LOW);
   digitalWrite(Buzzer, LOW);
   digitalWrite(Green, LOW);
   bool alert_sent = false;
   }
 }
 void loop() {
   Blynk.run();
   timer.run();
}

I get this, i think its already working

[8526] Connecting to blynk.cloud:80
[12841] Ready (ping: 186ms).
Temperature = 2
Temperature = 2
Temperature = 3
Temperature = 2
Temperature = 2
Temperature = 2

This is the web dashboard:

This is the mobile dashboard:

I think it is connected already, i can switch on/off from the mobile dashboard

Unfortunately, I was unable to activate the mq2 sensor. The gas concentration level is 0. Is there any advice on how to do so?

Your notification code still makes no sense to me.

Pete.

I refer some of the coding from this website (https://srituhobby.com/iot-based-gas-leakage-monitoring-system/) and some from (Events, Notifications and the use of "Flag" variables).

In the notification code:

void notification() {
  int sensor = analogRead(Sensor);
  Serial.print("Temperature = ");
  Serial.println(sensor);
  • The analogRead(Sensor) function reads the value from an analog pin (represented by the Sensor variable), and stores it in the sensor variable. The Serial.println(sensor) function prints the value of sensor to the serial monitor.

    sensor = map(sensor, 0, 1024, 0, 100);
    
  • The map(sensor, 0, 1024, 0, 100) function maps the sensor value from the range of 0-1024 to the range of 0-100. This is done to make the reading more human-readable and understandable.

    if (pinValue == 1) {
      if (sensor <= 50) {
         digitalWrite(Yellow, HIGH);
         digitalWrite(Green, LOW);
         digitalWrite(Buzzer, LOW);
    
  • The code then checks if the value of pinValue is 1. If it is, then it checks if the sensor value is less than or equal to 50. If it is, then the system assumes that the gas concentration level is normal and turns on the yellow LED and turns off the green LED and the buzzer.

   } else if (sensor > 50) {
       Blynk.logEvent("gas_leak_detected",  String("The gas concentration value is ") + sensor);  
       bool alert_sent = true;
       Serial.println("Blynk Alert Notification sent!");
       Serial.println();
       digitalWrite(Yellow, LOW);
       digitalWrite(Green, HIGH);
       digitalWrite(Buzzer, HIGH);
     }
  • If the sensor value is greater than 50, then it assumes that a gas leak has been detected, triggers a notification using the Blynk.logEvent() function, sets the alert_sent flag to true, and turns on the green LED and the buzzer while turning off the yellow LED.

    Blynk.virtualWrite(V1, sensor);
    
  • The Blynk.virtualWrite(V1, sensor) function writes the sensor value to a virtual pin
    (represented by V1 ).

      } else {
         digitalWrite(Yellow, LOW);
         digitalWrite(Buzzer, LOW);
         digitalWrite(Green, LOW);
         bool alert_sent = false;
      }
    
  • If the pinValue value is not 1, then the system assumes that the gas concentration level is normal, turns off all LEDs and the buzzer, and resets the alert_sent flag to false.

And how does all of this prevent your code from sending a Blynk notification each time the void notification() function is called and the gas sensor level is >50 ?

If you’re calling the function every 10 seconds then after 1000 seconds (just under 17 minutes) of sensor level >50 you will have reached your 24 hour limit for events, and the device will be of no further use during that time.

Pete.

  • I modified the code. It will prevents sending a Blynk notification each time the notification() function is called and the gas sensor level is greater than 50.
  • The code checks if the gasLeakDetected flag is false and if the current sensor value is greater than the previous sensor value plus the threshold.
  • It also checks the time elapsed since the last notification to ensure that notifications are not sent too frequently.
  • If all these conditions are met, the code sends a notification and sets the gasLeakDetected flag to true to avoid sending another notification for the same gas leak.

Current code:

#define BLYNK_TEMPLATE_ID "xxxxxxxxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxxxxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxxx"

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char wifiSSID[] = "xxxxxxxxxxxx";
char wifiPassword[] = "xxxxxxxxxxxx";

BlynkTimer timer;
int gasConcentration = 0;
bool gasLeakDetected = false;

#define BUZZER_PIN D5
#define GREEN_LED_PIN D6
#define RED_LED_PIN D7
#define GAS_SENSOR_PIN A0

int previousSensorValue = 0; // variable to store the previous sensor value
const int SENSOR_THRESHOLD = 20; // minimum difference between current and previous sensor value to trigger notification
const int NOTIFICATION_INTERVAL = 3600000L; // time interval in milliseconds between notifications (1 hour)
unsigned long lastNotificationTime = 0; // variable to store the time of the last notification

void setup() {
 Serial.begin(9600);
 pinMode(GREEN_LED_PIN, OUTPUT);
 pinMode(RED_LED_PIN, OUTPUT);
 pinMode(BUZZER_PIN, OUTPUT);
 pinMode(GAS_SENSOR_PIN, INPUT);
 Blynk.begin(BLYNK_AUTH_TOKEN, wifiSSID, wifiPassword);
 timer.setInterval(NOTIFICATION_INTERVAL, notification);
}

BLYNK_WRITE(V0) {
 gasLeakDetected = param.asInt();
}

void notification() {
 int sensorValue = analogRead(GAS_SENSOR_PIN);
 Serial.print("Gas Concentration = ");
 Serial.println(sensorValue);
 gasConcentration = map(sensorValue, 0, 1024, 0, 100);
 if (gasLeakDetected) {
  if (gasConcentration <= 50) {
  digitalWrite(GREEN_LED_PIN, HIGH);
  digitalWrite(RED_LED_PIN, LOW);
  digitalWrite(BUZZER_PIN, LOW);
  gasLeakDetected = false; // reset the flag if the sensor level falls below the threshold

} else if (gasConcentration > 50 && !gasLeakDetected && gasConcentration >= previousSensorValue + SENSOR_THRESHOLD) {
// send notification if the current sensor value is significantly higher than the previous value
 unsigned long currentTime = millis();
 if (currentTime - lastNotificationTime >= NOTIFICATION_INTERVAL) {
 Blynk.logEvent("gas_leak_detected", String("The gas concentration value is ") + gasConcentration);  
 bool alertSent = true;
 Serial.println("Blynk Alert Notification sent!");
 Serial.println();
 digitalWrite(GREEN_LED_PIN, LOW);
 digitalWrite(RED_LED_PIN, HIGH);
 digitalWrite(BUZZER_PIN, HIGH);
 gasLeakDetected = true; // set the flag to true if the sensor level exceeds the threshold for the first time
 lastNotificationTime = currentTime; // update the time of the last notification
 }
}

previousSensorValue = gasConcentration; // store the current sensor value as the previous value

} else {
 digitalWrite(GREEN_LED_PIN, LOW);
 digitalWrite(BUZZER_PIN, LOW);
 digitalWrite(RED_LED_PIN, LOW);
 gasLeakDetected = false; // reset the flag if notifications are turned off
  }
}

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

That sounds way too complicated and appears to mean that you could have to wait anywhere between 1 and 2 hours before your first gas leak notification is sent.

But, if it works for you then go for it.

Pete.

It seems like the code is unpratical.

This is the modified code:

#define BLYNK_TEMPLATE_ID "xxxxx"
#define BLYNK_DEVICE_NAME "xxxxx"
#define BLYNK_AUTH_TOKEN "xxxxx"

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char ssid[] = "xxxxx";
char pass[] = "xxxxx";

BlynkTimer timer;
int pinValue = 0;

#define Buzzer D5
#define Green D6
#define Red D7
#define Sensor A0

void setup() {
 Serial.begin(9600);
 pinMode(Green, OUTPUT);
 pinMode(Red, OUTPUT);
 pinMode(Buzzer, OUTPUT);
 pinMode(Sensor, INPUT);

Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(60000L, notifiaction)
}

BLYNK_WRITE(V0) {
 pinValue = param.asInt();
}

void notification() {
 int sensor = analogRead(Sensor);
 Serial.print("Temperature = ");
 Serial.println(sensor);
 sensor = map(sensor, 0, 1024, 0, 100);

 if (pinValue == 1) {
  if (sensor <= 50) {
   digitalWrite(Green, HIGH);
   digitalWrite(Red, LOW);
    digitalWrite(Buzzer, LOW);

  } else if (sensor > 50) {
   Blynk.logEvent("gas_leak_detected",  String("The gas concentration value is ") + sensor);  
   bool alert_sent = true;
   Serial.println("Blynk Alert Notification sent!");
   Serial.println();
   digitalWrite(Green, LOW);
   digitalWrite(Red, HIGH);
   digitalWrite(Buzzer, HIGH);
  }

  Blynk.virtualWrite(V1, sensor);
  } else {
    digitalWrite(Green, LOW);
    digitalWrite(Red, LOW);
    digitalWrite(Buzzer, LOW);
    bool alert_sent = false;
  }
}

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

What’s the point in posting “modified code” that you haven’t even tried to compile, never mind test on your device?

The same question needs asking as before…

and the answer is the same - it doesn’t.

I took quite a lot of time writing the “Events, Notifications and use of “Flag” variables” tutorial. You appear to have plucked some lines of code out of that, and modified them to make the “alert_sent” variable local - so that it’s useless, without actually understanding the principal of how this variable should be used in an if statement to limit the number of notifications sent.

I’d suggest that you re-read that tutorial and try to fully understand it, then apply the principals to your sketch, compile it, test iot, and ask further questions if it doesn’t work as expected.

I’d also recommend changing this…

to a more realistic interval, such as 1000ms rather than 60000ms if you want to build a useable device, as well as fixing the spelling.

Pete.

I’ve already attempted to verify and compile it. It appears to be able to connect to Blynk, but I am unable to obtain the gas consumption value, which remains zero despite my efforts to trigger it.

Is it required to include #include <MQUnifiedsensor.h> in my code? I’m using the MQ2
sensor.

On this website (GitHub - labay11/MQ-2-sensor-library: A simple library to retrieve the information given from the MQ 2 sensor in arduino), I discovered MQ2 libraries, but I was unable to install them.

Thank you for your suggestion

Well, the code that you posted wouldn’t compile, because this line of code…

has two issues. It’s missing the semicolon at the end, and it tries to call a non-existent function called notifiaction (note that it’s spelled with an “ac” here, but a “ca” in the actual function name)…

so no, you couldn’t have successfully verified and uploaded this code.

I have no idea, I’m not familiar with this sensor or the libraries that you’re attempting to use.
The sensible approach is to get your system working without Blynk, then add the Blynk code later.

Pete.

I have change a few things on the code, i try to use the mq2 sensor library instead just do the sensor.

this is the code:

#define BLYNK_TEMPLATE_ID "xxxxxxxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxxxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxx"

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <MQ2.h>

char ssid[] = "xxxxxxxxxxx";
char pass[] = "xxxxxxxxxxx";

BlynkTimer timer;
int pinValue = 0;

#define Buzzer D5
#define Green D6
#define Red D7

float smoke_threshold = 50.00;
bool alert_sent = false;
MQ2 mq2(A0);

void setup() {
Serial.begin(9600);
mq2.begin();

pinMode(Green, OUTPUT);
pinMode(Red, OUTPUT);
pinMode(Buzzer, OUTPUT);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(1000L, notification);

}

BLYNK_WRITE(V0) {
pinValue = param.asInt();
}

void notification() {
float smoke = mq2.readSmoke();

if (smoke > smoke_threshold && alert_sent == false) {
 digitalWrite(Green, HIGH);
 digitalWrite(Red, LOW);
 digitalWrite(Buzzer, LOW);
 Blynk.logEvent("gas_leak_detected",  String("The temperature is ") +smoke);
 Serial.println("High temperature alert!");
 Serial.println("Blynk Alert Notification sent!");
 Serial.println();
 alert_sent = true;

   } else if (smoke <= smoke_threshold && alert_sent == true) {
     Serial.println("Temperature back to normal");
     Serial.println();
     alert_sent = false;
  }
}

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

It can connect to wifi and also to the blynk cloud but the serial monitor did not send any notification. Maybe because the smoke value is not above the smoke threshold

Screenshot (265)

The gas concentration level is still 0 even though I try to trigger it. I don’t know which part is wrong, maybe the coding or I just can’t trigger the mq2 sensor(?)

so why not add a line of code after…

to tell you what the value of smoke is?

Also, you appear to be building a gas leak detector, but rather than reading the LPG ppm youre reading smoke particle ppm, the referring to this as a gas leak then and also mentioning temperature, which isn’t being monitored by this device at all.

Pete.