Assign input to a variable

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

No experience with MQTT.

You have a callback function that prints the received data. So in that callback you can do whatever you want.

One note:

      // If its a 'g', activate led 2
      case '1':

Non-matching comments are useless :wink:

You have a callback function that prints the received data

I assume You talking about that part of a code

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

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

if im correct than how i can use that recived data to triger a digitalWire ?

becouse now it uses a Serial.read to triger it... and i feel like the problem is somewhere in that part of a code.

          char inputMessage = Serial.read();
          // Actions depending on the message sent
          switch (inputMessage)

correct me if im wrong. and thanks for a reply.

ps.I have 0 programing skills all i do is new stuff to me so forgive me if i say something stupid.

Write a function to control the LED; pick a name that makes sense

void doIt(char c)
{
    switch (c)
    {
      case '1':
        digitalWrite(5, HIGH);
        break;
      default:
        // Any other letter, reset all the lights
        digitalWrite(5, LOW);
        break;
    }
}

Change loop()

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

  // Send a message back, saying the letter that was processed
  Serial.println(inputMessage);


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

  client.loop();
}

and in callback for example (the below assumes that payload[0] contains the value of interest).

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

  doIt(payload[0]);

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

Note that (as said) I have no experience MQTT