arthur64 Posted Wednesday at 01:56 PM Share Posted Wednesday at 01:56 PM 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 Wednesday at 02:56 PM Share Posted Wednesday at 02:56 PM 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 Wednesday at 03:47 PM Share Posted Wednesday at 03:47 PM 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...
kicken Posted 3 hours ago Share Posted 3 hours ago AI tools are usually pretty good for getting started with a simple task like this. I googled the relay board you mentioned and found a PDF that lists the data required to turn the relays on or off. I feed that into ChatGPT and asked it to describe the protocol to get an understanding of it. I then asked it to generate a web form using PHP that can be used to turn the relays on or off. <?php // relay_control.php // === Function to build command === function relayCommand(int $relay, bool $state): string { $header = 0xA0; $relayByte = $relay & 0xFF; $stateByte = $state ? 0x01 : 0x00; $checksum = ($header + $relayByte + $stateByte) & 0xFF; return pack('C4', $header, $relayByte, $stateByte, $checksum); } // === Handle form submission === $message = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $relay = intval($_POST['relay'] ?? 1); $state = ($_POST['action'] === 'on'); $cmd = relayCommand($relay, $state); $hexCmd = strtoupper(bin2hex($cmd)); // Example: write to USB device (uncomment when you know the device path) // $fp = fopen('/dev/ttyUSB0', 'wb'); // if ($fp) { // fwrite($fp, $cmd); // fclose($fp); // } $message = "Relay $relay " . ($state ? 'ON' : 'OFF') . " → Command: $hexCmd"; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Relay Control</title> <style> body { font-family: Arial, sans-serif; padding: 20px; } form { margin: 20px 0; } select, button { font-size: 16px; padding: 5px 10px; margin: 5px; } .msg { margin-top: 15px; font-weight: bold; } </style> </head> <body> <h1>LCUS-8 Relay Control</h1> <form method="post"> <label for="relay">Relay number:</label> <select name="relay" id="relay"> <?php for ($i = 1; $i <= 8; $i++): ?> <option value="<?= $i ?>"><?= $i ?></option> <?php endfor; ?> </select> <button type="submit" name="action" value="on">Turn ON</button> <button type="submit" name="action" value="off">Turn OFF</button> </form> <?php if ($message): ?> <div class="msg"><?= htmlspecialchars($message) ?></div> <?php endif; ?> </body> </html> Sounds like a project that you could do with a raspberry pi computer + the relay board. Might be able to go even simpler if you didn't need to host the control interface on the device itself and instead just communicated with it over the network or a serial port. Quote Link to comment https://forums.phpfreaks.com/topic/330181-8-port-relay-control/#findComment-1658416 Share on other sites More sharing options...
gizmola Posted 3 hours ago Share Posted 3 hours ago 11 minutes ago, kicken said: Sounds like a project that you could do with a raspberry pi computer + the relay board. Might be able to go even simpler if you didn't need to host the control interface on the device itself and instead just communicated with it over the network or a serial port. That was the question originally posed as I interpreted it. This is why the OP posted interest in the ESP32, which is a line of 32 bit Microcontrollers. They are typically used for IOT projects, and I don't think are a good match for this. A Raspberry pi, Orange Pi, or even a GMTek Mini PC are all a lot more viable given the OP's professed direction in this project, involving an OS capable of running an HTTP server and app server. Quote Link to comment https://forums.phpfreaks.com/topic/330181-8-port-relay-control/#findComment-1658417 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.