labay11 / MQ-2-sensor-library

A simple library to retrieve the information given from the MQ 2 sensor in arduino
Apache License 2.0
50 stars 32 forks source link

Compilation error: 'mq2' was not declared in this scope #10

Open nisanisa22 opened 1 year ago

nisanisa22 commented 1 year ago

For my IoT project, I'm creating a smoke and gas detector. Although I am using this mq2 library, it appears that the readTemperature member of the MQ2 class is missing. Can somebody help me to check the error?

The code:

#define BLYNK_TEMPLATE_ID "xxxxxxxxxxxxxx" #define BLYNK_DEVICE_NAME "xxxxxxxxxxxxxx" #define BLYNK_AUTH_TOKEN "xxxxxxxxxxxxxx"

#define BLYNK_PRINT Serial

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

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

BlynkTimer timer; int pinValue = 0;

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

float t_threshold = 50.00; bool alert_sent = false;

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 t = mq2.readTemperature();

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

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

}

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

This is the error:

C:\Users\User\OneDrive\Documents\Arduino\coding-2\coding-2\coding-2.ino: In function 'void setup()': C:\Users\User\OneDrive\Documents\Arduino\coding-2\coding-2\coding-2.ino:27:3: error: 'mq2' was not declared in this scope 27 | mq2.begin(); | ^~~ C:\Users\User\OneDrive\Documents\Arduino\coding-2\coding-2\coding-2.ino: In function 'void notification()': C:\Users\User\OneDrive\Documents\Arduino\coding-2\coding-2\coding-2.ino:42:13: error: 'mq2' was not declared in this scope 42 | float t = mq2.readTemperature(); | ^~~

exit status 1

Compilation error: 'mq2' was not declared in this scope

labay11 commented 1 year ago

You need to define the mq2 variable before setup: MQ2 mq2(MQ2PIN);.