As you can probably see, I’m putting together the building blocks to do something better. Each of these articles describes one sensor and how I hooked it up to the ESP, but on their own these are not that useful. This article is possibly most true in that department. I have mentioned how much I like bistable relays before, and I mostly mention it to get more people to buy them because right now TE Connectivity seems to be the only people making them and I want tons of cheap ones out there to play with (there are breakouts on ebay, but seriously: more people need to love these things). The benefit is simply that they are relaxed in two states. There are/were ratcheting relays that have more relaxed states, or possibly move sequentially IIRC, but what I’m talking about are relays that can be light switches. That means at least SPDT(3-way switch equivalent), and hopefully DPDT (n-way switch equivalent). I may be using a power-hungry micro-controller, and not putting it to sleep, but dammit I will not burn current in a relay coil!
The problem with using a bistable relay as a light switch is that without sensing if the power is currently flowing you have no idea which way will turn the lights on or off. If you remember which way the relay is currently settled you could do a ‘toggle’ to switch to the other position, but this does not help with ‘turn all the lights off’ unless you know which ones are on. That method also takes a bit in memory because at any power up the relay could be in either position (you can read the state of a digital output to see if it is set high or low, but in this case that isn’t happening). The answer I’m sure most people would come up with is to use a current sensor. Either a non-invasive current transformer or a shunt resistor. That is a fine idea, and there even exists a nice library for doing just that on the atmega 328, but I haven’t gotten around to porting that to the ESP yet.
The very clever method I saw once was to take a neon tube, a 100 ohm current limiting resistor and a photo-diode. You wire up the resistor and neon tube to glow when the power is flowing to the light and detect it with the photo-diode, putting the whole thing in heatshrink. This is a very simple opto-isolator, but it uses a neon tube to be compatible with high voltage. There are detectors that can sense voltage and even digitize the frequency, but I would much rather have current reading so I’m holding out for that. I may use the neon tube trick to get off the ground if I can’t port the current reading code fast enough, or I could throw on a 3.3v arduino pro mini and offload the analog to that.
The current implementation is just controlling a power strip with no feedback (look for the current reading or voltage detection in my power node post when I get that solved). The relay takes the 3.3v signals and drives the coils through 2 2n7000 FETs, they’re very convenient and I’ll probably get a favorite smd one but for now I love these. The relays are 5v coil, and get powered before the 3.3v regulator on the NodeMCU. I have 2 pins, and while I call them On and Off, I labeled the relay with A, B, and C(ommon) so I wouldn’t forget what the pinout was (or which FET triggered what. This setup’s limitations are that I cannot determine the state on boot, and cannot determine if it is flowing current or not. I have listed how I could do that above, and even now I could have it switch Off on boot just in case it was on, but I haven’t. The code assumes off on boot, but intil you trigger one of the coils that will not sync up with what the output is.
What I did for MQTT commands was to have the relay only triggered when needed. I envision sending broadcast messages saying to turn off the lights and I didn’t want to fire the coil if it wasn’t needed. This also allows me to keep track od the relay state so I can poll for it without changing it. This, coupled with MyMQTT for android basically recreates the other networked power strip that I built before, but I have to be running a server for this to work.
As should be expected the code is here.
The rest of this series can be gotten through from the home page here.