Rui, After countless hours of trying to get this switch code added to the sonoff basic, I am still unable to get it to work. I really need some guidance on the correct route to take with this effort.
he code attached is my latest feeble effort but all I have accomplished here is to get the button switch to turn the LED on/off. I can also turn the LED off and on from the Dashboard but the only debug message I get is the “on” message from the state message.
No matter if I click on the on button or the off button I always get the “on” state message. I also tried flashing the device with the tasmota firmware but I got completely confused by their different approach to the MQTT message structure. I would much prefer to use this firmware as I do understand it somewhat.
The use of the simple server on the RPi is easy and it works great. I would definitely prefer not to use any kind of Home Assistant software if I can help it.
The current code is here https://pastebin.com/Ptd6xYd9
Originally I may have not been clear as to what my goal is here. I want to add a pushbutton switch to a sonoff basic that will allow it to be turned on and off manually.
However, this has to work in conjunction with a motion sensor running the same switch. If the light is on, I want the switch to turn it off. If the light is off I want the switch to turn it on. As I said before. I am using the code for the sonoff that was part of one of you tutorials as well as the server the you devised using the RPi.
I have that all working with several sonoff switches in the house all independent from the Itead servers. I really don’t want to persue the HA route of the tasmota route as your system that I have working now is easy to maintain and understand.
I am not asking you too write the code but I would like some guidance as to where to add the various conditional statements. I am having too much fun with this stuff to give up on it.
A year ago I wasn’t sure how to spell ARDWINO. If it weren’t for your home automation course I still wouldn’t know how.
Thanks for your assistance
Charles
Thank you for your patience Charles! I definitely hope I can help, but it’s hard for me to debug custom code without trying (is like guessing and I might point you in the wrong direction). I just need one more details.
- To sum up, you want 1 SONOFF with 1 pushbutton. The SONOFF must turn on/off whether you use the physical pushbutton or the Node-RED dashboard. Right?
- Where’s your PIR connected to?
Rui, You have the requirements exactly correct.
Essentially I want to restore the function of the button as it was on the device. I will add wires to the new switch and mount it in the wall plate, Then the sonoff can be mounted behind it in the box.
The sonoff itself uses your code from the home automation course. I realize that I need to read the current state of both the switch and the MQTT message, store the value and then evaluate them depending on which one changed. But I just can’t seem to wrap my head around that in the right order.
The motion sensor is a modified version of your tutorial with the PIR, LDR and Temp device. I use an RCWL0516 for the sensor with an LDR so that it only operates at night.
I have built an interface to mount the sensor, a 5v -3.3v device and an ESP826601. The interface also has a connector for the FTDI unit and two other two pin connectors one to use for flashing and the other for power from the external power source. I am working on having that interface PCB made by OSHPARK. I tried that service with your PIR, LDR, TEMP unit and it worked very well. I printed the enclosure on my 3D printer, also new to me.
Thank you for your help
Charles
Photo here https://imgur.com/a/sWiWCf8
By default the built-in SONOFF button does not turn on/off anything. It’s just a button that you need press to configure the SONOFF with the default app.
In fact, for safety reasons you shouldn’t even use that button for normal on/off usage… The SONOFF should be placed/attached to a secure place to be properly isolated.
I’ve finally had time to run your sketch. Basically, you added the MQTT publish message in the wrong place. I’ve fixed it (it should be in the setup() when the button changes state).
Here’s the code that should work for you and for your exact needs: https://gist.github.com/RuiSantosdotme/fb7505ccf873e4e6c1ed25dc16fb4856
If the button is working in the opposite, you might need to swap line 170 with line 176.
Let me know if that example works for you!
Thanks for your patience!
Rui,
Thank you so much for your assistance. The system is not quite there yet as I have discovered some other pitfalls in the operation. I have moved the external switch to GPIO14 since it is readily available on the flashing connector. If I have to use the newer version , I will try the IO2 pad for those.
Currently I have GPIO14 pulled down with a 10k resistor and it connects to 3.3v when pushed. Here are the conditions as they stand at the moment. If I operate the device with the pushbutton, I can never get the output lamp to be on at the same time as the LED. I tried reversing the logic at the two statements that you pointed out, but that did not make any noticeable change. Pushing the pushbutton causes the load lamp to go on and off with each single push. Again the LED is lit opposite the lamp.
To me , this is of no concern because I will not be able to see it anyway. The only trouble with that is, I will not be able use that voltage to run a “Status” LED in the switch that will be mounted in the wall plate. I could probably use the relay voltage to run that led.
My next thought then would be to use a push on/push off switch instead of a momentary switch. When I tried that I encountered another issue. If I hold the switch down, the load lamp stays on until there happens to be a signal from the motion detector. As soon as the motion detector times out , the load goes off no matter if I am holding the switch or not. So it appears that I will have to give the manual switch some kind of authority over the entire operation. The motion sensor will serve to alert me of some intruder but I want the light to remain on, I will have to manually operate the switch and bypass any input from the sensor.
At any rate, you have given me some ideas to work on.
Regards
Charles
But is the relay turning on/off as expected? Does that wrong behavior only happen with the LED?
Thanks!
Rui’
Yes the relay is operating correctly. As I said , the LED will not make any difference as I will not be able to see it anyway. I plan to run a couple of extra wires to the relay coil to drive a Status LED in the switch.
Thank you again for the help