triger digitlWire via MQTT

Hello all you Arduino geeks :wink:

I'm makeing little home automation project with Arduino, openHAB and mqtt broker.

First thing i wont to pull up is a light switch on a relay.

I have manage to set up openHAB and MQTT broker, now im stuck on the Arduino side.

I used PubSubClient library to comunicate Arduino with MQTT, and its working well (useing examples and looking it up in MQTT.fx)

Arduino is subscribeing and publishing in Topics via MQTT it reciving data from MQTT as well (i can see it in a saerial port window)

I have some code that i will post below, in that code i can triger a digitalWire via Serial.read and my problem is to triger it via incoming data from a subscribed topic.

I think that the problem is in assigning to a variable but i might be wrong.

If you see any sugestion i'll be pleased.

ps.
Sorry for my english.

/*
 Basic MQTT example

 This sketch demonstrates the basic capabilities of the library.
 It connects to an MQTT server then:
  - publishes "hello world" to the topic "outTopic"
  - subscribes to the topic "inTopic", printing out any messages
    it receives. NB - it assumes the received payloads are strings not binary

 It will reconnect to the server if the connection is lost using a blocking
 reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
 achieve the same result without blocking the main loop.
 
*/

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>


// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 11);
IPAddress server(192, 168, 1, 10);

void callback(char* topic, byte* payload, unsigned int length) {
  

  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}

EthernetClient ethClient;
PubSubClient client(ethClient);

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");
      // Once connected, publish an announcement...
      client.publish("outTopic","hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup()
{
  Serial.begin(9600);
 pinMode(5, OUTPUT);

  client.setServer(server, 1883);
  client.setCallback(callback);
  Ethernet.begin(mac, ip);
  // Allow the hardware to sort itself out
  delay(1500);
}

void loop()

{
     // Check that our USB cable interface is functioning
     if (Serial.available() > 0) {
     // Check for input and assign the input to a variable
          char inputMessage = Serial.read();
          // Actions depending on the message sent
          switch (inputMessage) {
          // If its a 'g', activate led 2

               case '1':
                    digitalWrite(5, HIGH);
                    
               break;

               default:
                    // Any other letter, reset all the lights

                    digitalWrite(5, LOW);
                    

               }
 
          // Send a message back, saying the letter that was processed
          Serial.println(inputMessage);
     }
     
     
  {
  if (!client.connected()) 
  {
    reconnect();
  }
}
  
  client.loop();
}

Your code currently reads the input from Serial (the USB port) rather than the MQTT topic- is this what you want? Maybe you did this for testing.

Another issue might be this: if you send a single letter from your terminal its likely that you will send a return character as well. Your code will read the first letter and turn on the LED but straight away read the return character and turn the LED off. You can test for this by changing your code to require a specific letter to turn off eg

switch (inputMessage) {
 
  case '1':
    digitalWrite(5, HIGH);
    break;
 
  case '2':
    digitalWrite(5, LOW);
    break;
 
 
}

Hi,

Thanks for a reply.

You are right, curently it reads from serial.print and that is what I don't want it to do.

I want it to read the value from the MQTT topic and triger a digitalWire based on the value it read.

Unfortunately i have no experience in programing language and im stuck.

Any advise will be pleased.

In that case try changing Serial.available() to client.available() and Serial.read() to client.read()