arthur64 Posted 7 hours ago Share Posted 7 hours ago I'm starting a new project to replace the $700 hot tub controller board with a "Roll your own". Haven't decided on the CPU - ESP32-? or a less than $100 micro pc. As needed the relay board ( LCUS-8 USB Relay Module 8 Channel DC 5V ) will drive T9AS1D12 30A relays. Web page for display and control. Anyhow looking for how to control the relay board using PHP - Google wasn't any help. I found a 70+ line python script ( I dislike py ) so I used it as a base for a 19 line PHP script. #!/usr/bin/php <?php $on = array ("", "\xa0\x01\x01\xa2", "\xa0\x02\x01\xa3", "\xa0\x03\x01\xa4", "\xa0\x04\x01\xa5", "\xa0\x05\x01\xa6", "\xa0\x06\x01\xa7", "\xa0\x07\x01\xa8", "\xa0\x08\x01\xa9"); $off = array ("", "\xa0\x01\x00\xa1", "\xa0\x02\x00\xa2", "\xa0\x03\x00\xa3", "\xa0\x04\x00\xa4", "\xa0\x05\x00\xa5", "\xa0\x06\x00\xa6", "\xa0\x07\x00\xa7", "\xa0\x08\x00\xa8"); $relay = $argv[1]; $oo = $argv[2]; $fd = fopen ("/dev/ttyUSB0", "r+"); switch ($oo) { case 0: fprintf ($fd, "0x%s", $off[$relay]); break; case 1: fprintf ($fd, "0x%s", $on[$relay]); break; } fclose ($fd); Quote Link to comment https://forums.phpfreaks.com/topic/330181-8-port-relay-control/ Share on other sites More sharing options...
gizmola Posted 6 hours ago Share Posted 6 hours ago The script does no parameter count checking, which isn't a great idea. First thing you would want to do is check $argc == 3. I'm assuming you know that $argv[0] is the script name, so typically you would check the count and display a "usage {$argv[0]} relay# 0|1 [off|on]\n", or something similar if it's not the right count. With that said you stated you wanted to do this with an html page, so probably it would be better to just have a "self posting" form script where you use html to display the controls, and set the 2 parameters based on whatever UI decisions you make. In that case, you would want to get the 2 parameters via the $_POST[] superglobal array, perhaps with html form elements named $_POST['relay'] and $_POST['state'] which you can set to be 0 or 1. You certainly in either case want to do some bounds checking (cast to int or use intval) and insure that the relay is in the range of 0-7. It would also be good if there was a way to determine the pre-existing state of the system. With that said, given the circumscribed set of functions, and purpose of this, I would suggest that you consider the alternative of a board based controller, perhaps with an lcd keypad. With a "mini-pc" you have all the issues of how your web app is going to be displayed, although if your device has wifi, you could design the html/css to be responsive, and control it using a pc or phone. Sounds like a fun and interesting project. Quote Link to comment https://forums.phpfreaks.com/topic/330181-8-port-relay-control/#findComment-1658203 Share on other sites More sharing options...
requinix Posted 5 hours ago Share Posted 5 hours ago Did I miss where a question was asked? What was the source .py being copied here, in case that's necessary information? Input arguments are rather awkward, like gizmola said something like $_POST or even $_GET will probably be more appropriate. Obviously the exact nature of the $on/$off stuff is irrelevant to operating the board so it can be rewritten into any style. For the board, seems odd that you're actually supposed to include a "0x" in the buffer? Otherwise since this is a device, a mere file_put_contents is likely sufficient - w vs. r+ shouldn't matter. Quote Link to comment https://forums.phpfreaks.com/topic/330181-8-port-relay-control/#findComment-1658204 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.