need help converting this code to act as a momentary push button.

Hello, I have been trying to get this code to work with a push button.
I got the advice to use "blink without delay" but i don't know how to implement it to also use Mqtt on the ethernet shield.

I was hoping it would be a simple step to change from toggle to pushbutton.

I have got this setup working with home assistant, and made an automation to turn the switch back of after .5 second.
But this still isn't working for me because it only activates a switch when you press an release.
For a doorbell and a drive through sensor this isn't working well.

/*
 Example MQTT-switch-relay-node with 4 buttons and 4 leds 
 
  - connects to an MQTT server
  - publishes "hello world" to the topic "led"
  - subscribes to the topic "led"
  - controls 4 leds on pins 2,3,5 and 6 - leds can be replaced with relays
  - reads 4 button on pins 7,8,9 and 10
  - turns on/off a specific led when it receives a specific "on"/"off" from the "led" topic
  - sends a specific "on"/"off" to the "led" topic a specific button is pressed
  - multiple arduino's with same generic sketch can run parallel to each other
  - multiple arduino's need each to have a unique ip-addres, unique mac address and unique MQTT client-ID

  - tested on arduino-uno with W5100 ethernet shield
  - Ethernet Shield W5100 uses pins 4,10,11,12,13
  - availbale digital pins: 1,2,3,5,6,7,8,9,10 
*/

//------------------------------------------------------------------------------

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <Bounce2.h>

// Set led variables to Arduino digital pins
int led1 = 2;
int led2 = 3;
int led3 = 5;                             // pin 4 used by ethernet shield
int led4 = 6;


// Set button variables to Arduino digital pins
int button1 = 7;
int button2 = 8;
int button3 = 9;
int button4 = 10;                         // pins 11,12,13 used by ethernetshield

// Set variables to act as virtual switches
// Set variable values initially to LOW (and not HIGH)
int led1Value = LOW;             
int led2Value = LOW;
int led3Value = LOW;
int led4Value = LOW;


//---------------------------------------------------------------------------

// Arduino MAC address is on a sticker on your Ethernet shield
// must be unique for every node in same network
// To make a new unique address change last letter

byte mac[]    = { 0xxE, 0xxD, 0xxE, 0xxF, 0xxE, 0xxE };  

// Unique static IP address of this Arduino - change to adapt to your network
IPAddress ip(x, x, x, x);

// IP Address of your MQTT broker - change to adapt to your network
IPAddress server(x, x, x, x);

// Handle and convert incoming MQTT messages ----------------------------------------

void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived
  String content="";
  char character;
  for (int num=0;num<length;num++) {
      character = payload[num];
      content.concat(character);
  }   
  Serial.println(topic);
  Serial.println(content); // message sent out by button actions is returned from broker and serial printed


// Set specific virtual switches on basis of specific incoming messages ----------------------------
  
  if (content == "1on") {
    led1Value = HIGH;
  }
  
  if (content == "1off") {
    led1Value = LOW;
  }
  
  if (content == "2on") {
    led2Value = HIGH;
  }
  
  if (content == "2off") {
    led2Value = LOW;
  }

  
  if (content == "3on") {
    led3Value = HIGH;
  }
  
  if (content == "3off") {
    led3Value = LOW;
  }
  
  if (content == "4on") {
    led4Value = HIGH;
  }
  
  if (content == "4off") {
    led4Value = LOW;
  }
  
    
  // Set digital pin states according to virtual switch settings
    
  digitalWrite(led1,led1Value);
  digitalWrite(led2,led2Value);
  digitalWrite(led3,led3Value);
  digitalWrite(led4,led4Value);

}

// Initiate instances -----------------------------------

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

// Initiate a bouncer instance for each button
Bounce bouncer1 = Bounce();
Bounce bouncer2 = Bounce();
Bounce bouncer3 = Bounce();
Bounce bouncer4 = Bounce();

//-------------------------------------------------------

void setup()

{

  // setup led, button, bouncer 1 -----------------------
  pinMode(led1, OUTPUT);
  pinMode(button1,INPUT);
  digitalWrite(button1,HIGH);
  bouncer1 .attach(button1);
  bouncer1 .interval(5);

  // setup led, button, bouncer 2 -----------------------
  pinMode(led2, OUTPUT);
  pinMode(button2,INPUT);
  digitalWrite(button2,HIGH);
  bouncer2 .attach(button2);
  bouncer2 .interval(5);

  // setup led, button, bouncer 3 -----------------------
  pinMode(led3, OUTPUT);
  pinMode(button3,INPUT);
  digitalWrite(button3,HIGH);
  bouncer3 .attach(button3);
  bouncer3 .interval(5);

  // setup led, button, bouncer 4 -----------------------
  pinMode(led4, OUTPUT);
  pinMode(button4,INPUT);
  digitalWrite(button4,HIGH);
  bouncer4 .attach(button4);
  bouncer4 .interval(5);

  // setup serial and ethernet communications -------------------------------

  // Setup serial connection
  Serial.begin(9600);

  // Setup ethernet connection to MQTT broker
  Ethernet.begin(mac, ip);
  if (client.connect("arduino-ip-238", "xxxxxx", "xxxxx")) {  							// change as desired - clientname must be unique for MQTT broker
    client.publish("led","hello world - here arduino ip 239");
    Serial.println("connected");
    client.subscribe("led");										// subscribe to topic "led"
  }
}

//----------------------------------------------

void loop()
{

// Listen for button interactions and take actions ----------------------------------------  
// Note: Button actions do send MQTT message AND do set led(x)Value to HIGH or LOW

  if (bouncer1.update()) {
    if (bouncer1.read() == HIGH) {
      if (led1Value == LOW) {
        led1Value = HIGH;
        client.publish("led","1on");								
      } else {
        led1Value = LOW;
        client.publish("led","1off");
      }
    }
  }  

//-----------------------------------------------
  
  if (bouncer2.update()) {
    if (bouncer2.read() == HIGH) {
      if (led2Value == LOW) {
        led2Value = HIGH;
        client.publish("led","2on");
      } else {
        led2Value = LOW;
        client.publish("led","2off");
      }
    }
  }  
  
//------------------------------------------------  

  if (bouncer3.update()) {
    if (bouncer3.read() == HIGH) {
      if (led3Value == LOW) {
        led3Value = HIGH;
        client.publish("led","3on");
      } else {
        led3Value = LOW;
        client.publish("led","3off");
      }
    }
  }  

//-----------------------------------------------
  
  if (bouncer4.update()) {
    if (bouncer4.read() == HIGH) {
      if (led4Value == LOW) {
        led4Value = HIGH;
        client.publish("led","4on");
      } else {
        led4Value = LOW;
        client.publish("led","4off");
      }
    }
  }  
  
//------------------------------------------------  
  
  client.loop();
}

// End of sketch ---------------------------------

I hope someone can help me wit a correct working code or point me in the good direction.

I would just work on a short program looking at getting the button working , then move on . You’ll find that much easier to trace your problem.

Look at “debounce”; “momentary switch “ plus Arduino with Mr Google.

Take a look at the and .fell() and .rose() functions of Bounce2 to pick up the press or release of a button.

cattledog, i have been looking in to that the past days but i can not find how i need to adjust them to let the code act like a momentary push button. I must say that i am really not that good in understanding the code. I mostly use samples and tweak them a little to my needs.

Your buttons appear to be wired for INPUT_PULLUP, and will read LOW when pressed.

pinMode(button1,INPUT);
  digitalWrite(button1,HIGH);
//same as (pinMode(button1,INPUT_PULLUP);

But this still isn't working for me because it only activates a switch when you press an release.
For a doorbell and a drive through sensor this isn't working well.

Please explain what you want to happen when the button becomes pressed, when the button becomes released, and when the button is stable HIGH.

I don't think this does what you want, but shows how to use .rose() and . fell()

if (bouncer1.update()) {
   // if (bouncer1.read() == HIGH) {
   if (bouncer1.fell()) { //button pressed, state change to LOW
      if (led1Value == LOW) 
      {
        led1Value = HIGH;
        client.publish("led","1on");                
      } 
      else if(debouncer1.rose()) //button released
      {
        led1Value = LOW;
        client.publish("led","1off");
      }
    }
  }

I want the button to act as a real push button.
When the button is pressed(or a connected reed contact is activated) the value should become high(led on).
And stay high as long as the button is pressed.
When not pressed it should be low(led off).

But I think if I read it correct, your example would do this.

The code I started this post with acts as follows.
Starting state, led is off, button reads low.
When the button is pressed it reads high, but led stays off.
Only when I let go of the button and it reads low again, the led changes to on.

When I do the above again, then the led changes to off again.

The fact that it changes the led state only on releasing the button makes this useless for my application.
If the state would change as soon as the button is pressed(value goes from low to high) it would already be useful to me.(because I can make an automation in HomeAssistant to turn of the led again)

I want to use this unit to monitor the following.
Door bell pressed.
Gate open state(reed contact)
Gate closed state(reed contact)
Drive through detection with IR detector( normally closed or normally open contact)
The actions that need to be taken on the different functions will be processed by HomeAssistant over MQTT. I need it to be wired because the gate is to far from home and wired is more stable.

When the button is pressed(or a connected reed contact is activated) the value should become high(led on).
And stay high as long as the button is pressed.
When not pressed it should be low(led off).

The first thing to sort out is how the buttons (switches) are wired and how they behave. When do they digitalRead LOW, and when do they digitalRead HIGH. This button behaviour is independent of the LED on/off.

Your code activates the internal pullup resistor to 5v. Typically you would wire the button(switch) so that when pressed (contact is made) it connects the input pin to ground.
The pullup keeps the input pin from floating when not pressed. In this mode, the digitalRead of the input pin will be HIGH when not pressed, and LOW when pressed.

I believe that your original code is not working as you wish, because you have this behavior backwards.

Can you confirm the button behavior, and then we can work the code to work with the transitions. This will also help to send only one message, and not keep sending them as long as the button is held down.

I am not sure about when the value is high or low.
But i think it is indeed as you mentioned.
When the button is pressed and makes contact to ground the value will read low.
Depressed, no contact the value will be high.

That explains why the led changes state only when i let go of the button.
That is when the value goes from low to high.

In the following film i show how the button works.

button behaviour

The first part is when i have an automation in home assistant on, that turns of the led after it is on for one second.

The middle part is with the automation off.
I also show how i can switch the led on with MQTT in home assistant.

The last part is with the automation back on.

You can clearly see that the led changes only when i let go of the button, not when i press and hold it.

Doing this i found out that if i would use a normally closed button or sensor, than this whole thing would probably work without modifying the code.

But i don't think this is the right way to solve this.

When the button is pressed and makes contact to ground the value will read low.
Depressed, no contact the value will be high.

Good. This is a how a button switch wired to ground from an input with INPUT_PULLUP should work.

When the button is pressed(or a connected reed contact is activated) the value should become high(led on).
And stay high as long as the button is pressed.
When not pressed it should be low(led off).

But I think if I read it correct, your example would do this.

I think so as well. Did you try the code posted in Reply#4?
If it does not do what you want it to do, explain in detail how it is not acting correctly.

Nope, haven't tried it yet.
I will try it this weekend and let you know how this works.

Finally had some time to play with this.

But it does not function like it should.

If I replace

  if (bouncer2.update()) {
    if (bouncer2.read() == HIGH) {
      if (led2Value == LOW) {
        led2Value = HIGH;
        client.publish("led","2on");
      } else {
        led2Value = LOW;
        client.publish("led","2off");
      }
    }
  }

with your code

if (bouncer1.update()) {
   // if (bouncer1.read() == HIGH) {
   if (bouncer1.fell()) { //button pressed, state change to LOW
      if (led1Value == LOW) 
      {
        led1Value = HIGH;
        client.publish("led","1on");                
      } 
      else if(debouncer1.rose()) //button released
      {
        led1Value = LOW;
        client.publish("led","1off");
      }
    }
  }

Then I get the following error

MQTT-ethernet-node-4-buttons-4-leds:196:16: error: 'debouncer1' was not declared in this scope

  • else if (debouncer1.rose()) //button released*
    exit status 1
    'debouncer1' was not declared in this scope

I added the following line
#include <Debouncer.h>

But it did not help.

If I change
else if(debouncer1.rose()) //button released

To
else if(bouncer1.rose()) //button released

Then the code compiles.

Now the button acts as follows.

When I press the button the led turns on instantly.
When I let the button go, the led stays on.
When I press the button again, nothing happens and the led stays on.

If I change
else if(debouncer1.rose()) //button released
To
else if(bouncer1.rose()) //button released
Then the code compiles.

Sorry for the typo :frowning: You got it right.

There was a problem with my nested logic. Here is an example using a switch, an led, and Serial output. The led goes on when the switch is pressed and stays on as long as it is held down. When released, it goes off. Is this the behavior you want?

#include <Bounce2.h>
const byte button1 = 7;
const byte led1 = 2;
byte led1Value = LOW;
Bounce bouncer1 = Bounce();

void setup() {
  Serial.begin(115200);
  pinMode(led1, OUTPUT);
  pinMode(button1, INPUT_PULLUP);
  //digitalWrite(button1, HIGH);//same as INPUT_PULLUP
  bouncer1.attach(button1);
  bouncer1.interval(25);
}

void loop() {
  if (bouncer1.update())
  {
    if (bouncer1.fell()) //button pressed, state change to LOW
    {
      led1Value = HIGH;
      Serial.println("LED1 ON");
      digitalWrite(led1, led1Value);
    }   
    if (bouncer1.rose()) //button released
    {
      led1Value = LOW;
      Serial.println("LED1 OFF");
      digitalWrite(led1, led1Value);
    }
  }
}

This is exactly how I need this to work.

Now I have to try and implement the Ethernet Shield with the MQTT.

I cant figure out why my converted code in which I added the ethernet/mqtt stuff isn't working.

Your plain example works fine.

But in my code the led does turn on the moment i press the button but it stays on no mater what I do.

The only way to turn it off is by doing it over an mqtt message.
So it must have something tot do with that code i added, but i can't seem to find a solution.
Below is what I have. (first I am focusing on 1 button/led)

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <Bounce2.h>

// Set led variables to Arduino digital pins
const byte led1 = 2;

// Set button variables to Arduino digital pins
const byte button1 = 7;

// Set variable values initially to LOW (and not HIGH)
byte led1Value = LOW;            

//---------------------------------------------------------------------------

// Arduino MAC address is on a sticker on your Ethernet shield
// must be unique for every node in same network
// To make a new unique address change last letter

byte mac[]    = { *, *, *, *, *, * };  

// Unique static IP address of this Arduino - change to adapt to your network
IPAddress ip(*, *, *, *);

// IP Address of your MQTT broker - change to adapt to your network
IPAddress server (*, *, *, *);

// Handle and convert incoming MQTT messages ----------------------------------------
void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived
  String content="";
  char character;
  for (int num=0;num<length;num++) {
      character = payload[num];
      content.concat(character);
  }   
  Serial.println(topic);
  Serial.println(content); // message sent out by button actions is returned from broker and serial printed

// Set specific virtual switches on basis of specific incoming messages ----------------------------
    if (content == "1on") {
    led1Value = HIGH;
  }
  
  if (content == "1off") {
    led1Value = LOW;
  }
    
  // Set digital pin states according to virtual switch settings   
  digitalWrite(led1,led1Value);
}

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

// Initiate a bouncer instance for each button
Bounce bouncer1 = Bounce();


//-------------------------------------------------------
void setup()
{ // setup led, button, bouncer 1 -----------------------
  pinMode(led1, OUTPUT);
  pinMode(button1,INPUT_PULLUP);
  //digitalWrite(button1,HIGH);
  bouncer1.attach(button1);
  bouncer1.interval(25);

  // Setup serial connection
  Serial.begin(9600);

  // Setup ethernet connection to MQTT broker
  Ethernet.begin(mac);
  if (client.connect("arduino-ip-238", "*****", "*****")) {  	// change as desired - clientname must be unique for MQTT broker
    client.publish("led","hello world - here arduino ip 239");
    Serial.println("connected");
    client.subscribe("led");	// subscribe to topic "led"
  }
}
//----------------------------------------------
void loop()
{
// Listen for button interactions and take actions ----------------------------------------  
// Note: Button actions do send MQTT message AND do set led(x)Value to HIGH or LOW
if (bouncer1.update()) 
{
    if (bouncer1.fell()) { //button pressed, state change to LOW
      {
        led1Value = HIGH;
        //Serial.println("LED1 ON");
        //digitalWrite(led1, led1Value);
        client.publish("led","1on");                
      } 
      if (bouncer1.rose()) //button released
      {
        led1Value = LOW;
        //Serial.println("LED1 OFF");
        //digitalWrite(led1, led1Value);
        client.publish("led","1off");
      }
    }
  } 
//------------------------------------------------  
  client.loop();
}
// End of sketch ---------------------------------

I tried changing lots off things but nothing seems to point me in the right direction.

For instance, if I change "const byte" in to "int" --- Nothing changes.

If I comment out
Serial.println(topic);

  • Serial.println(content); // message sent out by button actions is returned from broker and serial printed*

  • digitalWrite(led1,led1Value);*

  • Serial.println("LED1 ON");*

  • digitalWrite(led1, led1Value);*

  • Serial.println("LED1 OFF");*

  • digitalWrite(led1, led1Value);*

Nothing changes.

If I interchange Values like "LOW"and "HIGH"
And the "FELL"and "ROSE"

It does change the results, sometimes it doesn't work at all, others change how the led reacts.
But no change I did gave me a clue that I am going in the right direction.

It doesn't look that difficult, but i can't seem to find the solution.

I got it partially working

I am sorry for another reply from me but I think this is the best way to explain my steps.

In this section of code

// Listen for button interactions and take actions ----------------------------------------  
// Note: Button actions do send MQTT message AND do set led(x)Value to HIGH or LOW
if (bouncer1.update()) 
{
    if (bouncer1.fell()) { //button pressed, state change to LOW
      {
        led1Value = HIGH;
        Serial.println("LED1 ON");
        digitalWrite(led1, led1Value);
        client.publish("led","1on");                
      } 
    }      else if (bouncer1.rose()) //button released
      {
        led1Value = LOW;
        Serial.println("LED1 OFF");
        digitalWrite(led1, led1Value);
        client.publish("led","1off");
      }
    }
  } 
//------------------------------------------------  
//  client.loop();
//}
// End of sketch ---------------------------------

I added the " } else ", but that gave me an error on the "client loop" called.
exit status 1
'client' does not name a type

So I commented that out.
Now the button acts like in your example, and I get MQTT messages.(i cnat toggle the button using Home Assistant)
But after 30 sec or so, the MQTT stops sending messages.
After a reboot of the Arduino, it works fine for a moment and then stops again.
If i keep pressing the button every few seconds MQTT keeps working.
As soon as I wait for 30 seconds or more, it stops working.
I am sure it has something to do with a loop.

Cattledog, thanks for the help this far, I really appreciate that.
I hope you can also help me with the last piece.

This first video shows the system working, also in Home Assistant.

Button functioning with MQTT

And in this one it stopped working.

MQTT stopped

UPDATE:
So finally made it.

The addition of "else " and a bunch of these " } { " did the trick.

Maybe the code still isn't exactly right, but it works(for now)

For people who may want to us this, below the code that works on an Arduino Uno with Ethernet Shield and MQTT and Home Assistant.

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <Bounce2.h>

// Set led variables to Arduino digital pins
const byte led1 = 2;

// Set button variables to Arduino digital pins
const byte button1 = 7;

// Set variable values initially to LOW (and not HIGH)
byte led1Value = LOW;            

//---------------------------------------------------------------------------

// Arduino MAC address is on a sticker on your Ethernet shield
// must be unique for every node in same network
// To make a new unique address change last letter

byte mac[]    = { *, *, *, *, *, * };  

// Unique static IP address of this Arduino - change to adapt to your network
IPAddress ip(*, *, *, *);

// IP Address of your MQTT broker - change to adapt to your network
IPAddress server (*, *, *, *);

// Handle and convert incoming MQTT messages ----------------------------------------
void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived
  String content="";
  char character;
  for (int num=0;num<length;num++) {
      character = payload[num];
      content.concat(character);
  }   
  Serial.println(topic);
  Serial.println(content); // message sent out by button actions is returned from broker and serial printed

// Set specific virtual switches on basis of specific incoming messages ----------------------------
    if (content == "1on") {
    led1Value = HIGH;
  }
  
  if (content == "1off") {
    led1Value = LOW;
  }
    
  // Set digital pin states according to virtual switch settings   
  digitalWrite(led1,led1Value);
}

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

// Initiate a bouncer instance for each button
Bounce bouncer1 = Bounce();


//-------------------------------------------------------
void setup()
{ // setup led, button, bouncer 1 -----------------------
  pinMode(led1, OUTPUT);
  pinMode(button1,INPUT_PULLUP);
  digitalWrite(button1,HIGH);
  bouncer1.attach(button1);
  bouncer1.interval(25);

  // Setup serial connection
  Serial.begin(9600);

  // Setup ethernet connection to MQTT broker
  Ethernet.begin(mac);
  if (client.connect("arduino-ip-238", "*****", "*****")) {   // change as desired - clientname must be unique for MQTT broker
    client.publish("led","hello world - here arduino ip 239");
    Serial.println("connected");
    client.subscribe("led"); // subscribe to topic "led"
  }
}
//----------------------------------------------
void loop()
{
// Listen for button interactions and take actions ----------------------------------------  
// Note: Button actions do send MQTT message AND do set led(x)Value to HIGH or LOW
if (bouncer1.update()) 
{
    if (bouncer1.fell()) { //button pressed, state change to LOW
      {
        led1Value = HIGH;
        Serial.println("LED1 ON");
        digitalWrite(led1, led1Value);
        client.publish("led","1on");                
      } 
    }   else if (bouncer1.rose()) { //button released
      {
        led1Value = LOW;
        Serial.println("LED1 OFF");
        digitalWrite(led1, led1Value);
        client.publish("led","1off");
      }
    }
  } 
//------------------------------------------------  
client.loop();
}
// End of sketch ---------------------------------

With many thanks to Cattledog.

Now on to making this work for 3 more buttons.

I have no experience with the MQTT. I took a quick look at PubSubClient.h and I would think that client.loop() may be required.

I think your brackets are a little messed up. Try this loop() code. I can't test the MQTT stuff.

// Listen for button interactions and take actions ----------------------------------------
// Note: Button actions do send MQTT message AND do set led(x)Value to HIGH or LOW

void loop()
{
  if (bouncer1.update())
  {
    if (bouncer1.fell()) //button pressed, state change to LOW
    {
      led1Value = HIGH;
      Serial.println("LED1 ON");
      digitalWrite(led1, led1Value);
      client.publish("led", "1on");
    }
    else if (bouncer1.rose()) //button released
    {
      led1Value = LOW;
      Serial.println("LED1 OFF");
      digitalWrite(led1, led1Value);
      client.publish("led", "1off");
    }
  }
  client.loop();
}
// End of sketch ---------------------------------

EDIT: I did not see your edit to the last post when I posted this. You still have extra brackets, but they are not making a difference. The key is that you put client.loop() back in the code.

That is indeed correct and works like it should.

Now I am adding the second button but i am not sure if it is even possible to have more then one INPUT_PULLUP ?

Because if I duplicate all the necessary lines, the program doesn't run anymore.

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <Bounce2.h>

// Set led variables to Arduino digital pins
const byte led1 = 2;
const byte led2 = 3;

// Set button variables to Arduino digital pins
const byte button1 = 7;
const byte button2 = 8;

// Set variable values initially to LOW (and not HIGH)
byte led1Value = LOW; 
byte led2Value = LOW;           

//---------------------------------------------------------------------------

// Arduino MAC address is on a sticker on your Ethernet shield
// must be unique for every node in same network
// To make a new unique address change last letter

byte mac[]    = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE };  

// Unique static IP address of this Arduino - change to adapt to your network
IPAddress ip(10, 0, 0, 85);

// IP Address of your MQTT broker - change to adapt to your network
IPAddress server (10, 0, 0, 47);

// Handle and convert incoming MQTT messages ----------------------------------------
void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived
  String content="";
  char character;
  for (int num=0;num<length;num++) {
      character = payload[num];
      content.concat(character);
  }   
  Serial.println(topic);
  Serial.println(content); // message sent out by button actions is returned from broker and serial printed

// Set specific virtual switches on basis of specific incoming messages ----------------------------
    if (content == "1on") {
    led1Value = HIGH;
  }
  
  if (content == "1off") {
    led1Value = LOW;
  }
  if (content == "2on") {
    led2Value = HIGH;
  }
  
  if (content == "2off") {
    led2Value = LOW;
  }
    
  // Set digital pin states according to virtual switch settings   
  digitalWrite(led1,led1Value);
  digitalWrite(led2,led2Value);
}

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

// Initiate a bouncer instance for each button
Bounce bouncer1 = Bounce();
Bounce bouncer2 = Bounce();


//-------------------------------------------------------
void setup()
{ // setup led, button, bouncer 1 -----------------------
  pinMode(led1, OUTPUT);
  pinMode(button1,INPUT_PULLUP);
  digitalWrite(button1,HIGH);
  bouncer1.attach(button1);
  bouncer1.interval(25);
  
 // setup led, button, bouncer 2 -----------------------
  //pinMode(led2, OUTPUT);
  //pinMode(button2,INPUT_PULLUP);
  //digitalWrite(button2,HIGH);
  //bouncer2.attach(button2);
  //bouncer2.interval(25);

  // Setup serial connection
  Serial.begin(9600);

  // Setup ethernet connection to MQTT broker
  Ethernet.begin(mac);
  if (client.connect("arduino-ip-238", "arnold", "sandra")) {    // change as desired - clientname must be unique for MQTT broker
    client.publish("led","hello world - here arduino ip 239");
    Serial.println("connected");
    client.subscribe("led");  // subscribe to topic "led"
  }
}
//----------------------------------------------
// Listen for button interactions and take actions ----------------------------------------  
// Note: Button actions do send MQTT message AND do set led(x)Value to HIGH or LOW
void loop()
{
  if (bouncer1.update()) 
  {
    if (bouncer1.fell())  //button pressed, state change to LOW
      {
        led1Value = HIGH;
        Serial.println("LED1 ON");
        digitalWrite(led1, led1Value);
        client.publish("led","1on");                
      } 
     else if (bouncer1.rose()) //button released
      {
        led1Value = LOW;
        Serial.println("LED1 OFF");
        digitalWrite(led1, led1Value);
        client.publish("led","1off");
      }
    }
//----------------------------------------------
  if (bouncer2.update()) 
  {
    if (bouncer2.fell())  //button pressed, state change to LOW
      {
        led2Value = HIGH;
        Serial.println("LED2 ON");
        digitalWrite(led2, led2Value);
        client.publish("led","2on");                
      } 
     else if (bouncer2.rose()) //button released
      {
        led1Value = LOW;
        Serial.println("LED2 OFF");
        digitalWrite(led2, led2Value);
        client.publish("led","2off");
      }
    }
      
  client.loop();
}
// End of sketch ---------------------------------

I commented this section out because otherwise i could not get it to compile.

 // setup led, button, bouncer 2 -----------------------
  //pinMode(led2, OUTPUT);
  //pinMode(button2,INPUT_PULLUP);
  //digitalWrite(button2,HIGH);
  //bouncer2.attach(button2);
  //bouncer2.interval(25);

I am stuck again.

Now I am adding the second button but i am not sure if it is even possible to have more then one INPUT_PULLUP ?

No problem. With INPUT_PULLUP you can remove this line. It's an older way of doing things before INPUT_PULLUP was implemented.

//digitalWrite(buttonx,HIGH);

I commented this section out because otherwise i could not get it to compile.

Your code compiles for me with the second button setup comments removed.

// setup led, button, bouncer 2 -----------------------
  pinMode(led2, OUTPUT);
  pinMode(button2,INPUT_PULLUP);
  //digitalWrite(button2,HIGH);
  bouncer2.attach(button2);
  bouncer2.interval(25);

Because if I duplicate all the necessary lines, the program doesn't run anymore.

There is an error in your code.

else if (bouncer2.rose()) //button released
      {
        //led1Value = LOW;
          led2Value = LOW;
        Serial.println("LED2 OFF");
        digitalWrite(led2, led2Value);
        client.publish("led","2off");
      }

With this correction, I can see both buttons working as intended.

This is strange...

I corrected the code which i still had in Arduino ide.
It does not compile.(gives a strange error)

I selected the code I added in this topic and put that in a new program sheet of Arduino ide.
Corrected my fault and commented out the 2 lines you suggested.

And now it compiles fine and after uploading, it works fine for both buttons as it did for one.

Man I hate why I didn't get this second button to work on my own, but I was close.

On to number 3 and 4.

And then on to real life testing.