Arduino, MQTT and OpenHab led switch sketch

Hello, i am new here and i am new to arduino / mqtt and openhab.

I have an Arduino Mega with ethernet (ENC28J60), 1 x DHT 11 and a led strip attached on pin PWM 4. I found this sketch on internet that works fine for the DHT11, i already have the openhab + mqtt setup working.

The problem is with the led strip. When i start the arduino the led strip just goes on and off until the arduino crashes. I am not a programmer, i understand very little from the arduino sketch. I think that the problem are the push buttons (i do’nt need any push button but the sketch that i found already had this).

Is there any one out there who will be willing to share his sketch for a on / off switch (with dimming if possible) including mqtt ?

Here is the sketch that i found and works for my DHT11 but not for the led switch:

#include <SPI.h>
//#include <Ethernet.h>
#include <UIPEthernet.h>
#include <PubSubClient.h>
#include <DHT.h>
const int butt1 = 3;// the pin that the pushbutton is attached to
const int butt2 = 2;
const int ledPin1 = 5;       
const int ledPin2 = 4;
int ledState1 = HIGH;         
int buttonState1;             
int lastButtonState1 = LOW;   
int ledState2 = HIGH;         
int buttonState2;             
int lastButtonState2 = LOW; 
long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 60000; // READING INTERVAL
int t = 0;  // TEMPERATURE VAR
int h = 0;  // HUMIDITY VAR
#define DHTPIN 24 // SENSOR PIN
#define DHTTYPE DHT11 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS
DHT dht(DHTPIN, DHTTYPE);
// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xEF };
IPAddress ip(192, 168, 1, 103);
IPAddress server(192, 168, 1, 210);

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

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

void callback(char* topic, byte* payload, unsigned int length) {
Serial.println();
Serial.println("Callback");
Serial.print("Topic = ");
Serial.println(topic);
Serial.print("Payload = ");
for (int i=0;i<length;i++){
Serial.print((char)payload[i]);
}
Serial.println();
 if (strcmp(topic,"/arduino/l1/com")==0) { 
   if (payload[0] == 'OFF') 
    {
     digitalWrite(ledPin1, LOW);
     delay(100);
    client.publish("/arduino/l1/state","OFF");
    } 
    else if (payload[0] == 'ON') 
    {
    digitalWrite(ledPin1, HIGH);
        delay(100);
    client.publish("/arduino/l1/state","ON");
    }
   } 

  if (strcmp(topic,"/arduino/l2/com")==0) { 
   if (payload[0] == 'OFF') 
    {
    digitalWrite(ledPin2, LOW);
        delay(100);
    client.publish("/arduino/l2/state","OFF");
    } 
    else if (payload[0] == 'ON') 
    {
    digitalWrite(ledPin2, HIGH);
     delay(100);
    client.publish("/arduino/l2/state","ON");
    }
   }                
} 

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("arduinoClient")) {
      Serial.println("connected");
      client.subscribe("/arduino/#");
      client.publish("conn","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 push1() {
int reading1 = digitalRead(butt1);
buttonState1 = reading1;
 if (buttonState1 == HIGH) {
   ledState1 = !ledState1;
          if (ledState1 < 1)
          {
            digitalWrite(ledPin1, LOW);
            delay(100);
            client.publish("/arduino/l1/com","OFF");
            client.publish("/arduino/l1/state","OFF");
          }
          else
          {
            digitalWrite(ledPin1, HIGH);
            delay(100);
            client.publish("/arduino/l1/com","ON");
            client.publish("/arduino/l1/state","ON");
          }
        }
      }
void push2() {
int reading2 = digitalRead(butt2);
buttonState2 = reading2;
 if (buttonState2 == HIGH) {
   ledState2 = !ledState2;
          if (ledState2 < 1)
          {
            digitalWrite(ledPin2, LOW);
            delay(100);
            client.publish("/arduino/l2/com","OFF");
            client.publish("/arduino/l2/state","OFF");
          }
          else
          {
            digitalWrite(ledPin2, HIGH);
            delay(100);
            client.publish("/arduino/l2/com","ON");
            client.publish("/arduino/l2/state","ON");
          }
        }
    }
    void temp() {
  h = (int)dht.readHumidity();
  t = (int)dht.readTemperature();
  char temp[2];
  char hum[3];
  sprintf(hum, "%d", h);
  sprintf(temp, "%d", t);
  client.publish("/arduino/temp/state", temp);
  client.publish("/arduino/hum/state", hum);
}

void setup() {
  // put your setup code here, to run once:
  pinMode(butt1, INPUT);
  pinMode(butt2, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  pinMode(26, OUTPUT);      // sets the digital pin as output
  pinMode(22, OUTPUT);      // sets the digital pin as output
  digitalWrite(26, HIGH);   // sets +5v for the sensor
  digitalWrite(22, LOW);    // sets gnd for the sensor
  h = (int)dht.readHumidity();
  t = (int) dht.readTemperature();
    if (client.connect("arduinoClient")) {
  client.publish("conn","Connected");
  client.subscribe("/arduino/#");
   }
}

void loop() {
  // put your main code here, to run repeatedly:
 if (!client.connected()) {
    reconnect();
 }
  currentMillis = millis();
  if (currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
    previousMillis = currentMillis;
    temp();
  }
 int reading1 = digitalRead(butt1);
 int reading2 = digitalRead(butt2);
 if (reading1 != buttonState1) {
  push1();
 }
 if (reading2 != buttonState2){
  push2();
 }
 client.loop();
}

You might be better off asking this question at the Arduino forum: http://forum.arduino.cc/

I agree with Max. Please continue in the Arduino forum.

My first guess is, that you are not supplying enough power for the LED strip. This leads to a voltage drop followed by a reboot of the Arduino. Be aware that an error in your circuit can easily destroy your hardware.

After you made sure your circuit is correct, go through the functionality of your sketch step by step by printing out some data by the help of Serial.println(...). This will help you understand at which step your sketch fails.

Good luck! :wink: