-->
Page 1 of 1

Mqtt multiple publication

PostPosted: Sat Feb 24, 2018 3:55 am
by netromaster
Hey guys, esp8266 and C noob here. I crossed over from the raspberry pi and python some 2 months ago. Been learning fast but now i have cause to seek for help.

I have a simple ldr connected to my nodemcu and an mqtt sketch that sends an mqtt payload when a set threshold is exceeded. It works well, only problem being that it sends multiple mqtt payloads for as long as the threshold is exceeded. An example of this issue will be that when i put my palm over the LDR it sends an mqtt message for every millisecond is reads the value from the LDR. I do not want it so.

I want the values from the LDR to be outputted every millisecond (it does this already) but i want the mqtt message to be sent only once in one minute when the threshold is exceeded.
Here's my sketch. I appreciate all the help i can get.


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);
}