arthur64 Posted August 20 Share Posted August 20 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 August 20 Share Posted August 20 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 August 20 Share Posted August 20 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 August 22 Share Posted August 22 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 August 22 Share Posted August 22 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...
arthur64 Posted 9 hours ago Author Share Posted 9 hours ago The basis of this work is: my hot tub is [older] and the control board is flaky(?). The mfg says: "We do not share schematics". OK, BITE ME! I'll roll my own. To whit there are several questions which all end with "and that is the question". A bit of >my< background. USAF Electronics School 1961, self taught programming on a DEC PDP-7 (lac, dac, isz,...), HP-3000 UNIX ... now LAMP. My philosophy is: every line of code is an invite to a problem - therefore 7000 lines - 7000 problems. 70 lines - 10x less problems. I have an Raspberry Pi - unused, so ... and a 8 port USB relay (amazon LCOS 8 port usb relay $12) to drive 30A relays where needed. So first: build a web page to control everything - wiring tbd. Code running in the background/crontab: take temperature on the minute and record in DB. If below low-set engage Circulation pump, insure Flow SW engaged, engage Heater, update database for status (Maria sql). At noon/midnight engage Pump1 on high, Blower for 3 minutes as a cleaning cycle. Etc, etc, etc (says the bald man). Button.php sends 1/0 to relay X and update the DB. And with that I have my web page 90% the way I (think) want it - what's the 90-10 / 10-90 rule? Following is my code - it's the way "I" roll. Go ahead and "roll" >your< eyes ... <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Cal's Hot Tub Control</title> <style> table { border: 1px solid black; table-layout: fixed; width: 90%; } th, td { border: 1px solid black; width: 33%; .default-button { background-color: #40FF40; color: black; padding: 10px 20px; font-size: 20px; border: none; border-radius: 20px; } .clicked-button { background-color: white; /* Green */ font-size: 20px; color: red; border-radius: 20px; } </style> <script> function htbutton (a) { var myval1 = a; d1 = document.createElement("script"); d1.src = "button.php?info="+myval1; d1.type = "text/javascript"; document.body.appendChild(d1); } </script> </head> <body bgcolor=#27ftf5> <table align=center> <?php include 'dbconnect.php'; $q1 = mysqli_query ($db1, "select htt from temps order by id desc limit 1;"); $r1 = mysqli_fetch_row ($q1); $temp = $r1[0]; echo "<tr><td align=center border=1>"; echo date("Y-m-d H:i:s"); echo "</td><td align=center><H1>HOTTUB</H1> <a href=index.php>refresh</a></td><td align=center>rightboxtbd</td><tr>\n<tr>\n"; echo "<tr>\n <td align=center>Temp Set<br>102.5<img src=updwn.png width=50px></td>\n <td align=center><H2>$temp</H2></td>\n <td align=center>rightboxtbd</td>\n <tr>\n"; echo "<tr><td align=center colspan=3>"; $list = array ("pump1", "pump2", "circ", "blower", "heater", "light"); foreach ($list as $l) { $x = mysqli_query ($db1, "RESET QUERY CACHE;"); $q1 = mysqli_query ($db1, "select status from info where name = '$l';"); echo "<button class=default-button id=$l onclick=htbutton('$l')>"; echo strtoupper($l); echo"</button>\n"; echo "<script> document.getElementById('$l').addEventListener('click', function() { this.classList.toggle('clicked-button'); // Toggles the class }); </script>"; } echo "<tr><td colspan=3>\n"; echo "for now a place holder for a phplot graph of water temp for the past 24 hrs"; echo "</td><tr>\n"; ?> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/330181-8-port-relay-control/#findComment-1659412 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.