Mqtt无限循环

问题描述 投票:-2回答:2

我有一个简单的ldr连接到我的nodemcu和一个mqtt草图,当超过设定的阈值时发送mqtt有效载荷。它运行良好,唯一的问题是,只要超过阈值,它就会发送多个mqtt有效负载。这个问题的一个例子是,当我把手掌放在LDR上时,它每隔毫秒发送一条mqtt消息,从LDR读取值。我不希望如此。

我希望LDR的值每毫秒输出一次(它已经这样做了)但我希望mqtt消息在超过阈值时仅在一分钟内发送一次。这是我的素描。我很感激能得到的所有帮助。

void setup() {
delay(10);
Serial.begin(115200);
Serial.println();
Serial.println();

// Initialising the UI will init the display too.
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_16);
//display.display();
// delay(300);
//display.drawString(0, 10, "connecting to the grid..."
//display.display();

pinMode(2, OUTPUT); //pin connected to the led 
Serial.print("Connecting to ");

Serial.println(ssid);

WiFi.begin(ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) 
delay(500);
Serial.print(".");
Serial.println("WiFi connected");
//splay.drawString(0, 10, "ldr on");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (client.connect(clientID)) {
Serial.println("Connected to MQTT Broker!");
}
else 
Serial.println("Connection to MQTT Broker failed...");
}


void drawFontFaceDemo() {
// Font Demo1
// create more fonts at http://oleddisplay.squix.ch/
// display.setTextAlignment(TEXT_ALIGN_LEFT);
// display.setFont(ArialMT_Plain_16);
// display.drawString(0, 0, "Hello world");
// display.setFont(ArialMT_Plain_16);
// display.drawString(0, 10, "Hello world");
// display.setFont(ArialMT_Plain_24);
// display.drawString(0, 26, "Hello world");


}

void drawProgressBarDemo() {
int progress = (counter / 5) % 100;
// draw the progress bar
display.drawProgressBar(0, 32, 120, 10, progress);

// draw the percentage as String
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(64, 15, String(progress) + "%");
}

void drawImageDemo() {
// see http://blog.squix.org/2015/05/esp8266-n ... e-xbm.html
// on how to create xbm files
display.drawXbm(34, 14, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits);
}

//Demo demos[] = {drawFrontFaceDemo , drawImageDemo , drawProgressBarDemo, };

Demo demos[] = {drawProgressBarDemo, drawImageDemo};


int demoLength = (sizeof(demos) / sizeof(Demo));
long timeSinceLastModeSwitch = 0;

void loop() {
// clear the display
display.clear();
// draw the current demo method
demos[demoMode]();

display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.drawString(10, 128, String(millis()));
// write the buffer to the display
display.display();

if (millis() - timeSinceLastModeSwitch > DEMO_DURATION) {
demoMode = (demoMode + 1) % demoLength;
timeSinceLastModeSwitch = millis();
}
{
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
if (sensorValue > 700){
digitalWrite(5, HIGH);
if (client.publish(mqtt_topic, "**!!ALERT!!**")){
Serial.println("Tripped and message sent!");
} 
else{
Serial.println("Message failed to send. Reconnecting to MQTT Broker and trying again");
client.connect(clientID);
delay(10); // This delay ensures that client.publish doesn't clash with the client.connect call
client.publish(mqtt_topic, "**!!ALERT!!**"); }
} digitalWrite(5, LOW);}
// counter++;
// delay(10);
}
arduino mqtt
2个回答
0
投票

您可以使用变量作为FLAG来指示数据已发送一次!


0
投票

我认为可以这样做:

int FLAG_SEND=0;
int countMinute;
//...
//...
//...
if(countMinute>1){ //countMinute can be done in a various ways
    countMinute = 0;
    FLAG_SEND = 1;
}



if (sensorValue > 700){

    if(FLAG_SEND){
        digitalWrite(5, HIGH);
        if (client.publish(mqtt_topic, "**!!ALERT!!**")){
            Serial.println("Tripped and message sent!");
        } 
        else{
            Serial.println("Message failed to send. Reconnecting to MQTT Broker and trying again");
            client.connect(clientID);
            delay(10); // This delay ensures that client.publish doesn't clash with the client.connect call
            client.publish(mqtt_topic, "**!!ALERT!!**"); 
        }
        FLAG_SEND = 0;
    }
}

最好的祝福

古斯塔沃

© www.soinside.com 2019 - 2024. All rights reserved.