So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By hagensieker
#61801 Fairly new to ESP8266 and am trying to figure out how to control a 2 channel relay via firmware with MQTT, preferred. I cannot find any firmware examples anywhere. 1 relay is no problem but I'd like to be able to control the other channel without adding a 2nd ESP866.

Any and all help appreciated. Here are my first two ESP8266 projects (copying others work)

http://www.hagensieker.com/blog/page/?p ... om-esp8266

http://www.hagensieker.com/blog/page/?p ... ifi-outlet
User avatar
By g6ejd
#61804 Out of interesting your wiring looks ok, the lolin nodemcu Vin is the input to the on-board regulator that has a ~4 to 20 volt input range, do using 5volts is ideal as does your relay.
User avatar
By hagensieker
#61810 I actually kind of solved my own problem. I found a firmware that uses aRest at this site.

https://www.seeed.cc/Wifi-Relay-With-ES ... 1680.html#!

At the bottom of the page is a download link to wirerelay.ino

It uses aRest and has a server built in with 2 relay on and off buttons. You just have to find the IP of your chip. I had originally wanted MQTT because I use Home Assistant and I thought it would tie in easier with that.

However I found out that I can still call the aRest server and issue the on and off command in configuration.yaml like this"

Code: Select allswitch 5:
  platform: command_line
  switches:
    arest_pin_two:
      command_on: "/usr/bin/curl -X GET http://192.168.20.102/digital/2/1"
      command_off: "/usr/bin/curl -X GET http://192.168.20.102/digital/2/0"
      friendly_name: Kitchen Lightswitch


To make the next relay you would just make another entry (i.e. Switch 6) and on this one my pin would be 0 instead of 2. (That's the default in this code.) I'm using NodeMCU 1.0 and that would be pins D0 and D2. Easy enough to change the assignments by changing my 2 above to a 4 or anything else.

Now I only need to build another driver circuit as my relay won't work with the GPIO 3.3v. It actually works but works backwards. On is off, off is on. I guess I could change the pin state and recompile but that's not really the "right" way. I'll build a driver for both sides with a 2N3904 NPN transistor.
User avatar
By g6ejd
#61845 Looks like your problem is solved.

I'd say 'active low' is the default for most designs, which is what you have, maybe the relays are single-pole types otherwise you could move one wire over to invert the switch.