For learning purposes I am looking to make 3 switches (preferably using a small board), each one being able to send a wireless command (ON) to a 4th main switch, which in turn after receiving it, will turn on a power relay. I also wish to have a timer set on that main switch (after receiving the ON command) e.g. for 60 minutes, and then turn OFF (the relay). When one of the 3 “peripheral” switches sends the ON command, I need them to turn on a LED light (one connected on each of the three boards).
Example: A button is pushed on “Board A”, the Led on the board turns ON (Board B and Board C LED’s turn on as well) and a command is sent over the air to the “Main Board”. The power relay stays on for 60 minutes.
If during these 60 minutes the button is pressed on Board B, the “Main Board” will disregard it. In other words: IF already on, and a command comes in from any of the 3 boards => do nothing.
The distance between the peripheral switches and the main one will be about 50 meters.
Would it be possible to give the turn on command from a remote location (and have the 3 LEDs turn on, to indicate "Main Switch" being ON) ?
I would really appreciate any thoughts on the hardware and general ideas for the code?
Do the perhipheral switches have to run on battery power, or can they be powered from a mains power supply (e.g. USB phone charger)?
Can the 3 peripheral switches be wired to each other, or do all 3 need wireless communication?
The 'Button pressed message' doesn't need any parameters.
Each remote switch would send it once each time the button state changed from off to on.
The main switch wouldn't need to know which remote switch sent the button pressed message
The 'LED control message' would need a parameter to indicate whether to turn the LED on, or turn it off.
It could be broadcast to all 3 remote switches. Their LEDs don't need to be individually addressed.
Do my thoughts match with your requirements?
[Edit added: I don't have any experience of radio transmission other than WiFi and Bluetooth. Perhaps this link will help, or someone else reading this who does have experience may help]
I don't quite understand that. A diagram might help explain it.
Edit, added the following to explain what I'm not understanding:
I assume BT = bluetooth and you are thinking of using it to communicate for the messages in my digaram in post #4.
If you are using bluetooth for all the messages in that diagram, what is the WiFi from the main switch to the home router for? Is some other device going to communicate with the main switch?
Do the remote switches have access to the WiFi network? I guess not, otherwise you'd just use WiFi for everything and skip bluetooth.
Regarding the WiFi, I was thinking to implement it on the main switch in order to be able to turn the relay ON/OFF remotely, but if this complicates things a lot I can do a version with BT only.
They wouldn't need to. I mean they could, but I don't see a reason for them to access the WiFi network. I am mostly interested to physically operate these 3 switches to control the relay (through the main board/Main Switch) .
I'd use EPS32s because they are cheap and support BT + WiFi. I'd use WiFi for all the communications links because the some of the WiFi code would be common to all the processors, and I have much more experience of using ESP32 WiFi than BT. I see nothing wrong with your scheme. I don't know what range you'd get with ESP32 BT.
Thank you Dave! OK so 4 boards ESP32, that sounds great (e.g. XIAO ESP32C6?). As for the 3 boards, will they have (I assume) almost identical code to communicate with the "main board"? I am not sure if it matters but since I forgot to mention it before, the buttons will be momentary push buttons. Any further ideas on the code for the three peripherals and the main board?
Edit: I believe the WiFi distance will be covered fine, after all the area is pretty much signal free.
I always use "ESP32 Dev Kit C V4 (WROOM-32D)", but only because I got familiar with them, have quite a few, and didn't need to change. They are £6-£7 each on Amazon. You might get them cheaper on eBay. I've bought the cheap ones from China and not had any problems.
If they use WiFi and get their IP address from the router using DHCP, then I can't see a reason why exactly the same code can't be loaded on each. You may want to consider having a header on the messages they send to indicate which remote switch sent the message, just in case you ever need to know, e.g. for debugging.
I assumed that would be the case. Some simple switch debounce code would perhaps be worthwhile, to stop the remote switch trying to send multiple button pressed messages per actual button press.
Lots
You will need to cater for the router rebooting. So the WiFi code will have to check if it has a connection and attempt to reconnect if it doesn't.
A problem I have is when the power comes back on after a power cut, the ESP32s come up before the router. For some reason they get stuck in a loop trying to connect to WiFi even after the router comes back up. Power cuts are so rare that I've not investigated a fix. I just turn the ESP32s off and on again when the router is back up, and then they work fine.
I think the comms is 90+% of the programming complexity. The code to read the switch / control the LED should be trivial. The logic in the main switch shouldn't be that complicated using millis() for timing.
If I were you I'd get the comms working for one remote switch and the main switch first. Then check it works over the 50m distance.
I'm happy to answer further questions if you get stuck.
I think most of @Dave_Lowther answers cover pretty much the design. The peripheral boards don't need to communicate between them at all, just do two send/receive messages to the main board:
Send "My button was pushed" when a pressing happens.
Wait for a message "the light condition changed" message to update the local led.
Every other thing might run in the main board, receiving the messages from any N momentary push buttons (there's no need to limit the number, why should you?). Pressing any of the buttons will be the same to the board running program.
For the code you can use my own ButtonToSwitch_ESP32 library. The TmLtchMPBttn class (Time Latched Momentary Push Button) does exactly what you want.
You can configure it to ignore any presses until the time runs out or reset the timer to keep the light on for a renewd time period.
As for the fact that the library was written with a pin detected input, the code receiving the "turn on" message will have to generate the pin input, no big deal there.
[Edit: I'm sorry, re-reading I find some parts of my answer repeat @Dave_Lowther post #4, my excuses]
You're welcome... and now that I remember you said you're having main power for the remote boards, you might want to try do something like the HntdTmLtchMPBttn class of that same library, here you have the link to the simulated model...