Connect ESP8266 with Openhab via MQTT

I am working on home automation system with raspberry pi, esp8266 and openhab application. Raspberry pi is working as server here. I want code of ESP8266 and openhab switch that will send command to esp8266 through raspberry pi to turning on and off lights.

Do you know tasmota firmware for esp8266?
You can flash tasmota on sonoff, NodeMCU, ESP-01,…
Regard Alex

Hi!

What exactly is your question here?
Did you already install a MQTT binding, did you already program your arduino?
There are plenty of guides for each step you have to take to your finished project :slight_smile:
I can tell you it works. I have an nodemcu publishing temp, pressure and humidity to my mqtt server that displays that values on my knx smart display-device.

if you have any specific questions in the progress i am happy to help.

That’s rude. If you know that your English is not perfect, please say so at the beginning. Otherwise we need to assume you know how to use the language. And in that case, you “want” the community to do the work for you.

Don’t expect much help. Please use Google to find out about projects first and use the openHAB documentation. Come back with a real question and not demand anything.

why?

I also want MQTT version 3 binding that gets installed with only voice commands and includes all possible combinations of external integrations with all devices (current and future!)

I want to have an easy life and to enjoy my Christmas eggnog while watching openHAB 2 with MQTTv3 automagically configuring itself!

3 Likes

Hey David Sorry for my poor English from next time i will use correct words while posting my question.

I have searched on google for code but i am not able to find code what i am looking for so i thought to post on community.

there are hundrends of examples in this forum and on the internet as well for such implementations

check: [OH2] Control ESP8266 Relay using MQTT Eventbus
(similar but not the same… you can use this as a guideline)

keep in mind that the focus of this forum is openHAB (not ESP code)
You will find many more examples of dedicated ESP forums

I will also second the recommendation to flash your ESP with some “all-in-one” framework firmware (like tasmota)

1 Like

Hi Jonas,

I have already install MQTT binding on R Pi 3. But i am not able to do program on ESP8266.

If you are having code already please share with me.

Thank you in advance.

Switch code:

Switch Switch1

{mqtt=">[mqtt:broker:/ledStatus:command:ON:1],>[mqtt:broker:/ledStatus:command:O

FF:0],<[mqtt:broker:/ledStatus:state:ON:1],<[mqtt:broker:/ledStatus:state:OFF:0"}

Thank You Dim…!!! :grinning:

1 Like

If you don’t know how to program the esp:

You need

  • Arduino IDE
  • correct libraries (check google for setting up arduino for esp8266
  • pubsub client or other MQTT libraries for arduino
  • alternatively tasmota firmware for the esp
1 Like

That’s the old MQTT binding.
Which version of the IDE are you running? Did you install the required libraries?
Here’s a nice example:
http://www.s6z.de/cms/index.php/homeautomation-homecontrol/hardwareplattformen/esp8266/113-mqtt-basic-esp8266-mqtt-example

1 Like

Thank you Sascha_Billian :slight_smile:

I have done with

  • Arduino IDE
  • correct libraries (check google for setting up arduino for esp8266
  • pubsub client or other MQTT libraries for arduino

I will now check

  • alternatively tasmota firmware for the esp

Is your code not compiling or do you just not see any messages from your broker? i use mqtt.fx to see if everything is going well.
if you want help you should be the one to post his code here :slight_smile:

I have installed 1.8.8 IDE and Yes i have installed required libraries.

Code is compiling but i am not receiving any output on serial monitor. Also my switch is not working
can you please help me.
mqtt.cfg file
############################################
mqtt:broker.url=tcp://localhost:1883

mqtt:broker.clientId=openhab

mqtt:broker.user=openhabian
mqtt:broker.pwd=openhabian

mqtt:broker.retain=retain
##############################################

switch.items :-

Switch Switch1
{mqtt=">[mqtt:broker:/switch/relay/switch:command:ON:1],>[mqtt:broker:/switch/relay/switch:command:O
FF:0],<[mqtt:broker:/switch/relay/switch:state:ON:1],<[mqtt:broker:/switch/relay/switch:state:OFF:0"}

#################################################

ESP8266 code:-

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Wifi Connection
const char* ssid = “XXXXXXXX”;
const char* password = “XXXXXXXX”;

// MQTT Server address
const char* mqtt_server = “192.168.1.114”; // Pi IP address

const char* mqttUser = “openhabian”;
const char* mqttPassword = “openhabian”;

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup() {

// prepare GPIO4

pinMode(4, OUTPUT);
digitalWrite(4, 1);

Serial.begin(9600);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}

void setup_wifi() {

delay(2);
// We start by connecting to a WiFi network
Serial.println();
Serial.println(ssid);

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

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print(“Message arrived [”);
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();

// Switch on the LED if an 1 was received as first character
int val;
if ((char)payload[0] == ‘1’) {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
val = 1;
digitalWrite(4, val);
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
val = 0;
digitalWrite(4, val);
}

}

void reconnect() {
// Loop until we’re reconnected
while (!client.connected()) {
Serial.print(“Attempting MQTT connection…”);
// Attempt to connect
if (client.connect(“ESP8266Client”, mqttUser, mqttPassword )) {
Serial.println(“connected”);
// Once connected, publish an announcement…
client.publish("/switch/relay/state", “connected”);
// … and resubscribe
client.subscribe("/switch/relay/switch");
} else {
Serial.print(“failed, rc=”);
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {

if (!client.connected()) {
reconnect();
}
client.loop();

long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 75, “connected”, value);
Serial.print(“Publish message: “);
Serial.println(msg);
client.publish(”/switch/relay/state”, msg);
}
}

##############################################################################################