diff --git a/docs/prerequisites/board.md b/docs/prerequisites/board.md
index 0d37a1c..9947ab2 100644
--- a/docs/prerequisites/board.md
+++ b/docs/prerequisites/board.md
@@ -4,7 +4,7 @@ OpenMQTTGateway is not closed to one board or type of board, by using the power
You can take a look to the [OpenMQTTGateway compatible website](https://compatible.openmqttgateway.com) to have a view of the [supported boards](https://compatible.openmqttgateway.com/index.php/boards/).
-Moreover the gateways capacities can be extended with sensors; DHT, HC SR501, ADC, I2C bus, INA226, TEMT6000, TSL2561, BME280/BMP280, HTU21D, AHTx0, DS1820
+Moreover the gateways capacities can be extended with sensors; DHT, HC SR501, ADC, I2C bus, INA226, MQ2, TEMT6000, TSL2561, BME280/BMP280, HTU21D, AHTx0, DS1820
or actuators; LED, relays, PWM.
::: tip Running on a computer
diff --git a/docs/setitup/sensors.md b/docs/setitup/sensors.md
index d58ad67..384153b 100644
--- a/docs/setitup/sensors.md
+++ b/docs/setitup/sensors.md
@@ -12,6 +12,7 @@
|GPIO Input|Inputs|-|
|GPIO KeyCode|Keycode|[parts list](https://compatible.openmqttgateway.com/index.php/parts)|
|INA226|Current, Voltage|[parts list](https://compatible.openmqttgateway.com/index.php/parts)|
+|MQ2|Gas (flammable)|[parts list](https://compatible.openmqttgateway.com/index.php/parts)|
|TEMT6000|Luminosity|[parts list](https://compatible.openmqttgateway.com/index.php/parts)|
|TSL2561|Luminosity|[parts list](https://compatible.openmqttgateway.com/index.php/parts)|
@@ -29,6 +30,7 @@
|HTU21 SCL|A5|D1|22|
|INA226 SDA|A4|D2|21|
|INA226 SCL|A5|D1|22|
+|MQ02 |A0 + D4|A0 + D4|A0 + D4|
|TEMT6000 |A0|A0|A0|
|TSL2561 SDA|A4|D2|21|
|TSL2561 SCL|A5|D1|22|
diff --git a/environments.ini b/environments.ini
index f8660de..10617a7 100644
--- a/environments.ini
+++ b/environments.ini
@@ -99,6 +99,7 @@ build_flags =
'-DZsensorHTU21="HTU21"'
'-DZsensorAHTx0="AHTx0"'
'-DZsensorTEMT6000="TEMT6000"'
+ '-DZsensorMQ2="MQ2"'
'-DZsensorTSL2561="TSL2561"'
'-DZsensorDHT="DHT"'
'-DZsensorDS1820="DS1820"'
@@ -1106,6 +1107,7 @@ build_flags =
;'-DZsensorHTU21="HTU21"'
'-DZsensorAHTx0="AHTx0"'
'-DZsensorTEMT6000="TEMT6000"'
+ '-DZsensorMQ2="MQ2"'
'-DZsensorTSL2561="TSL2561"'
'-DZsensorDHT="DHT"'
'-DZsensorDS1820="DS1820"'
diff --git a/main/User_config.h b/main/User_config.h
index fdc01a3..7886e50 100644
--- a/main/User_config.h
+++ b/main/User_config.h
@@ -326,6 +326,7 @@ int lowpowermode = DEFAULT_LOW_POWER_MODE;
//#define ZsensorHCSR501 "HCSR501" //ESP8266, Arduino, ESP32, Sonoff RF Bridge
//#define ZsensorADC "ADC" //ESP8266, Arduino, ESP32
//#define ZsensorBH1750 "BH1750" //ESP8266, Arduino, ESP32
+//#define ZsensorMQ2 "MQ2" //ESP8266, Arduino, ESP32
//#define ZsensorTEMT6000 "TEMT6000" //ESP8266
//#define ZsensorTSL2561 "TSL2561" //ESP8266, Arduino, ESP32
//#define ZsensorBME280 "BME280" //ESP8266, Arduino, ESP32
diff --git a/main/ZmqttDiscovery.ino b/main/ZmqttDiscovery.ino
index 0ac8858..4e7a59f 100644
--- a/main/ZmqttDiscovery.ino
+++ b/main/ZmqttDiscovery.ino
@@ -768,6 +768,27 @@ void pubMqttDiscovery() {
}
# endif
+# ifdef ZsensorMQ2
+# define MQ2parametersCount 2
+ Log.trace(F("MQ2Discovery" CR));
+ char* MQ2sensor[MQ2parametersCount][8] = {
+ {"sensor", "gas", "MQ2", "", jsonVal, "", "", "ppm"},
+ {"binary_sensor", "MQ2", "", "", jsonPresence, "true", "false", ""}
+ //component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
+ };
+
+ for (int i = 0; i < MQ2parametersCount; i++) {
+ createDiscovery(MQ2sensor[i][0],
+ subjectMQ2toMQTT, MQ2sensor[i][1], (char*)getUniqueId(MQ2sensor[i][1], MQ2sensor[i][2]).c_str(),
+ will_Topic, MQ2sensor[i][3], MQ2sensor[i][4],
+ MQ2sensor[i][5], MQ2sensor[i][6], MQ2sensor[i][7],
+ 0, Gateway_AnnouncementMsg, will_Message, true, "",
+ "", "", "", "", false, // device name, device manufacturer, device model, device ID, retain
+ stateClassNone //State Class
+ );
+ }
+# endif
+
# ifdef ZsensorTEMT6000
# define TEMT6000parametersCount 3
Log.trace(F("TEMT6000Discovery" CR));
diff --git a/main/ZsensorMQ2.ino b/main/ZsensorMQ2.ino
new file mode 100644
index 0000000..91fd1e8
--- /dev/null
+++ b/main/ZsensorMQ2.ino
@@ -0,0 +1,73 @@
+/*
+ OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation
+
+ Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
+ Send and receiving command by MQTT
+
+ This is the MQ2 GAS Sensor Addon based on modules with a MQ:
+ - Measures flammable gas
+ - Required Hardware Module: MQ2
+
+ Connection Scheme:
+ --------------------
+
+ MQ-2 -------> Arduino Uno ----------> ESP8266
+ ==============================================
+ Vcc ---------> 3.3V or 5V------------> 3.3V or 5V
+ GND ---------> GND ------------------> GND
+ A0 ----------> Pin A0 ---------------> A0
+ D0 ----------> Pin D4 ---------------> D4
+
+ This file is part of OpenMQTTGateway.
+
+ OpenMQTTGateway is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ OpenMQTTGateway is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see .
+*/
+#include "User_config.h"
+
+#ifdef ZsensorMQ2
+
+# include "Wire.h"
+# include "math.h"
+
+void setupZsensorMQ2() {
+ Log.notice(F("Setup MQ2 detection on pin: %d" CR), MQ2SENSORDETECTPIN);
+ pinMode(MQ2SENSORDETECTPIN, INPUT); // declare GPIOInput pin as input_pullup to prevent floating. Pin will be high when not connected to ground
+
+ Log.notice(F("Starting MQ2 calibration on pin: %d" CR), MQ2SENSORADCPIN);
+
+ //Simple calibrate
+ float sensorValue;
+ for (int x = 0; x < 1000; x++) {
+ analogRead(MQ2SENSORADCPIN);
+ }
+ delay(1000);
+
+ Log.trace(F("MQ2 Initialized." CR));
+}
+
+void MeasureGasMQ2() {
+ if (millis() > (timemq2 + TimeBetweenReadingmq2)) {
+ timemq2 = millis();
+
+ Log.trace(F("Creating MQ2 buffer" CR));
+ StaticJsonDocument jsonBuffer;
+ JsonObject MQ2data = jsonBuffer.to();
+
+ MQ2data["gas"] = analogRead(MQ2SENSORADCPIN);
+ MQ2data["detected"] = digitalRead(MQ2SENSORDETECTPIN) == HIGH ? "false" : "true";
+
+ pub(subjectMQ2toMQTT, MQ2data);
+ }
+}
+#endif
diff --git a/main/config_DHT.h b/main/config_DHT.h
index e578811..16d7333 100644
--- a/main/config_DHT.h
+++ b/main/config_DHT.h
@@ -36,7 +36,9 @@ extern void DHTtoMQTT();
/*-------------DHT SENSOR TYPE-------------*/
//#define DHT_SENSOR_TYPE DHT11 //uncomment for DHT11 Sensor
//#define DHT_SENSOR_TYPE DHT21 //uncomment for DHT21 Sensor
-#define DHT_SENSOR_TYPE DHT22 //uncomment for DHT22 Sensor (default for backwards compatibility)
+#ifndef DHT_SENSOR_TYPE
+# define DHT_SENSOR_TYPE DHT22 //uncomment for DHT22 Sensor (default for backwards compatibility)
+#endif
/*-------------------PIN DEFINITIONS----------------------*/
#ifndef DHT_RECEIVER_GPIO
# if defined(ESP8266)
diff --git a/main/config_MQ2.h b/main/config_MQ2.h
new file mode 100644
index 0000000..6367c74
--- /dev/null
+++ b/main/config_MQ2.h
@@ -0,0 +1,66 @@
+/*
+ OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation
+
+ Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
+ Send and receiving command by MQTT
+
+ This is the MQ2 GAS Sensor Addon based on modules with a MQ:
+ - Measures flammable gas
+ - Required Hardware Module: MQ2
+
+ Connection Scheme:
+ --------------------
+
+ MQ-2 -------> Arduino Uno ----------> ESP8266
+ ==============================================
+ Vcc ---------> 3.3V or 5V------------> 3.3V or 5V
+ GND ---------> GND ------------------> GND
+ A0 ----------> Pin A0 ---------------> A0
+ D0 ----------> Pin D4 ---------------> D4
+
+ This file is part of OpenMQTTGateway.
+
+ OpenMQTTGateway is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ OpenMQTTGateway is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see .
+*/
+#ifndef config_MQ2_h
+#define config_MQ2_h
+
+extern void setupZsensorMQ2();
+extern void MQ2toMQTT();
+
+#ifndef MQ2SENSORADCPIN
+# ifdef ESP32
+# define MQ2SENSORADCPIN 5 //MQ2 Gas sensor reading = ADC0
+# else
+# define MQ2SENSORADCPIN A0 //MQ2 Gas sensor reading = ADC0
+# endif
+#endif
+
+#ifndef MQ2SENSORDETECTPIN
+# ifdef ESP32
+# define MQ2SENSORDETECTPIN 24 //MQ2 Gas sensor detected flag
+# else
+# define MQ2SENSORDETECTPIN D4 //MQ2 Gas sensor detected flag
+# endif
+#endif
+
+#define TimeBetweenReadingmq2 10000
+/*----------------------------USER PARAMETERS-----------------------------*/
+/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/
+#define subjectMQ2toMQTT "/GAStoMQTT/mq2"
+
+//Time used to wait for an interval before resending measured values
+unsigned long timemq2 = 0;
+
+#endif
\ No newline at end of file
diff --git a/main/main.ino b/main/main.ino
index d7dbcbe..2f7b741 100644
--- a/main/main.ino
+++ b/main/main.ino
@@ -109,6 +109,9 @@ struct GfSun2000Data {};
#ifdef ZsensorBH1750
# include "config_BH1750.h"
#endif
+#ifdef ZsensorMQ2
+# include "config_MQ2.h"
+#endif
#ifdef ZsensorTEMT6000
# include "config_TEMT6000.h"
#endif
@@ -879,6 +882,10 @@ void setup() {
setupZsensorBH1750();
modules.add(ZsensorBH1750);
#endif
+#ifdef ZsensorMQ2
+ setupZsensorMQ2();
+ modules.add(ZsensorMQ2);
+#endif
#ifdef ZsensorTEMT6000
setupZsensorTEMT6000();
modules.add(ZsensorTEMT6000);
@@ -1655,6 +1662,9 @@ void loop() {
#ifdef ZsensorBH1750
MeasureLightIntensity(); //Addon to measure Light Intensity with a BH1750
#endif
+#ifdef ZsensorMQ2
+ MeasureGasMQ2();
+#endif
#ifdef ZsensorTEMT6000
MeasureLightIntensityTEMT6000();
#endif