Function code to switch Relays over ESP Easy

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:

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 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:

 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.

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:

[{"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.

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

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.

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

Screen Shot David Sun Dec  4 17-30-01 2022

[{"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":""}]

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


see mqtt node

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?

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

Are you using Home-Assistant ??

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

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.

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

... 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


thanking You so much

1 Like

here you go:


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.

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 .

1 Like

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.

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.

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

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