# Function code to switch Relays over ESP Easy

**URL:** https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637
**Category:** General
**Created:** [3 December 2022 09:58 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637 "2022-12-03T09:58:33Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![at-bh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/at-bh/32/72219_2.png) [@at-bh](https://discourse.nodered.org/u/at-bh)
#### Post date: [3 December 2022 09:58 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/1 "2022-12-03T09:58:33Z")

</div>

Seemed easy, but my relais on a D1 mini (over GPIO5) do not switch with function code on a ESP Easy flashed device. Switching via http works perfect. Using Mqtt and Node Red, just a simple flow  
inject node - function - mqtt --\> the fuctionnode has the code:

```auto
var gpio_pin = 5; // = represents "D1" on a wemos d1 mini
  var espeasy_device_name = "ESP_Easy"
  var gpio_state = msg.payload;
  msg.topic = String(espeasy_device_name) + "/cmd";
  msg.payload = "gpio," + String(gpio_pin) + "," + (gpio_state ? "1" : "0");
  return msg;

```

With command inside the browser [http://192.168.178.xxx/control?cmd=gpio,5,1](http://192.168.178.xxx/control?cmd=gpio,5,1) the relais is switching fine. Transfer to mqtt is also working, but no switch will released with this code. My question is, how should the function code be modified to gain a switching event. Alternatively my  
second question, how to create a command for ESPEasy just to call an event, witch is f.e. declaired inside a rule. My rule declaration is:

```auto
 On relaisein do
  GPIO,5,1 //Relais D1 On
  endon
  
  On relaisaus do
  GPIO,5,0 // Relais D1 Off
  endon

```

Help would be great, tried several things, without success.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [4 December 2022 14:48 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/2 "2022-12-04T14:48:53Z")

</div>

> [@at-bh](#):
>
> control?cmd=gpio,5,1

I've just setup a working example but have run out of time. I'm going to the 'Cirque de Sol Christmas' performance with three of my grandchildren and their partnes and will be gone for about 5 hours.

Here is the simple flow:

```auto
[{"id":"f06fcd03923d3be5","type":"inject","z":"68e0e5c3e5fa25a3","name":"Uff","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"gpio,5,0","payloadType":"str","x":290,"y":260,"wires":[["dc371aa6b2f78362"]]},{"id":"dc371aa6b2f78362","type":"mqtt out","z":"68e0e5c3e5fa25a3","name":"","topic":"node46/cmd","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"1b5cd988ac6a4346","x":650,"y":280,"wires":[]},{"id":"d87529616f3a292a","type":"inject","z":"68e0e5c3e5fa25a3","name":"On","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"gpio,5,1","payloadType":"str","x":290,"y":300,"wires":[["dc371aa6b2f78362"]]},{"id":"1b5cd988ac6a4346","type":"mqtt-broker","name":"","broker":"mqttpizw.local","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

```

and when I get back I'll send you the ESPEasy settings...gotta dash now.

---

<div class="post-metadata">

### Author: ![at-bh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/at-bh/32/72219_2.png) [@at-bh](https://discourse.nodered.org/u/at-bh)
#### Post date: [4 December 2022 16:39 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/3 "2022-12-04T16:39:31Z")

</div>

Hi zenofmud,  
hope Your event was fine ... trying Your flow, working fine so far to node:  
` mqtt.0.node46.cmd`  
there will be written the state by injecting the flow  
Uff: gpio,5,0  
On: gpio,5,1  
but now my hot question is, how to tell this to ESP\_Easy with Topic and MQTT  
Very, very nice from You to help me, trying since hours to find out, how to transfer the command to ESP\_Easy

---

<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: [4 December 2022 17:31 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/4 "2022-12-04T17:31:48Z")

</div>

Just a thought... are you using the correct GPIO reference within ESPeasy ??.  
D5 is gpio-14 \<\<\< which is what you need in the MQTT-Out node.

 ![Screen Shot David Sun Dec 4 17-24-36 2022](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/6/86e32b0a3fb1499e09803136ee8f7570349a0933.png)

Could you try this and see if you can switch D5 on/off

![Screen Shot David Sun Dec 4 17-30-01 2022](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/b/cbe959c085026bfe18ccddd5257ba2fd857a2ae8.png)

```auto
[{"id":"a76ca09c8b3d3e1a","type":"tab","label":"Forum","disabled":false,"info":"","env":[]},{"id":"a85c52fe0d38fbe4","type":"mqtt in","z":"a76ca09c8b3d3e1a","name":"","topic":"node96/status","qos":"2","datatype":"auto-detect","broker":"7002d750.c4462","nl":false,"rap":true,"rh":0,"inputs":0,"x":210,"y":140,"wires":[["5eb012fb9ccfc202"]]},{"id":"5eb012fb9ccfc202","type":"debug","z":"a76ca09c8b3d3e1a","name":"debug 895","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":140,"wires":[]},{"id":"90f68b9fcbc18415","type":"mqtt out","z":"a76ca09c8b3d3e1a","name":"","topic":"node96/cmd/gpio/14","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"7002d750.c4462","x":420,"y":240,"wires":[]},{"id":"4f528293f9d4322e","type":"inject","z":"a76ca09c8b3d3e1a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":210,"y":240,"wires":[["90f68b9fcbc18415"]]},{"id":"ab24ed1993fb7ec1","type":"inject","z":"a76ca09c8b3d3e1a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":210,"y":300,"wires":[["90f68b9fcbc18415"]]},{"id":"7002d750.c4462","type":"mqtt-broker","name":"","broker":"192.168.1.156","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

```

---

<div class="post-metadata">

### Author: ![at-bh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/at-bh/32/72219_2.png) [@at-bh](https://discourse.nodered.org/u/at-bh)
#### Post date: [4 December 2022 19:01 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/5 "2022-12-04T19:01:56Z")

</div>

thanks tried Your solution, but the relais itself is not switching, inside esp\_easy menue I was using the pin D5 (gpio 14).  
via mqtt the message will be published and on port

 ![esp_easy1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/e/3e5d00c4295059c9a6cdcda39b57b98d099fd453.jpeg)  
see mqtt node  
 ![esp_easy2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/6/b65f9c39aa7a7717e2ce5f3c56324fdbde13a1c3.jpeg)  
Switching via browser with command is working fine  
`http://192.168.178.127/control?cmd=gpio,14,1`  
Is there something else to change inside the ESP menue?

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [4 December 2022 19:17 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/6 "2022-12-04T19:17:42Z")

</div>

I’ll be home in about a half hour and send you the info

---

<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: [4 December 2022 19:57 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/7 "2022-12-04T19:57:55Z")

</div>

Are you using Home-Assistant ??

---

<div class="post-metadata">

### Author: ![at-bh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/at-bh/32/72219_2.png) [@at-bh](https://discourse.nodered.org/u/at-bh)
#### Post date: [4 December 2022 20:37 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/8 "2022-12-04T20:37:46Z")

</div>

No I want just switch a relaisshield with a simple Wemos D1 Mini (flashed with ESP\_Easy) ...

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [4 December 2022 20:43 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/9 "2022-12-04T20:43:56Z")

</div>

Ok, on the Wemos on the 'Controlers' tab,  
1 - select 'Home Assistant (openhanded) MQTT'  
2 - enter your the MQTT Brokers IP in the 'Controller IP' option  
3 - I set the Controller Subscribe to 'node46/cmd' because node46 is what I'm calling that WeMos

That is it for the wemos side. I have a 'Relay Shield' connected to the WeMos so it defaults to using D1 (which is GPIO pin 1) to control it.

That plus the little flow I put up earlier is all I need to make the relay work.

---

<div class="post-metadata">

### Author: ![at-bh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/at-bh/32/72219_2.png) [@at-bh](https://discourse.nodered.org/u/at-bh)
#### Post date: [4 December 2022 20:54 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/10 "2022-12-04T20:54:00Z")

</div>

What should I write into the controller (homeassistent) fields MQTT ??? Could You please send Your settings?

---

<div class="post-metadata">

### Author: ![at-bh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/at-bh/32/72219_2.png) [@at-bh](https://discourse.nodered.org/u/at-bh)
#### Post date: [4 December 2022 21:03 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/11 "2022-12-04T21:03:48Z")

</div>

... I tried the following ... and it's working ... great  
seemed I had the wrong settings  
wow I am happy now, but please just for checking, send me please Your controller settings belonging to mqtt

 ![mqtt_node46](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/2/a2f5171e473004200ab9931e9f480af294f2dd48.jpeg)  
thanking You so much

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [4 December 2022 22:25 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/12 "2022-12-04T22:25:18Z")

</div>

here you go:

 ![Screen Shot 2022-12-04 at 5.24.13 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/3/73d5f4ad62c2d14e52c0c8f1bb6cfc2cc0db8aaa.png)  
 ![Screen Shot 2022-12-04 at 5.24.03 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6f7e99d605ea66a8ed66ebaf7083367a4697e112.png)

---

<div class="post-metadata">

### Author: ![at-bh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/at-bh/32/72219_2.png) [@at-bh](https://discourse.nodered.org/u/at-bh)
#### Post date: [5 December 2022 11:07 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/13 "2022-12-05T11:07:07Z")

</div>

SOLVED ... now just a feedback  
First of all ... thank You gentlemen, for helping.  
Both ways, solution from zenofmud and dynamicwave are working fine, my major problems were wrong settings in Controller Subcribe and Controller Publish. Now everything is working even the proposal from  
nerdiy with a function Node script - ESP Easy with NodeRed, MQTT Switching. This was my starting and it took me long time to understand and find out my failures, finally with the help of zenofmud and dynamicwave.

 ![relais-switch with functioncode](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/f/0fc01af6b9c054718581534fd279eb8b5211ddd0.jpeg)

---

<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: [5 December 2022 11:46 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/14 "2022-12-05T11:46:02Z")

</div>

Glad to hear you have got your set-up working. Be careful - using Node-RED can become **addictive**!!

I noticed you said you are using D1 on GPIO-5 on a Wemos to drive your relay. You might want to consider using a different pin as D1 and D2 are often used for I2C. I usually make use of pins D7, D6, D5 as they can be used as inputs or outputs without any side-effects.

Here's a link to a useful document that shows the 'features' of each pin. You'll need to scroll-down the document to the section labelled... 'Best Pins to Use – ESP8266'

[ESP8266 Pinout Reference: Which GPIO pins should you use? | Random Nerd Tutorials](https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/) .

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [5 December 2022 12:10 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/15 "2022-12-05T12:10:49Z")

</div>

One additional point, if you use a WeMos Relay Shield, you have to use D1 - GPIO-5 pins bucause that is how it is setup on the shield.

---

<div class="post-metadata">

### Author: ![at-bh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/at-bh/32/72219_2.png) [@at-bh](https://discourse.nodered.org/u/at-bh)
#### Post date: [5 December 2022 13:09 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/16 "2022-12-05T13:09:54Z")

</div>

thanks again for hints, I was using in the past sometimes i2c, with OLED and BME280 devices and I agree to Your opion, but as zenofmud mentioned, I purchaised a relay shield, and I don't know why this is fixed wired to GPIO5 (D1). Probably it can be reconfured, but yet I don't know how ... will find out pinning probably it can be resolderd to another pin.

---

<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: [5 December 2022 13:28 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/17 "2022-12-05T13:28:26Z")

</div>

Oh sorry - I didn't see the statement where you said you are using a relay-shield.

---

<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: [19 December 2022 13:29 UTC](https://discourse.nodered.org/t/function-code-to-switch-relays-over-esp-easy/71637/18 "2022-12-19T13:29:07Z")

</div>

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