Using mqtt from ESP to Openhab2

Of course. This is my home.items:


//Temperature Sensors
Number Temperature "Temperature [%.1f °C]" {mqtt="<[broker:sensor/temperature:state:default]"}
Number Humidity "Humidity [%.1f%%]" {mqtt="<[broker:sensor/humidity:state:default]"}

Switch Relay "Channel 1" {mqtt=">[broker:sensor/relay:command:ON:on],>[broker:sensor/relay:command:OFF:off]"}

Im trying to make the HomeKit binding to work, but everytime I add [ “Lighting” ] , the switch stop working. I don´t know if its possible to use HomeKit on devices connected to an MQTT network. If anybody knows something please let me know. Thanks.

I have the same problem. I did what they did, but I could not get it working. Where could I have done wrong please help …

openhabtemp

home.items
Number Oda “Temperature [%.1f °C]” {mqtt="<[broker:sensor/temperature:state:default]" }

home.sitemap
Text item=Oda icon=“temperature”

mqttcfg

This is the complete code:

//Import Libraries
#include <ESP8266WiFi.h>
#include <DHT.h>
#include <PubSubClient.h>

// DHT22 Temperature chip i/o
#define DHTPIN 2 //NodeMCU pin D4
#define DHTTYPE DHT22
DHT dht(DHTPIN,DHTTYPE);
char tmp[50];
char hum[50];
int tellstate = 0;

int RelayPin = 5; //Set Relay to be connected to digital pin 5 (NodeMCU pin D1)

//Wifi/Broker parameters
const char* ssid = "HayBolat"; //Wifi network SSID
const char* password = "Efe040605"; //Wifi network PASSWORD
const char* mqtt_server = "192.168.100.155"; //Broker IP Address

//MQTT Configuration
WiFiClient espClient; //Creates a partially initialised client instance.
PubSubClient client(espClient); //Before it can be used, the server details must be configured

void connect_to_MQTT() {
 client.setServer(mqtt_server, 1883);//Set the MQTT server details
  client.setCallback(callback);

  if (client.connect("temperature_sensor_relay")) {
    Serial.println("Connected to MQTT Server");
    client.subscribe("sensor/relay");
  } else {
    Serial.println("Could not connect to MQTT");
  }
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print(topic);
  Serial.print(" => ");

char* payload_str;
  payload_str = (char*) malloc(length + 1);
  memcpy(payload_str, payload, length);
  payload_str[length] = '\0';
Serial.println(String(payload_str));
  
  if ( String(topic) == "sensor/relay" ) {
    if (String(payload_str) == "on" ) {
      digitalWrite(RelayPin, HIGH);   // turn the RELAY on
      client.publish("sensor/relay_state","on");
    } else if ( String(payload_str) == "off" ) {
      digitalWrite(RelayPin, LOW);    // turn the RELAY off
      client.publish("sensor/relay_state","off");
    } else {
      Serial.print("I cannot process ");
      Serial.print(String(payload_str));
      Serial.print(" on topic ");
      Serial.println( String(topic));
    }
  }
}

void setup() {
  Serial.begin(115200); //Sets the data rate in bits per second (baud) for serial data transmission.

// Connecting to our WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // initialize pin 5, where the relay is connected to.
  pinMode(RelayPin, OUTPUT);

  connect_to_MQTT();
}

void getTemperature() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  //Temp as string
  itoa(t,tmp,10);
  client.publish("sensor/temperature",tmp);
  Serial.println(tmp);

  //Humidity as string
  itoa(h,hum,10);
  client.publish("sensor/humidity",hum);
  Serial.println(hum);
}

void getVoltage() {
  int iVcc = ESP.getVcc();
  float fVcc = (float)ESP.getVcc() / 1000;
  char cVcc[5];
  dtostrf(fVcc,5, 3, cVcc);
  
  client.publish("sensor/voltage",cVcc);
  Serial.println(cVcc);
}
  
void loop() {
  client.loop();

  if (! client.connected()) {
    Serial.println("Not connected to MQTT....");
    connect_to_MQTT();
  }

 //Every 60 seconds read the temperature, humidity and relay state
  if ( (millis() - tellstate) > 60000 ) {
    getTemperature();
    getVoltage();
    if ( digitalRead(RelayPin) ) {
       client.publish("sensor/relay_state","on");
    } else {
      client.publish("sensor/relay_state","off");
    }
    tellstate = millis();
  }
}

The topic in your lua script does not match the topic in your items definition.

I corrected the wrong code loading here. I can read the Mqttfix temperature data.

Where am I doing wrong?

Can’t find something obvious, sorry.
Your temperature comes in without decimals, so I would try a label without decimals: “Temperature [%.0f °C]”

Thank you. I did what you said, but it did not happen again. :frowning:

Go through this step by step tutorial:

I checked everything. showing down in the picture. I can not just show OH2.

It does look like you’ve got your ESP and mqtt broker running OK, and I can’t see any immediate or obvious issues with your .items file or .sitemap - but, please in future, put these into code fences, like this (or by using one of the buttons in the toolbar above):

```
Your text here
```

Let’s also see your openhab.log file - please copy/paste into code fences a snippet of the log covering a time period showing the startup of openHAB, and you receiving at least 1 message from your ESP into mqtt.fx

Hopefully in this log file there will be something that will give either you and/or us an idea of what’s going on. These logs are important to troubleshooting.

I apologize very much, I am in this regard. I do not understand exactly what to do. Do you explain to me in detail?

You are trying to ask us to troubleshoot a very complex piece of software, by providing us with only a couple of screenshots, and some copy & pasted code, which, due to not using the ‘code fences’, has been passed through a processor which may have amended or stripped out important errors in your code.

You have used the ‘code fences’ option for when you posted your ESP code, so you know how to do it already - please just apply it to anything that has anything to do with your openhab configuration :slight_smile:

Please show us the contents of the log file. I would like you to restart openhab (or restart the computer it is running on) so that we can see all of the entries in the log for it starting up, trying to set up all the bindings etc, and hopefully this will highlight an error that you can easily correct! But without log files, it is going to be very difficult.

2018-01-26 20:49:42.547 [vent.ItemStateChangedEvent] - Date changed from 2018-01-26T20:49:27.493+0300 to 2018-01-26T20:49:42.499+0300
2018-01-26 20:49:57.514 [vent.ItemStateChangedEvent] - Date changed from 2018-01-26T20:49:42.499+0300 to 2018-01-26T20:49:57.503+0300
==> /var/log/openhab2/openhab.log <==
2018-01-26 20:50:05.144 [INFO ] [clipse.smarthome.model.script.Keller] - Temperatur: python: can’t open file ‘/home/opnehabian/AdafruitDHT.py’: [Errno 2] No such file or directory°C, Feuchte:
2018-01-26 20:50:11.888 [ERROR] [hab.binding.exec.handler.ExecHandler] - An exception occurred while executing ‘http://localhost:8091/sicaklik’ : ‘Cannot run program “http://localhost:8091/sicaklik”: error=2, No such file or directory’
==> /var/log/openhab2/events.log <==
2018-01-26 20:50:12.516 [vent.ItemStateChangedEvent] - Date changed from 2018-01-26T20:49:57.503+0300 to 2018-01-26T20:50:12.506+0300
2018-01-26 20:50:27.520 [vent.ItemStateChangedEvent] - Date changed from 2018-01-26T20:50:12.506+0300 to 2018-01-26T20:50:27.509+0300
2018-01-26 20:50:39.263 [vent.ItemStateChangedEvent] - Deneme2_LastExecution changed from 2018-01-26T20:47:38.311+0300 to 2018-01-26T20:50:39.251+0300
2018-01-26 20:50:42.529 [vent.ItemStateChangedEvent] - Date changed from 2018-01-26T20:50:27.509+0300 to 2018-01-26T20:50:42.518+0300
2018-01-26 20:50:57.417 [vent.ItemStateChangedEvent] - Nem changed from 41.20 to 41.30
2018-01-26 20:50:57.429 [vent.ItemStateChangedEvent] - Rutubet_LastExecution changed from 2018-01-26T20:47:26.248+0300 to 2018-01-26T20:50:57.417+0300
2018-01-26 20:50:57.536 [vent.ItemStateChangedEvent] - Date changed from 2018-01-26T20:50:42.518+0300 to 2018-01-26T20:50:57.521+0300
==> /var/log/openhab2/openhab.log <==
2018-01-26 20:51:11.907 [ERROR] [hab.binding.exec.handler.ExecHandler] - An exception occurred while executing ‘http://localhost:8091/sicaklik’ : ‘Cannot run program “http://localhost:8091/sicaklik”: error=2, No such file or directory’
==> /var/log/openhab2/events.log <==
2018-01-26 20:51:12.980 [vent.ItemStateChangedEvent] - Date changed from 2018-01-26T20:50:57.521+0300 to 2018-01-26T20:51:12.967+0300
2018-01-26 20:51:27.999 [vent.ItemStateChangedEvent] - Date changed from 2018-01-26T20:51:12.967+0300 to 2018-01-26T20:51:27.985+0300
2018-01-26 20:51:43.012 [vent.ItemStateChangedEvent] - Date changed from 2018-01-26T20:51:27.985+0300 to 2018-01-26T20:51:42.988+0300
2018-01-26 20:51:58.004 [vent.ItemStateChangedEvent] - Date changed from 2018-01-26T20:51:42.988+0300 to 2018-01-26T20:51:57.994+0300
==> /var/log/openhab2/openhab.log <==
2018-01-26 20:52:11.920 [ERROR] [hab.binding.exec.handler.ExecHandler] - An exception occurred while executing ‘http://localhost:8091/sicaklik’ : ‘Cannot run program “http://localhost:8091/sicaklik”: error=2, No such file or directory’
==> /var/log/openhab2/events.log <==
2018-01-26 20:52:13.033 [vent.ItemStateChangedEvent] - Date changed from 2018-01-26T20:51:57.994+0300 to 2018-01-26T20:52:12.998+0300
2018-01-26 20:52:28.029 [vent.ItemStateChangedEvent] - Date changed from 2018-01-26T20:52:12.998+0300 to 2018-01-26T20:52:28.006+0300

Looks like a typo in the path for the python file:
opnehabian I suspect should be openhabian

my problem is not it, I have mqtt problem. There is nothing about mqtt in the log file.

From the earlier posts your setup looks quite right, so did you install the MQTT binding at all?

1 Like

Then I am out of ideas, sorry …

it is very interesting. I practiced the ensemble below.

broker.url=tcp://localhost:1883
broker.clientId=openHab
broker.qos=1

If you are playing around with the mqtt cfg file a lot you may be affected by a bug (wrong content of config could be cached):

check your /var/lib/openhab2/config/org/openhab/mqtt.config file if it has the same content as your mqtt.cfg.
If not, delete the mqtt.config and restart openHAB. The file will be recreated with the correct content and mqtt should work.

Thank you for your help.