Arduino ESP8266两个MQ2传感器数字读取

时间:2019-07-09 08:18:34

标签: arduino arduino-ide nodemcu arduino-esp8266

我正在使用带MQ2 LPG传感器的ESP8266 NodeMCU v3开发板。我修改了samirsogay代码,添加了一个额外的传感器和几个LED。由于某种原因,它无法按我期望的方式工作。我是Arduino新手,正在学习编码,因此您可能会想到愚蠢的错误:)

传感器1 =如果检测到气体泄漏,请闪烁红色LED并播放蜂鸣器声音。 传感器2 =如果检测到气体泄漏,请闪烁蓝色LED并播放蜂鸣器声音。

未检测到泄漏时,BuilinIn LED点亮,绿色LED点亮。

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "SSID";
const char* password = "PASSWORD@!@";

//const char* mqtt_server = "test.mosquitto.org";
const char* mqtt_server = "192.168.1.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);
}

代码无法循环工作。如果未检测到气体泄漏,则nodemcu的内置LED应该与绿色LED一起点亮,但是只要在SENSOR1或SENSOR2中的任何一个上检测到气体泄漏,nodemcu的内置LED应当与绿色LED和相应的传感器LED一起熄灭(SENSOR1 =红色并且SENSOR2 = BLUE)应与蜂鸣器一起闪烁,并发出在任何传感器上检测到的TILL气体泄漏的提示音。

在每个步骤中,还发布了MQTT,但是代码似乎不起作用。不知道怎么了。

0 个答案:

没有答案