MQTT garage door opener with Node-RED

Absolutely right, all correct.

the flow is to accept and respond to MQTT messages.
i'm trying to keep it simple and under-control
for example open and close are working.
problem - but when i have both switches on... motor is stopped and depending on open/close on postion it directly starts....motor stop isn't a problem, but working in wrong direction is.
hopefully you understand ?

I think part of the problem is that the feedback notification of the received message is creating a loop, that maybe why the motor keeps going once it starts.

Create flows that do one thing, and accept results. But don't feed the result back into the same flow. This is to just get the basic functions working, like ON and OFF, etc. Then we can decide how to handle direction control. For example, we might want to create a flow that ONLY lets the direction be set then the motor is OFF. But lets get the basic functions working first with no feed back actions just feed back reporting.

Push ON Button->MQTT send->Broker->MQTT receive->Motor On
Push OFF Button->MQTT send->Broker->MQTT receive->Motor Off

The above ignore for now any feedback, just simple, easy actions.

Then create separate flows for getting the feedback

Motor ON->MQTT send->Broker->MQTT receive->Debug Message
Motor OFF->MQTT send->Broker->MQTT receive->Debug Message

Notice these are 4 flows that are separate from each other. So there should be no chance of the feedback creating a loop. Get these 4 flows working, and the motor turning on and off as desired. Then once this is working, we can move to the next step(s).

It looks as if you are making great progress with the splendid help from @Nodi.Rubrum, so I won't interfere with the flows (as that would possibly confuse the situation).

However, to answer a couple of the questions you raised in this thread.
You could send a jason string via MQTT to operate the two input pins (C and D) of the L289 H-Bridge.
Something like...

msg.payload = {"gateID":"xxxx","cmd":"yyyy"};
return msg;

Where "xxxx" can be "south" or "east" and "yyyy" can be "stop", "open" or "close".

Then in your Arduino code you could simply decode these values and operate pins C and D (D8 and D7).
Screen Shot 09-27-20 at 09.59 AM
There's a brilliant library called <ArduinoJson.h> that makes doing this a breeze. Get version 6.

You could even make the Arduino sketch completely generic and use a 'config.h' to define the gate's location.

As @Nodi.Rubrum said you need to get the four flows working. If once you've done that you need advice with the 'backend', please let me know.

Dave,

i totally agree that's the way most people are achieving this i guess. also everyone has their own method and setup functions/sensors/etc.

to avoid any mess i've setup a separate mqtt server and here's my flow.
all i have on my nodemcu running tasmota firmware

connections are as follows

NodeMCU L298N(H-Bridge)
D7 (BROWN-JUMPER) IN2
D8 (RED-JUMPER) IN1
GND (YELLOW-JUMPER) GND

From L298N(H-Bridge) 2 wires go out to the motor
Motor(wires) wire(in middle) L298N(H-Bridge)
RED black-wire OUT1
BLUE red-wire OUT2

From 12V power-adapter 2 wires come out
BLACK connects to GND on L298N(H-Bridge) middle
RED connects to NC on L298N(H-Bridge) left


Question - Should i be connecting the L298N(H-Bridge) OUT3 and OUT4 also to the same motor ?

Here's my flow still the basic, reason i'm seeking to put the "Gate<" in off position when i press "Gate>" button reason, motor rotates in different direction

[{"id":"a14b09c4.ca5f18","type":"ui_switch","z":"10b29368.6f662d","name":"PUMP","label":"WATER PUMP","tooltip":"","group":"1609fe57.dc1592","order":1,"width":"4","height":"2","passthru":true,"decouple":"false","topic":"/home/Utanki","style":"","onvalue":"on","onvalueType":"str","onicon":"","oncolor":"","offvalue":"off","offvalueType":"str","officon":"","offcolor":"","x":270,"y":80,"wires":[["f654a853.6e194"]],"icon":"font-awesome/fa-bolt","info":"# à€®à¥‹à€Ÿà€°"},{"id":"1609fe57.dc1592","type":"ui_group","z":"","name":"PUMP","tab":"2a9f0caf.995024","order":1,"disp":true,"width":"8","collapse":false},{"id":"2a9f0caf.995024","type":"ui_tab","z":"","name":"Home","icon":"home","order":1,"disabled":false,"hidden":false}]

Believe me, when i comes to switch on and off the lights, i am okay with that. it's just the motor i'm dealing with for the first time. :grinning: Motors are painful

Not sure if this will be easy
i was searching for the code and got this github project, which i really find exactly where i would like to get. but this uses platformIO

also has provided the youtube guides that i've watched couple times

but i get this error

Although you don't show it on your drawing, I assume you have connected the GND of the H-Bridge to GND on the NodeMCU ???

The datasheet says the maximum current load MUST NOT exceed 2 amps, but you can increase this by connecting Ch-1 with Ch-4 and Ch-2 with Ch-3 and pairing-up the appropriate IN pins.
I suggest you check the datasheet for full details.

I've not tried the L298N with an ESP8266 or a Raspberry Pi. These devices have 3v3 logic levels, whereas the H-Bridge uses 5v logic. It should work as the logic-high level from the microcontroller is higher than the lowest value for VinH for the H-Bridge. Someone on the forum may be able to confirm this for you.

Last point, I've not used Lua or Tasmota firmware with the NodeMCU, although I have uploaded an Arduino test sketch to one of my Wemos D1 Minis this afternoon and found the D8 and D7 outputs work fine.

Totally Agreed, Dave. confirmed.

My 5 wemos D1 mini's are arriving on Tuesday, NodeMCU is good for 3.3V for 5V wemos D1 mini's are getting preferred.

i've got rid of tasmota-firmware from the NodeMCU and sorted out the issue with the platformIO config errors. Also have been able to rotate the motor just fine. working with node-red flow now to publish to the topics to open/close/stop etc.

Nodered remains the brain.. and i feel comfortable with it.

I've just found an H-Bridge and lashed it up. It works fine. I had to use D7 and D6 (rather than D8 and D7) as D8 is used during boot-up and affects the motor. If it helps, here's my Node-RED test flow.

[{"id":"e3cdd336.8c8e5","type":"mqtt in","z":"99e54d66.bb9098","name":"","topic":"esp/test/#","qos":"2","datatype":"auto","broker":"a4345f1b.854f48","x":160,"y":400,"wires":[["40f52a29.0c24cc"]]},{"id":"40f52a29.0c24cc","type":"debug","z":"99e54d66.bb9098","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":350,"y":400,"wires":[]},{"id":"abbb7b37.45e358","type":"mqtt out","z":"99e54d66.bb9098","name":"","topic":"esp/test","qos":"","retain":"","broker":"a4345f1b.854f48","x":620,"y":600,"wires":[]},{"id":"f1699e9.6a366e","type":"inject","z":"99e54d66.bb9098","name":"Trigger","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":130,"y":480,"wires":[["ff7b0c39.2ace"]]},{"id":"ff7b0c39.2ace","type":"function","z":"99e54d66.bb9098","name":"South gate, OPEN","func":"msg.payload = {\"gateID\":\"south\",\"cmd\":\"open\"};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":480,"wires":[["abbb7b37.45e358"]]},{"id":"e5d2aee1.bf358","type":"function","z":"99e54d66.bb9098","name":"South gate, CLOSE","func":"msg.payload = {\"gateID\":\"south\",\"cmd\":\"close\"};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":380,"y":520,"wires":[["abbb7b37.45e358"]]},{"id":"6da00565.dd393c","type":"function","z":"99e54d66.bb9098","name":"South gate, STOP","func":"msg.payload = {\"gateID\":\"south\",\"cmd\":\"stop\"};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":560,"wires":[["abbb7b37.45e358"]]},{"id":"470fb7fc.fddf3","type":"inject","z":"99e54d66.bb9098","name":"Trigger","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":130,"y":520,"wires":[["e5d2aee1.bf358"]]},{"id":"41b4b16f.041ef8","type":"inject","z":"99e54d66.bb9098","name":"Trigger","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":130,"y":560,"wires":[["6da00565.dd393c"]]},{"id":"d6f7ef01.e2d2d8","type":"function","z":"99e54d66.bb9098","name":"East gate, Open","func":"msg.payload = {\"gateID\":\"east\",\"cmd\":\"open\"};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":640,"wires":[["abbb7b37.45e358"]]},{"id":"c61bdced.cb5a1","type":"inject","z":"99e54d66.bb9098","name":"Trigger","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":130,"y":640,"wires":[["d6f7ef01.e2d2d8"]]},{"id":"6b109259.04499c","type":"function","z":"99e54d66.bb9098","name":"East gate, Close","func":"msg.payload = {\"gateID\":\"east\",\"cmd\":\"close\"};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":680,"wires":[["abbb7b37.45e358"]]},{"id":"cde5fabd.46ec","type":"inject","z":"99e54d66.bb9098","name":"Trigger","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":130,"y":680,"wires":[["6b109259.04499c"]]},{"id":"c203ea2c.60ae","type":"function","z":"99e54d66.bb9098","name":"East gate, STOP","func":"msg.payload = {\"gateID\":\"east\",\"cmd\":\"stop\"};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":720,"wires":[["abbb7b37.45e358"]]},{"id":"99367551.a7838","type":"inject","z":"99e54d66.bb9098","name":"Trigger","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":130,"y":720,"wires":[["c203ea2c.60ae"]]},{"id":"19bb5945.c02df7","type":"comment","z":"99e54d66.bb9098","name":"Change IP address and topic to match your broker","info":"","x":760,"y":560,"wires":[]},{"id":"a4345f1b.854f48","type":"mqtt-broker","z":"","name":"RPi-146","broker":"192.168.1.146","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Dave,

i think we're close :grinning:

i've connected a relay now with the nodemcu running the platformIO code
And i'm using syslog server to monitor the logs
PINS in use currently
NodeMCU Relay
VIN orange VCC
GND white IN1
D6 yellow IN2
D2 black GND (when i remove this cable, status changes from close to open) shows on syslog and relay turns on and off

i think issue is with the flow still, if you can look at it once

[{"id":"10b29368.6f662d","type":"tab","label":"Home","disabled":false,"info":""},{"id":"f654a853.6e194","type":"Sonoff device Enhanced","z":"10b29368.6f662d","mode":"0","broker":"da22485c.d50c","device":"Utanki","name":"Utanki","onValue":"ON","offValue":"OFF","toggleValue":"toggle","cmdPrefix":"cmnd","statPrefix":"stat","telePrefix":"tele","x":530,"y":80,"wires":[[]]},{"id":"a14b09c4.ca5f18","type":"ui_switch","z":"10b29368.6f662d","name":"PUMP","label":"WATER PUMP","tooltip":"","group":"1609fe57.dc1592","order":1,"width":"4","height":"2","passthru":true,"decouple":"false","topic":"/home/Utanki","style":"","onvalue":"on","onvalueType":"str","onicon":"","oncolor":"","offvalue":"off","offvalueType":"str","officon":"","offcolor":"","x":270,"y":80,"wires":[["f654a853.6e194"]],"icon":"font-awesome/fa-bolt","info":"# à€®à¥‹à€Ÿà€°"},{"id":"f20926e6.a17c28","type":"mqtt out","z":"10b29368.6f662d","name":"","topic":"/controls/garagedoor","qos":"","retain":"","broker":"aed54839.77f6a8","x":510,"y":420,"wires":[]},{"id":"c4f05f26.6e549","type":"mqtt out","z":"10b29368.6f662d","name":"","topic":"/devices/remotepower","qos":"","retain":"","broker":"aed54839.77f6a8","x":510,"y":480,"wires":[]},{"id":"e7d8579a.ef54b","type":"inject","z":"10b29368.6f662d","name":"1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":380,"wires":[["f20926e6.a17c28","11ef002b.7cb64"]]},{"id":"7d3e4c41.a55f64","type":"mqtt in","z":"10b29368.6f662d","name":"","topic":"sensors/doors/garage","qos":"2","datatype":"auto","broker":"aed54839.77f6a8","x":200,"y":260,"wires":[["11ef002b.7cb64"]]},{"id":"9f650321.23a9c","type":"inject","z":"10b29368.6f662d","name":"0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":420,"wires":[["f20926e6.a17c28","11ef002b.7cb64"]]},{"id":"c0822233.367b08","type":"inject","z":"10b29368.6f662d","name":"0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":460,"wires":[["c4f05f26.6e549","11ef002b.7cb64"]]},{"id":"11ef002b.7cb64","type":"debug","z":"10b29368.6f662d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":300,"wires":[]},{"id":"da22485c.d50c","type":"mqtt-broker","z":"","name":"pi","broker":"192.168.1.11","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"1609fe57.dc1592","type":"ui_group","z":"","name":"PUMP","tab":"2a9f0caf.995024","order":1,"disp":true,"width":"8","collapse":false},{"id":"aed54839.77f6a8","type":"mqtt-broker","z":"","name":"192.168.1.11","broker":"192.168.1.11","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"2a9f0caf.995024","type":"ui_tab","z":"","name":"Home","icon":"home","order":1,"disabled":false,"hidden":false}]

main-cpp.txt (6.2 KB)

share the connections picture, if possible

Here are my Arduino files to handle receiving a MQTT message from Node-RED and driving the L298N H-Bridge which is connected to pins D7 and D6 on the Wemos. I've used the blue builtin LED to indicate when the motor is turning. All the user definitions are in config.h

main-cpp.txt (3.8 KB) <<< This file is suitable for 'platformIO'

mqtt_sub_pub.txt (3.9 KB) <<<<<< Change the filename to mqtt_sub_pub.ino

config.txt (851 Bytes) <<<<<< Change the filename to config.h

I've used the Arduino IDE to write, compile and upload the code.
You will need to install the ArduinoJson library version-6 <<< Make sure it's version-6
This is needed to deserialize the Json string and extract the values for gateID and cmd.

PS:
Do you know the voltage and current requirements of the actual motor you will be using to operate the gate, as the L298N is only intended for low current and low voltage operation? (e.g. robots, toy model trains)

PPS:
Just noticed you're using PlatformIO. I must read-up on that as it looks really interesting and productive.

2 Likes

There is one more issue with motors.... noise. I have on every motor I have used, soldered a capacitor across the motor leads (+ and -) which seems odd until you realize it GREATLY removes electromagnetic noise, which can lead to all kinds issues. Wires and input and outputs randomly fail, etc. So if you get some strange behavior, this might be part of the issue. I had a simple motor project where I also had a input sensor, and the input sensor was sending junk results, but it only happened when the motor was on. That was the clue.

Here is an excellent discussion on the issue and how to resolve it, if it is happening...

All the NodeMCU or ESP stuff I have done, I have done in straight C, since I have long history with C and C# in .NET. But the more I see of the ESP Home and other canned firmware images, the more I am thinking I need to spend some serious time with same.

1 Like

funny thing about me that professionally i'm a Network and Voip Engineer have more than a dozen cisco certifications and design contact centers, architecting the infrstatructure etc but i never did software and programming stuff ever.

For contact center web/call/chat i did do some flows very much similar to nodered that encouraged me to use basic stuff.

all this i'm doing is about my passion and learning curve. success of this DIY project should give me comfort and satisfaction i feel. Arduino uno as a device had no wifi plus i don't personally like battery operated remotes to open the gate especially when WIFI has become a very basic. with ESP8266 i guess it's endless with all your background skills you can use.

in my case again neither i have programming skills nor the electronics but with platformIO the way it can tell the minor issues with code and libraries so helpful, in no time anyone can pick this up and do what they want.

i am fascinated with the ideas like automatic-blinds, water-tank-alarms and sliding gates, totally under control

how about sharing connections basic pic or a rough drawing will be cool

Dave,

About the motor i'm trying to stick to car power-window motor or wiper-motor.
Agreed no one uses the L298N H-Bridge but i was just trying to get this easily.

with relays i've seen people again got their own logics, for example some are able to do this open/close/stop gate with single relay, some use 2 in case they put sensors and want to break it down into easy pieces and some even use 3...best approach i like the guy is using just a basic-sonoff. :smile:

please tell me if this is correct, what i'm missing

You have the grey and yellow wires incorrectly wired on Relay 2. They need to be swapped over.
As you have them wired at the moment the motor will only turn in one direction.
I'm off out so don't have the time to draw a diagram for you. I'll do it when I return this evening.

1 Like

Thanks Dave,

it appears that didn't work either, i tried swapping relay 2 A and C
grey into C currently
yellow into A currently
i think that's what you suggested but something's still wrong