OpenHab and esp8266

I am new to openHAB and esp8266. I want to build my self a smart light switch which consists of a four channel relay board and esp8266. I want to control the relay with MQTT from the openHAB. But i need help with the code for the esp8266 and openhab configuration.

4 lights are connected to relay

mqtt.cfg as follow
mqtt:broker.url=tcp://192.168.1.114:1883
mqtt:broker.clientId=openhab
mqtt:broker.retain=true
mqtt:broker.async=false

1 Like

I have a 4 relay board controlled from a nodeMCU controller running my sprinklers.

This is my arduino code




#include <ESP8266WiFi.h>
#include <MQTTClient.h>




/* WIFI Settings */
// Name of wifi network
const char* ssid = "Pretty_Fly_For_A_Wifi";

// Password to wifi network
const char* password = "9306023373"; 


/* MQTT Settings */
// Topic which listens for commands
char* subscribeTopic1 = "SprinklerSystem/Zone1"; 
char* subscribeTopic2 = "SprinklerSystem/Zone2";
char* subscribeTopic3 = "SprinklerSystem/Zone3";
char* subscribeTopic4 = "SprinklerSystem/Zone4";
char* publishTopic1 = "SprinklerSystem/Zone1/Stat"; 
char* publishTopic2 = "SprinklerSystem/Zone2/Stat";
char* publishTopic3 = "SprinklerSystem/Zone3/Stat";
char* publishTopic4 = "SprinklerSystem/Zone4/Stat";
char* publishTopic = "SprinklerSystem/State";

//MQTT Server IP Address
const char* server = "192.168.0.143";

//Unique device ID 
const char* mqttDeviceID = "SprinklerSystem"; 




int channe1 = 16; //Zone1
int channe2 = 0;  //Zone2
int channe3 = 2;  //Zone3
int channe4 = 14; //Zone4
int wifi_led = 13; //i have two LEDS hooked up
int mqtt_led = 15; //i have two LEDS hooked up

//MQTT
WiFiClient net;
MQTTClient client;

unsigned long lastMillis = 0;

//Connect to WiFI and MQTT
void connect();

//Setup pins, wifi, webserver and MQTT
void setup() 
{



  
  pinMode(wifi_led, OUTPUT);
pinMode(mqtt_led, OUTPUT);

  Serial.begin(115200);

  // set pin modes
  pinMode(channe1, OUTPUT);
  digitalWrite(channe1, HIGH); //My relay board needs a HIGH output to be OFF and a LOW output to be ON

  pinMode(channe2, OUTPUT);
  digitalWrite(channe2, HIGH); //My relay board needs a HIGH output to be OFF and a LOW output to be ON

  pinMode(channe3, OUTPUT);
  digitalWrite(channe3, HIGH); //My relay board needs a HIGH output to be OFF and a LOW output to be ON

   pinMode(channe4, OUTPUT);
  digitalWrite(channe4, HIGH); //My relay board needs a HIGH output to be OFF and a LOW output to be ON
  
  WiFi.mode(WIFI_STA);
  
  WiFi.begin(ssid, password);
  client.begin(server, net);
  client.onMessage(messageReceived);

  connect();

  
}

//Connect to wifi and MQTT
void connect() 
{
     delay(100);
   digitalWrite(wifi_led, LOW); //turn off WIFI_LED
  // We start by connecting to a WiFi network
    Serial.print("Connecting to ");
    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: ");

 digitalWrite(wifi_led, HIGH); //if wifi is connected, turn on WIFI_LED    
  Serial.println(WiFi.localIP());
  

  

  // Loop until we're reconnected
  while (!client.connected()) 
  
  {   digitalWrite(mqtt_led, LOW);   //turn off MQTT_LED
    Serial.print("Attempting MQTT connection...");

    // Attempt to connect

    if (client.connect(mqttDeviceID))

    {   digitalWrite(mqtt_led, HIGH);  //if MQTT is connected, turn on MQTT_LED
      Serial.println("connected");

     //once connected to MQTT broker, subscribe command if any
      client.subscribe(subscribeTopic1);
      client.subscribe(subscribeTopic2);
      client.subscribe(subscribeTopic3);
      client.subscribe(subscribeTopic4);
      client.publish(publishTopic,"ONLINE");

      
    } else {
      Serial.print("failed");
      Serial.println(" try again in 5 seconds");
      // Wait 6 seconds before retrying
      delay(6000);
    }
  }

}
  



void loop() 
{
  // MQTT Loop
  client.loop();
  delay(10);

  // Make sure device is connected
  if(!client.connected()) 
  {
    connect();
  }



}

// Change the state of a relay based on the MQTT Message
void messageReceived(String &topic, String &payload) 
{
  Serial.print(topic);
  Serial.print("\t");
  Serial.println(payload);

 String msgString = payload; 

 
          if (topic == subscribeTopic1 || topic == subscribeTopic2 || topic == subscribeTopic3 || topic == subscribeTopic4)
          {
           if (msgString=="ON")
            {
              if (topic == subscribeTopic1)
              {
                digitalWrite(channe1, LOW);
              client.publish(publishTopic1,msgString);
              Serial.println("Turning on Zone 1");
              }
              else if (topic == subscribeTopic2)
              {
                digitalWrite(channe2, LOW);
              client.publish(publishTopic2,msgString);
              Serial.println("Turning on Zone 2");
              }
              else if (topic == subscribeTopic3)
              {
                digitalWrite(channe3, LOW);
              client.publish(publishTopic3,msgString);
              Serial.println("Turning on Zone 3");
              }
              else if (topic == subscribeTopic4)
              {
                digitalWrite(channe4, LOW);
              client.publish(publishTopic4,msgString);
              Serial.println("Turning on Zone 4");
              }
            }
          
          if (msgString=="OFF")
            {
              if (topic == subscribeTopic1)
              {
                digitalWrite(channe1, HIGH);
              client.publish(publishTopic1,msgString);
              Serial.println("Turning off Zone 1");
              }
              else if (topic == subscribeTopic2)
              {
                digitalWrite(channe2, HIGH);
              client.publish(publishTopic2,msgString);
              Serial.println("Turning off Zone 2");
              }
              else if (topic == subscribeTopic3)
              {
                digitalWrite(channe3, HIGH);
              client.publish(publishTopic3,msgString);
              Serial.println("Turning off Zone 3");
              }
              else if (topic == subscribeTopic4)
              {
                digitalWrite(channe4, HIGH);
              client.publish(publishTopic4,msgString);
              Serial.println("Turning off Zone 4");
              }
            }
          }
}

and this is my items

Switch SprinklerSystemZone1 "Front Grass Sprinklers"     	(gIrrigation)      	[ "Switchable" ] 	{ mqtt=">[broker:SprinklerSystem/Zone1:command:*:default],<[broker:SprinklerSystem/Zone1/Stat:state:default]" , autoupdate="false" }
Switch SprinklerSystemZone2 "Front Garden Sprinklers" 	   	(gIrrigation)	 	[ "Switchable" ] 	{ mqtt=">[broker:SprinklerSystem/Zone2:command:*:default],<[broker:SprinklerSystem/Zone2/Stat:state:default]" , autoupdate="false" }
Switch SprinklerSystemZone3 "Back Grass Sprinklers" 		(gIrrigation)		[ "Switchable" ] 	{ mqtt=">[broker:SprinklerSystem/Zone3:command:*:default],<[broker:SprinklerSystem/Zone3/Stat:state:default]" , autoupdate="false" }
Switch SprinklerSystemZone4 "Back Garden Sprinklers" 		(gIrrigation)		[ "Switchable" ] 	{ mqtt=">[broker:SprinklerSystem/Zone4:command:*:default],<[broker:SprinklerSystem/Zone4/Stat:state:default]" , autoupdate="false" }
Switch SprinklerSystemZone1Restart	{ mqtt=">[broker:SprinklerSystem/Zone1:command:*:default]" , autoupdate="false" }
Switch SprinklerSystemZone2Restart 	{ mqtt=">[broker:SprinklerSystem/Zone2:command:*:default]" , autoupdate="false" }
Switch SprinklerSystemZone3Restart 	{ mqtt=">[broker:SprinklerSystem/Zone3:command:*:default]" , autoupdate="false" }
Switch SprinklerSystemZone4Restart 	{ mqtt=">[broker:SprinklerSystem/Zone4:command:*:default]" , autoupdate="false" }

String SprinklerSystemStatus	{ mqtt="<[broker:SprinklerSystem/State:state:default]" } //hidden string to be updated depending on status

if this can be of any use

4 Likes

You can leave out the 4 restart items and the status item if you want.
I use them to know when the board restarts. and to send the last status to MQTT again.
Because i couldn’t figure out how to retain just some messages on my mqtt broker instead of all. I do it this way.

1 Like

I want code of ESP8266 and openhab switch that will send command to esp8266 through raspberry pi to turning on and off light.

You can use my code and tweak it for yourself. Look online and YouTube videos on how to do it.

1 Like

Great Thank you…!!!