-
-
Save samirsogay/53cb24602bf537bbf9918ee43f82348b to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
const char* ssid = "Ermenegildo Zegna"; | |
const char* password = "hhjw-ofvq-pafm"; | |
//const char* mqtt_server = "test.mosquitto.org"; | |
const char* mqtt_server = "192.168.100.113"; | |
unsigned long lastUpdate; | |
int GAS_DO = 10; | |
int STATE = 0; | |
int lastState = 0; // previous state of the sensor | |
#define BUZZER_PIN D1 | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
void setup() { | |
delay(60000); // allow the MQ-6 to warm up | |
pinMode(BUILTIN_LED,OUTPUT); | |
digitalWrite(BUILTIN_LED,HIGH); | |
pinMode(GAS_DO,INPUT); | |
Serial.begin(115200); | |
setup_wifi(); | |
client.setServer(mqtt_server, 1883); | |
client.setCallback(callback); | |
reconnect(); | |
} | |
void setup_wifi(){ | |
delay(10); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
for (int i = 0; i < length; i++) { | |
Serial.print((char)payload[i]); | |
} | |
Serial.println(); | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
// Attempt to connect | |
if (client.connect("ESP8266Client")) { | |
Serial.println("connected"); | |
// Once connected, publish an announcement... | |
client.publish("ESP8266/status", "Connected!"); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void loop() { | |
//Serial.println("Before reconnect"); | |
if (!client.connected()) { | |
reconnect(); | |
} | |
// Serial.println("After reconnect"); | |
client.loop(); | |
STATE = digitalRead(GAS_DO); | |
// compare the sensor State to its previous state | |
if ((STATE != lastState)&&((millis() - lastUpdate) > 10000)) { | |
// if the state has changed, increment the counter | |
lastUpdate = millis(); | |
if (STATE == HIGH) { | |
// if the current state is HIGH then the sensor is not detecting leakage: | |
Serial.println("NO LPG LEAKAGE"); | |
digitalWrite(BUILTIN_LED,HIGH); | |
client.publish("ESP8266/LPGStatus", "OFF"); | |
} else{ | |
// if the current state is LOW then the sensor detected gas leakage: | |
digitalWrite(BUILTIN_LED, LOW); | |
Serial.println("GAS IS LEAKED"); | |
client.publish("ESP8266/LPGStatus", "ON"); | |
} | |
// Delay a little bit to avoid bouncing | |
delay(50); | |
lastState = STATE; | |
} | |
if(STATE == LOW) { | |
playTone(BUZZER_PIN, 2000, 200); | |
} | |
} | |
void playTone(int _pin, int _frequency, int _length){ | |
analogWriteFreq(_frequency); | |
analogWrite(_pin, 512); | |
delay(_length); | |
analogWrite(_pin, 0); | |
} |
Just like you have added parameters for both sensors, there should be 2 parameters for lastupdate and laststate.
I have made the changes.... but the Builtin LED is not on... Also REDLED is not flashing and buzzer is also not playing tone ... I guess these function are not exactly in loop... can't figure out whats the problem: Here's the code:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "Digoo";
const char* password = "C!$c0#B@z1ng@@";
//const char* mqtt_server = "test.mosquitto.org";
const char* mqtt_server = "10.12.47.48";
unsigned long lastUpdate;
unsigned long lastUpdate1;
int GAS_DO = D2;
int GAS_D1 = D6;
int REDLED = D5;
int GREENLED = D3;
int BLUELED = D0;
int STATE = 0;
int STATE1 = 0;
int lastState = 0; // previous state of the sensor
int lastState1 = 0; // previous state of the sensor 2
#define BUZZER_PIN D1
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
delay(6000); // allow the MQ-6 to warm up
pinMode(BUILTIN_LED, OUTPUT);
pinMode(REDLED, OUTPUT);
pinMode(GREENLED, OUTPUT);
digitalWrite(BUILTIN_LED, HIGH);
pinMode(GAS_DO, INPUT);
pinMode(GAS_D1, INPUT);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
reconnect();
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("ESP8266/status", "Connected!");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
//Serial.println("Before reconnect");
if (!client.connected()) {
reconnect();
}
// Serial.println("After reconnect");
client.loop();
STATE = digitalRead(GAS_DO);
STATE1 = digitalRead(GAS_D1);
// compare the sensor State to its previous state
if ((STATE != lastState) && ((millis() - lastUpdate) > 10000)) {
// if the state has changed, increment the counter
lastUpdate = millis();
if (STATE == HIGH) {
// if the current state is HIGH then the sensor is not detecting leakage:
Serial.println("NO LPG LEAKAGE on SENSOR 1");
digitalWrite(BUILTIN_LED, HIGH);
digitalWrite(GREENLED, HIGH);
client.publish("ESP8266/LPGStatus", "OFF");
} else {
// if the current state is LOW then the sensor detected gas leakage:
digitalWrite(BUILTIN_LED, LOW);
digitalWrite(GREENLED, LOW);
Serial.println("GAS IS LEAKED on SENSOR 1");
client.publish("ESP8266/LPGStatus", "ON");
}
// Delay a little bit to avoid bouncing
delay(50);
lastState = STATE;
if (STATE == LOW) {
playTone(BUZZER_PIN, 2000, 200);
if (STATE == LOW) {
digitalWrite(REDLED, HIGH);
delay(100);
digitalWrite(REDLED, LOW);
delay(100);
}
}
}
if ((STATE1 != lastState1) && ((millis() - lastUpdate1) > 10000)) {
// if the state has changed, increment the counter
lastUpdate1 = millis();
lastState1 = STATE1;
if (STATE1 == HIGH) {
// if the current state is HIGH then the sensor is not detecting leakage:
Serial.println("NO LPG LEAKAGE on SENSOR 2");
digitalWrite(BUILTIN_LED, HIGH);
digitalWrite(GREENLED, HIGH);
client.publish("ESP8266/LPGStatus1", "OFF");
} else {
// if the current state is LOW then the sensor detected gas leakage:
digitalWrite(BUILTIN_LED, LOW);
digitalWrite(GREENLED, LOW);
Serial.println("GAS IS LEAKED on SENSOR 2");
client.publish("ESP8266/LPGStatus1", "ON");
}
// Delay a little bit to avoid bouncing
delay(50);
if (STATE1 == LOW) {
playTone(BUZZER_PIN, 2000, 200);
if (STATE1 == LOW) {
digitalWrite(BLUELED, HIGH);
delay(100);
digitalWrite(BLUELED, LOW);
delay(100);
}
}
}
}
void playTone(int _pin, int _frequency, int _length) {
analogWriteFreq(_frequency);
analogWrite(_pin, 512);
delay(_length);
analogWrite(_pin, 0);
}
can i use MQ2 sensor and ESP8266 CH340 version for this project?
Thanks for the code. I am trying to add another MQ2 sensor with few LED's as I sensor is not enough for me.. I tried modifying your code but for some reason I can't get it working. Can you help?
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "MYSSID";
const char* password = "MYWEPKEY";
//const char* mqtt_server = "test.mosquitto.org";
const char* mqtt_server = "192.168.1.48";
unsigned long lastUpdate;
int GAS_DO = D2;
int GAS_D1 = D6;
int REDLED = D5;
int GREENLED = D3;
int BLUELED = D8;
int STATE = 0;
int STATE1 = 0;
int lastState = 0; // previous state of the sensor
#define BUZZER_PIN D1
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
delay(6000); // allow the MQ-6 to warm up
pinMode(BUILTIN_LED,OUTPUT);
pinMode(REDLED,OUTPUT);
pinMode(GREENLED,OUTPUT);
digitalWrite(BUILTIN_LED,HIGH);
pinMode(GAS_DO,INPUT);
pinMode(GAS_D1,INPUT);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
reconnect();
}
void setup_wifi(){
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("ESP8266/status", "Connected!");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
//Serial.println("Before reconnect");
if (!client.connected()) {
reconnect();
}
// Serial.println("After reconnect");
client.loop();
STATE = digitalRead(GAS_DO);
STATE1 = digitalRead(GAS_D1);
// compare the sensor State to its previous state
if ((STATE != lastState)&&((millis() - lastUpdate) > 10000)) {
// if the state has changed, increment the counter
lastUpdate = millis();
if (STATE == HIGH) {
// if the current state is HIGH then the sensor is not detecting leakage:
Serial.println("NO LPG LEAKAGE on SENSOR 1");
digitalWrite(BUILTIN_LED,HIGH);
digitalWrite(GREENLED,HIGH);
client.publish("ESP8266/LPGStatus", "OFF");
} else{
// if the current state is LOW then the sensor detected gas leakage:
digitalWrite(BUILTIN_LED, LOW);
digitalWrite(GREENLED,LOW);
Serial.println("GAS IS LEAKED on SENSOR 1");
client.publish("ESP8266/LPGStatus", "ON");
}
// Delay a little bit to avoid bouncing
delay(50);
lastState = STATE;
}
if(STATE == LOW) {
playTone(BUZZER_PIN, 2000, 200);
if(STATE == LOW) {
digitalWrite(REDLED,HIGH);
delay(100);
digitalWrite(REDLED,LOW);
delay(100);
digitalWrite (GREENLED, LOW);
}
}
if ((STATE1 != lastState)&&((millis() - lastUpdate) > 10000)) {
// if the state has changed, increment the counter
lastUpdate = millis();
if (STATE1 == HIGH) {
// if the current state is HIGH then the sensor is not detecting leakage:
Serial.println("NO LPG LEAKAGE on SENSOR 2");
digitalWrite(BUILTIN_LED,HIGH);
digitalWrite(GREENLED,HIGH);
client.publish("ESP8266/LPGStatus1", "OFF");
} else{
// if the current state is LOW then the sensor detected gas leakage:
digitalWrite(BUILTIN_LED, LOW);
digitalWrite(GREENLED,LOW);
Serial.println("GAS IS LEAKED on SENSOR 2");
client.publish("ESP8266/LPGStatus1", "ON");
}
// Delay a little bit to avoid bouncing
delay(50);
lastState = STATE1;
}
if(STATE1 == LOW) {
playTone(BUZZER_PIN, 2000, 200);
if(STATE1 == LOW) {
digitalWrite(BLUELED,HIGH);
delay(100);
digitalWrite(BLUELED,LOW);
delay(100);
digitalWrite (GREENLED, LOW);
}
}
void playTone(int _pin, int _frequency, int _length){
analogWriteFreq(_frequency);
analogWrite(_pin, 512);
delay(_length);
analogWrite(_pin, 0);
}
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "MYSSID";
const char* password = "MYWEPKEY";
//const char* mqtt_server = "test.mosquitto.org";
const char* mqtt_server = "192.168.1.48";
unsigned long lastUpdate;
int GAS_DO = D2;
int GAS_D1 = D6;
int REDLED = D5;
int GREENLED = D3;
int BLUELED = D8;
int STATE = 0;
int STATE1 = 0;
int lastState = 0; // previous state of the sensor
#define BUZZER_PIN D1
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
delay(6000); // allow the MQ-6 to warm up
pinMode(BUILTIN_LED,OUTPUT);
pinMode(REDLED,OUTPUT);
pinMode(GREENLED,OUTPUT);
digitalWrite(BUILTIN_LED,HIGH);
pinMode(GAS_DO,INPUT);
pinMode(GAS_D1,INPUT);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
reconnect();
}
void setup_wifi(){
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("ESP8266/status", "Connected!");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
//Serial.println("Before reconnect");
if (!client.connected()) {
reconnect();
}
// Serial.println("After reconnect");
client.loop();
STATE = digitalRead(GAS_DO);
STATE1 = digitalRead(GAS_D1);
// compare the sensor State to its previous state
if ((STATE != lastState)&&((millis() - lastUpdate) > 10000)) {
// if the state has changed, increment the counter
lastUpdate = millis();
if (STATE == HIGH) {
// if the current state is HIGH then the sensor is not detecting leakage:
Serial.println("NO LPG LEAKAGE on SENSOR 1");
digitalWrite(BUILTIN_LED,HIGH);
digitalWrite(GREENLED,HIGH);
client.publish("ESP8266/LPGStatus", "OFF");
} else{
// if the current state is LOW then the sensor detected gas leakage:
digitalWrite(BUILTIN_LED, LOW);
digitalWrite(GREENLED,LOW);
Serial.println("GAS IS LEAKED on SENSOR 1");
client.publish("ESP8266/LPGStatus", "ON");
}
// Delay a little bit to avoid bouncing
delay(50);
lastState = STATE;
}
if(STATE == LOW) {
playTone(BUZZER_PIN, 2000, 200);
if(STATE == LOW) {
digitalWrite(REDLED,HIGH);
delay(100);
digitalWrite(REDLED,LOW);
delay(100);
digitalWrite (GREENLED, LOW);
}
}
if ((STATE1 != lastState)&&((millis() - lastUpdate) > 10000)) {
// if the state has changed, increment the counter
lastUpdate = millis();
if (STATE1 == HIGH) {
// if the current state is HIGH then the sensor is not detecting leakage:
Serial.println("NO LPG LEAKAGE on SENSOR 2");
digitalWrite(BUILTIN_LED,HIGH);
digitalWrite(GREENLED,HIGH);
client.publish("ESP8266/LPGStatus1", "OFF");
} else{
// if the current state is LOW then the sensor detected gas leakage:
digitalWrite(BUILTIN_LED, LOW);
digitalWrite(GREENLED,LOW);
Serial.println("GAS IS LEAKED on SENSOR 2");
client.publish("ESP8266/LPGStatus1", "ON");
}
// Delay a little bit to avoid bouncing
delay(50);
lastState = STATE1;
}
if(STATE1 == LOW) {
playTone(BUZZER_PIN, 2000, 200);
if(STATE1 == LOW) {
digitalWrite(BLUELED,HIGH);
delay(100);
digitalWrite(BLUELED,LOW);
delay(100);
digitalWrite (GREENLED, LOW);
}
}
void playTone(int _pin, int _frequency, int _length){
analogWriteFreq(_frequency);
analogWrite(_pin, 512);
delay(_length);
analogWrite(_pin, 0);
}