OH3, MQTT, Arduino Topic Subscribe and Publish

I have been working on getting my openHAB to connect to my mosquito broker and then connect my Arduino to the broker to receive the data. I have set up openHAB Thing Channel and Item with the help of this article, [OH3] Tasmota relay via MQTT (Sonoff Basic with optional DHT22) although I am unsure if I have done it correctly.

and when clicking the switch the broker responds I was wondering if when writing my Arduino code if I use the .subscribe(“kitchen”) function to subscribe to the topic of “kitchen” in my case. If that is the case how would I format the code to know if it is receive the ON or OFF command of the switch?

Unfortunately your log from Mosquitto is truncated, so we can’t actually see what the full entries are. Can you grab from the Mosquito log file and copy/paste into your OP inside code fences?

Another option is to install the 3rd party MQTT Explorer or mqtt.fx to monitor what’s being published.

Hi @mach, maybe you got it solved already, however, I am sharing my Arduino code that works for me (Arduino Mega + W5500 Lan module). As a command I am sending via MQTT to Arduino “ON” and “OFF” instead of 1 or 0, but principles should be same. And I must say I am not a very good programmer, but it should work… :wink:

  //RobotDyn MEGA 2560
  //W5500 LAN modul
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Enter a MAC address (anything but must be unique in your network)
IPAddress ip(192, 168, 1, 175); // Enter IP address, The IP address will be dependent on your local network - it is the address of your arduino LAN module
EthernetClient ethClient;

const char* mqtt_server = "";//set your broker's IP
const char* mqttClientID = "177_60:01:94:37:8F:55";//must be unique for each client
const char* mqttUser = "abcdefg";//if used in your broker setup
const char* mqttPassword = "123456";//if used in your broker setup

PubSubClient mqttClient(ethClient);
char msg[50]; //MQTT msg conversion
const char* inTopicZas02Rele01 = "Relay01";//topic where Relay receives command
const char* outTopicZas02Rele01 = "Relay01State";//topic where Arduino sends confirmation of state changed

#define rele01Pin 5 // relay pin

//------------------------------------------------------ SETUP -------------------------------------------------------------------
void setup() {
  Ethernet.init(10);  // Most Arduino shields // You can use Ethernet.init(pin) to configure the CS pin
  Serial.begin(9600);// Open serial communications and wait for port to open:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only

  pinMode(rele01Pin, OUTPUT);
  digitalWrite(rele01Pin, HIGH);// switch relay off after reboot - may not be needed in your setup
  mqttClient.setServer(mqtt_server, 1883);
  mqttClient.setCallback(mqttCallback);// in this mqttCallback function defined below you handle the incoming message
  Ethernet.begin(mac, ip); // start the Ethernet connection and the server
  delay(1500);// Allow the hardware to sort itself out
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {// Check for Ethernet hardware present
    Serial.println("Ethernet shield was not found!");
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");

//------------------------------------------------------ LOOP -------------------------------------------------------------------

void loop() {

    if (!mqttClient.connected()) {


//------------------------------------------------------ MQTT FUNCTIONS -------------------------------------------------------------------
void mqttCallback(char* topic, byte* payload, unsigned int length) {
     // Convert the incoming byte array to a string
    payload[length] = '\0'; // Null terminator used to terminate the char array
    String message = (char*)payload;
    if(message == "ON"){
          if((strcmp(topic, inTopicZas02Rele01) == 0)){
              digitalWrite(rele01Pin, LOW);
              mqttClient.publish(outTopicZas02Rele01, "ON");
    if(message == "OFF"){
        if((strcmp(topic, inTopicZas02Rele01) == 0)){
              digitalWrite(rele01Pin, HIGH);
              mqttClient.publish(outTopicZas02Rele01, "OFF");

void mqttReconnect() {
  while (!mqttClient.connected()) {// Loop until we're reconnected
    //Serial.print("Attempting MQTT connection...");
    if (mqttClient.connect(mqttClientID, mqttUser, mqttPassword)) { // Attempt to connect
      //mqttClient.loop();//if you have more than few topics it is better to include the client.loop() after each topic subscription
    } else {
      /*Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      delay(5000);// Wait 5 seconds before retrying

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.