# Controlling Relay using mqtt

**URL:** https://discourse.nodered.org/t/controlling-relay-using-mqtt/62459
**Category:** General
**Created:** [10 May 2022 14:43 UTC](https://discourse.nodered.org/t/controlling-relay-using-mqtt/62459 "2022-05-10T14:43:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![rolandoartha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rolandoartha/32/60834_2.png) [@rolandoartha](https://discourse.nodered.org/u/rolandoartha)
#### Post date: [10 May 2022 14:43 UTC](https://discourse.nodered.org/t/controlling-relay-using-mqtt/62459/1 "2022-05-10T14:43:50Z")

</div>

hello guys,  
i want to control my relay by mqtt broker, it's already subcribe the data from Soil Moisture Sensor, but the relay is always off

this is the logic i use

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/486048dee5984d8f99d709f32be9250065c577fd.png)

this is the callback function

```auto
void mqttCallback(char* topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;
  
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  if (String(topic) == DeviceConfig.MqttSub) { // DeviceConfig.MqttSub = /command
    Serial.print("Changing output to ");
    if(messageTemp == "ON"){
      digitalWrite(AIO_SENSOR_PIN_3, HIGH);
      Serial.println("ON");
    }
    else if(messageTemp == "OFF"){
      digitalWrite(AIO_SENSOR_PIN_3, LOW); //AIO_SENSOR_PIN_3 expands to pin32 esp
      Serial.println("OFF");
    }
  }
}

```

and the value already sended  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/6/16366ee72154f4343682a039b2d31dc79573546e.png)

but the relay state is not affected at all

can you guys help me please?

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [10 May 2022 16:33 UTC](https://discourse.nodered.org/t/controlling-relay-using-mqtt/62459/2 "2022-05-10T16:33:38Z")

</div>

At first glance, the piece of code you have posted looks OK.

I assume `digitalWrite(AIO_SENSOR_PIN_3, HIGH);` maps to an actual pin?

If I was doing it, I would have used the \<ArduinoJson.h\> library in your sketch as you could send json from your flow which would give you much more flexibility in decoding a number of different commands.  
So, for example, you could send commands like...

`msg.payload = {"relay":"main","action":"on"};`  
or  
`msg.payload = {"relay":"secondary","action":"off"};`

The reason I say this is... you are bound to add extra relays or LEDs to your system. Plan ahead.

PS: It's not normal to start a topic-name with a` /`  
Maybe you can change it to just... `command` (in your flow and in your sketch).

**PPS: If you write a simple test-sketch (like the 'blink' sketch), can you turn the relay On and Off ?**

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [10 May 2022 22:27 UTC](https://discourse.nodered.org/t/controlling-relay-using-mqtt/62459/3 "2022-05-10T22:27:33Z")

</div>

What are you running the sketch on ? i.e. arduino uno, mini, ESP8266 etc etc Do you have a Fritzing schematic so we can see how that side looks ?

Craig

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [11 May 2022 16:37 UTC](https://discourse.nodered.org/t/controlling-relay-using-mqtt/62459/4 "2022-05-11T16:37:18Z")

</div>

One of my IoT students was asking about nearly the same question that you posted on the forum.  
He was using a Wemos D1 Mini - not sure what you are using. But I'm sure it could be adapted.

I've posted what we did on the forum as I think it might go some way to solving your problem.

> [@Controlling LEDs on a Wemos D1 Mini (Arduino code) via MQTT](https://discourse.nodered.org/t/controlling-leds-on-a-wemos-d1-mini-arduino-code-via-mqtt/62523):
>
> One of my IoT students was asking how to control a set of LEDs on a Wemos D1 Mini, that had been programmed using Arduino code, via MQTT commands from Node-RED. This is what the breadboard lash-up looks like... The red LED is connected to D8 (GPIO-15). The green LED is connected to D7 (GPIO-13) The yellow LED is connected to D6 (GPIO-12) This is the very simple test-flow we created in Node-RED. [Screen Shot 05-11-22 at 06.35 PM] [{"id":"bb2bbcc236726f09","type":"tab","label…

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [10 July 2022 16:37 UTC](https://discourse.nodered.org/t/controlling-relay-using-mqtt/62459/5 "2022-07-10T16:37:57Z")

</div>

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