1

I have just made an account because of this particular problem I'm having with OpenHAB. I was following a tutorial from this https://openhardwarecoza.wordpress.com/2015/03/29/openhab-mqtt-arduino-and-esp8266-part-3-hardware-arduino-with-ethernet-shield/ site but since the reply there didn't help me. I decided to go to this site.

I have successfully installed OpenHAB and use it. When I turn the switch off and on from both the HTTP page and android device, It works just fine. But when I tried to manual override using a push button on an Arduino. It didn't update the state of the switch in both of them. I'm using windows with OpenHAB version 1.71

The Openhab server notices that there is an update of the state from the push button, but the button in the HTTP page and android device didn't change at all. I have tested the connection using MQTTlens and it works just fine.

Below is my code
items configuration

Group All
Switch mqttsw1 "Switch 1" (all) {mqtt=">[mymosquitto:/arduino/l1/com:command:off:0],>[mymosquitto:/arduino/l1/com:command:on:1],<[mymosquitto:/arduino/l1/state:state:default]"}
Switch mqttsw2 "Switch 2" (all) {mqtt=">[mymosquitto:/arduino/l2/com:command:off:0],>[mymosquitto:/arduino/l2/com:command:on:1],<[mymosquitto:/arduino/l2/state:state:default]"}
Number temp "Temperature [%.1f &deg;C]" <temperature> {mqtt="<[mymosquitto:/arduino/temp/state:state:default]"}
Number hum "Humidity [%.1f &#37]" <temperature> {mqtt="<[mymosquitto:/arduino/hum/state:state:default]"}
Sitemap configuration

sitemap dolphin label="Main Menu"
{
   Frame label="Switch" {
   Switch item=mqttsw1 label="Switch 1"
   Switch item=mqttsw2 label="Switch 2"
}
   Frame label="Weather" {
   Text item=temp
   Text item=hum
} 

The Arduino Code

#include <SPI.h>
#include <Ethernet.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, 100);

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,"/esp1/l1/com")==0) { 
   if (payload[0] == '0') 
    {
     digitalWrite(ledPin1, LOW);
     delay(100);
    client.publish("/esp1/l1/state","0");
    } 
    else if (payload[0] == '1') 
    {
    digitalWrite(ledPin1, HIGH);
        delay(100);
    client.publish("/esp1/l1/state","1");
    }
   } 

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

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("/esp1/#");
      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("/esp1/l1/com","0");
            client.publish("/esp1/l1/state","0");
          }
          else
          {
            digitalWrite(ledPin1, HIGH);
            delay(100);
            client.publish("/esp1/l1/com","1");
            client.publish("/esp1/l1/state","1");
          }
        }
      }
void push2() {
int reading2 = digitalRead(butt2);
buttonState2 = reading2;
 if (buttonState2 == HIGH) {
   ledState2 = !ledState2;
          if (ledState2 < 1)
          {
            digitalWrite(ledPin2, LOW);
            delay(100);
            client.publish("/esp1/l2/com","0");
            client.publish("/esp1/l2/state","0");
          }
          else
          {
            digitalWrite(ledPin2, HIGH);
            delay(100);
            client.publish("/esp1/l2/com","1");
            client.publish("/esp1/l2/state","1");
          }
        }
    }
    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("/esp1/temp/state", temp);
  client.publish("/esp1/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("/esp1/#");
   }
}

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

If there are anybody who can help me I would be very grateful. Thank you for your attention !

Best Regards,

Aldi

1 Answers1

0

If I remember correctly you should post back a status of ON or OFF instead of 1 or 0.

Could you change your arduino code to publish back ON and OFF and test that? e.g.

client.publish("/esp1/l1/state","ON");
  • If you don't want to change the Arduino code you could also add a transform rule to Openhab to transform 1 and 0 to ON and OFF. – Jaun vd Walt Jan 28 '16 at 06:55