-->
Page 1 of 1

Mqtt button - pressed and hold down message

PostPosted: Wed Jan 08, 2020 1:38 pm
by gfo
Hi!

First - i'm complete newbie on this...

With the help of internet i have put together a code for sending status on 4 inputs over mqtt, so far so good.

But i now want it to act like this (for inputs from a push button):
Short activation of input (press and release button) - Send "On"
Long activation of input (press and hold button) - Send "Hold"
Deactivation of input (when button is released) - Send "Off"

And, i only want the message to be sendt when there is a change of status.

Can anybody help me with some guidance?

The code:
Code: Select all#include <EEPROM.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <ESP8266WiFi.h>




// Connect to the WiFi
const char* ssid = "...";
const char* password = "...";
// Connect to the MQTT Broker
const char* mqtt_server = "...";
const char* mqtt_username ="";
const char* mqtt_password = "";
const char* clientID = "ESP8266_1";


// this constant won't change:
const int buttonPin1 = D1;    // the pin that the pushbutton is attached to
const int buttonPin2 = D2;    // the pin that the pushbutton is attached to
const int buttonPin5 = D5;    // the pin that the pushbutton is attached to
const int buttonPin6 = D6;    // the pin that the pushbutton is attached to


// Variables will change:
int buttonPushCounter1 = 0;   // counter for the number of button presses
int buttonState1 = 0;         // current state of the button
int lastButtonState1 = 0;     // previous state of the button

int buttonPushCounter2 = 0;   // counter for the number of button presses
int buttonState2 = 0;         // current state of the button
int lastButtonState2 = 0;     // previous state of the button

int buttonPushCounter5 = 0;   // counter for the number of button presses
int buttonState5 = 0;         // current state of the button
int lastButtonState5 = 0;     // previous state of the button

int buttonPushCounter6 = 0;   // counter for the number of button presses
int buttonState6 = 0;         // current state of the button
int lastButtonState6 = 0;     // previous state of the button


WiFiClient espClient;
PubSubClient client(espClient);



//void callback(char* topic, byte* payload, unsigned int length) {

//}


void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.println("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(clientID, mqtt_username, mqtt_password)) {
      Serial.println("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 setup()
{
  {

    client.setServer(mqtt_server, 1883);
    //client.setCallback(callback);

    // initialize the button pin as a input:
    pinMode(buttonPin1, INPUT_PULLUP);
    pinMode(buttonPin2, INPUT_PULLUP);
    pinMode(buttonPin5, INPUT_PULLUP);
    pinMode(buttonPin6, INPUT_PULLUP);

    // initialize serial communication:
    Serial.begin(9600);
  }

// Connect to WiFinetwork
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    //Serial.begin(9600);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  //Serial.begin(9600);
  // Start the server
  // server.begin();
  // Serial.println("Server started");
  //Serial.begin(9600);
  // Print the IP address
  Serial.print("Local IP: ");
  Serial.println(WiFi.localIP());
}

void loop() {

  // read the pushbutton input pin1:
  buttonState1 = digitalRead(buttonPin1);

  // compare the buttonState to its previous state
  if (buttonState1 != lastButtonState1) {
    // if the state has changed, increment the counter
    if (buttonState1 == HIGH) {
      client.publish("ESP8266_1/Input1", "OFF"); //
      // if the current state is HIGH then the button
      // went from off to on:
    } else {
      // if the current state is LOW then the button
      // went from on to off:
      client.publish("ESP8266_1/Input1", "ON"); //
    }
    // Delay a little bit to avoid bouncing
    delay(100);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState1 = buttonState1;



  // read the pushbutton input pin2:
  buttonState2 = digitalRead(buttonPin2);

  // compare the buttonState to its previous state
  if (buttonState2 != lastButtonState2) {
    // if the state has changed, increment the counter
    if (buttonState2 == HIGH) {
      client.publish("ESP8266_1/Input2", "OFF"); //
      // if the current state is HIGH then the button
      // went from off to on:
    } else {
      // if the current state is LOW then the button
      // went from on to off:
      client.publish("ESP8266_1/Input2", "ON"); //
    }
    // Delay a little bit to avoid bouncing
    delay(100);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState2 = buttonState2;
 



    // read the pushbutton input pin5:
  buttonState5 = digitalRead(buttonPin5);

  // compare the buttonState to its previous state
  if (buttonState5 != lastButtonState5) {
    // if the state has changed, increment the counter
    if (buttonState5 == HIGH) {
      client.publish("ESP8266_1/Input5", "OFF"); //
      // if the current state is HIGH then the button
      // went from off to on:
    } else {
      // if the current state is LOW then the button
      // went from on to off:
      client.publish("ESP8266_1/Input5", "ON"); //
    }
    // Delay a little bit to avoid bouncing
    delay(100);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState5 = buttonState5;
 

    // read the pushbutton input pin6:
  buttonState6 = digitalRead(buttonPin6);

  // compare the buttonState to its previous state
  if (buttonState6 != lastButtonState6) {
    // if the state has changed, increment the counter
    if (buttonState6 == HIGH) {
      client.publish("ESP8266_1/Input6", "OFF"); //
      // if the current state is HIGH then the button
      // went from off to on:
    } else {
      // if the current state is LOW then the button
      // went from on to off:
      client.publish("ESP8266_1/Input6", "ON"); //
    }
    // Delay a little bit to avoid bouncing
    delay(100);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState6 = buttonState6;
 
  {
      if (!client.connected()) {
      reconnect();
    }
    client.loop();
  }
}