Skip to content

Instantly share code, notes, and snippets.

@fhorlaville
Last active January 14, 2020 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save fhorlaville/747ea36d81ec5b6cd4de to your computer and use it in GitHub Desktop.
Save fhorlaville/747ea36d81ec5b6cd4de to your computer and use it in GitHub Desktop.
Robot Electronics ETH008 PHP controlling code - moved to https://github.com/fhorlaville/ETH008

This is my PHP library for controlling a Robot Electronics ETH008 unit. This unit has eight programmable relays and an Ethernet connector.

Here are the files:

  • functions.php This is the library with the functions you need to call

  • tests.php These are the unit tests for the functions library ; it shows how you use the functions

  • index.html This is a sample page with two buttons asynchronously controlling four relays.

  • ajax.php This is the back end for the sample. Button 1 turns on relays 1,7,8 (blue, yellow, green) Button 2 turns on relays 1,4,8 (blue, red, green)

<?php
require_once("functions.php");
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.
$config = array(
'blue'=> 1,
'red'=> 4,
'yellow'=> 7,
'green'=> 8
);
$gDebug=false;
$bdc=new bdc();
$button1Duration=get('button1');
$button2Duration=get('button2');
$button1On=get('button1on');
$button1Off=get('button1off');
$button2On=get('button2on');
$button2Off=get('button2off');
$allOff=get('alloff');
if (!setup("192.168.1.1", "17494")) {
returnError('setup');
exit();
}
function get($parm) {
$parmOutput="";
if (isset($_GET[$parm])) { $parmOutput = $_GET[$parm];}
return addslashes(htmlentities($parmOutput, ENT_QUOTES, "UTF-8"));
}
function firstSet($turnOn = 0, $duration = 0)
{
global $config;
return !setMultiple(array(
[$config['blue'], $turnOn, $duration],
[$config['yellow'], $turnOn, $duration],
[$config['green'], $turnOn, $duration]
));
}
function secondSet($turnOn = 0, $duration = 0)
{
global $config;
return !setMultiple(array(
[$config['blue'], $turnOn, $duration],
[$config['red'], $turnOn, $duration],
[$config['green'], $turnOn, $duration]
));
}
if ($button1Duration) {
if(firstSet(1, $button1Duration)){
returnOK('firstSet');
} else {
returnError('firstSet');
}
}
if ($button2Duration) {
if(secondSet(1, $button2Duration)){
returnOK('secondSet');
} else {
returnError('secondSet');
}
}
if ($button1On) {
if(firstSet(1, 0)){
returnOK('firstSetOn');
} else {
returnError('firstSetOn');
}
}
if ($button1Off) {
if (firstSet(0, 0)) {
returnOK('firstSetOff');
} else {
returnError('firstSetOff');
}
}
if ($button2On) {
if(secondSet(1, 0)){
returnOK('secondSetOn');
} else {
returnError('secondSetOn');
}
}
if ($button2Off) {
if(secondSet(0, 0))
{
returnOK('secondSetOff');
} else {
returnError('secondSetOff');
}
}
if ($allOff) {
if (!setRelays('00000000')) {
returnOK('allOff');
} else {
returnError('allOff');
}
}
//--- functions
function returnError($msg="")
{
header("Status: 400 ERROR", 400);
echo "Error:".$msg . "<br>";;
exit();
}
function returnOK($msg="")
{
global $gDebug;
if($gDebug) {
global $button1Duration, $button2Duration, $button1On, $button1Off, $button2On, $button2Off, $allOff;
echo "action:".$msg . "<br>";
echo "button1:" . $button1Duration . '<br>';
echo "button2:" . $button2Duration . '<br>';
echo "button1on:" . $button1On . '<br>';
echo "button1off:" . $button1Off . '<br>';
echo "button2on:" . $button2On . '<br>';
echo "button2off:" . $button2Off . '<br>';
echo "alloff:" . $allOff . '<br>';
echo "Status:" . getRelays();
} else {
return(header("Status: 200 OK", 200));
}
exit();
}
<?php
/*
* PHP code to manipulate a Robot Electronics ETH008 relay box
* http://www.robot-electronics.co.uk/products/relay-modules/ethernet-relay/eth008-8-x-16a-ethernet-relay.html
*/
$g_ip_address = "";
$g_port = "";
$g_timeout = 1;
define("C_ACTIVATE", 32);
define("C_DEACTIVATE", 33);
define("C_SET_OUTPUTS", 35);
define("C_GET_OUTPUTS", 36);
define("C_SET_STRING", 58);
function testConnect($ip_address="", $port="", $timeout=""){
$fp = openConnection($ip_address, $port, $timeout);
if (!$fp) {
return false;
} else {
closeConnection($fp);
return true;
}
}
function openConnection($ip_address="", $port="", $timeout=""){
global $g_ip_address, $g_port, $g_timeout;
if($ip_address == "") { $ip_address = $g_ip_address; }
if($port == "") { $port = $g_port; }
if($timeout == "") { $timeout = $g_timeout; }
try {
$fp = fsockopen($ip_address, $port, $errno, $errstr, $timeout);
if (!$fp) {
return false;
} else {
return $fp;
}
} catch (Exception $e) {
//echo 'Caught exception: ', $e->getMessage(), "\n";
return false;
}
}
function closeConnection($fp){
if($fp) {
fclose($fp);
}
}
function setup($ip_address = "192.168.0.100", $port = 17494, $timeout=1) {
global $g_ip_address, $g_port, $g_timeout;
$g_ip_address = $ip_address;
$g_port = $port;
$g_timeout = $timeout;
return($g_ip_address == $ip_address && $g_port == $port && $g_timeout == $timeout);
}
function message($command, $param1=null, $param2=null)
{
$msg = prepareMsg($command, $param1, $param2);
return sendMsg($msg);
}
function prepareMsg($command, $param1, $param2)
{
if (isset($param2)){
$msg = pack("CCC", $command, $param1, $param2);
} elseif (isset($param1)) {
$msg = pack("CC", $command, $param1);
} else {
$msg = pack("C", $command);
}
return $msg;
}
function sendMsg($msg)
{
$fp = openConnection();
if ($fp) {
fwrite($fp, $msg);
$result = ord(fread($fp, 1));
closeConnection($fp);
return ($result);
} else return false;
}
function turnOn($relay, $duration=0){
return message(C_ACTIVATE,$relay, $duration); //0 for success, 1 for failure
}
function turnOff($relay, $duration=0){
return message(C_DEACTIVATE,$relay, $duration); //0 for success, 1 for failure
}
function setRelays($relayByte=0){
return message(C_SET_OUTPUTS,bindec($relayByte)); //0 for success, 1 for failure
}
function getRelays(){
return decbin(message(C_GET_OUTPUTS)); //string with current relay states
}
function setRelayString($config){
return sendMsg($config); //0 for success, 1 for failure
}
function setMultiple($relayArray){
$result=0;
foreach($relayArray as $relay){
$result+=message(($relay[1]==1? C_ACTIVATE: C_DEACTIVATE) ,$relay[0], $relay[2]);
}
return ($result>0 ? 1 : 0); //0 for success, 1 for failure
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Buttons Test</title>
</head>
<body>
<h3>Web buttons</h3>
<input type="button" value="One 15s" onclick="activate('button1', 150);" />
<input type="button" value="Two 15s" onclick="activate('button2', 150);" />
<hr>
<h3>Testing</h3>
<input type="button" value="One - ON" onclick="activate('button1on', 1);" />
<input type="button" value="One - OFF" onclick="activate('button1off', 1);" />
<br>
<input type="button" value="Two - ON" onclick="activate('button2on', 1);" />
<input type="button" value="Two - OFF" onclick="activate('button2off', 1);" />
<br>
<input type="button" value="ALL OFF" onclick="activate('alloff', 1);" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
function activate(params, duration) {
var request = jQuery.ajax({
url: '/ajax.php?'+ params + '=' + duration,
type: "GET",
dataType: "html"
});
//
// provide feedback if needed
//
// request.done(function(msg) {
// alert( "done: " + msg );
// });
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
</script>
</body>
</html>
<?php
include_once("functions.php");
class testFunctions extends PHPUnit_Framework_TestCase {
// Relays are numbered 87654321
public function setUp()
{
setup();
}
public function testSetup(){
$this->assertTrue(setup(), "default setup");
$this->assertTrue(testConnect(), "default connect");
$this->assertTrue(setup("192.168.12.252"));
$this->assertTrue(testConnect(), "default connect");
$this->assertTrue(setup("192.168.12.253"));
$this->assertFalse(testConnect(), "default connect to wrong ip");
$this->assertTrue(setup("192.168.12.252", 10000));
$this->assertFalse(testConnect(), "default connect to wrong port");
$this->assertTrue(setup("192.168.12.252", 17494));
$this->assertTrue(testConnect(), "default connect to correct port");
}
public function testConnection(){
$this->assertTrue(testConnect("192.168.12.252"), "connect by ip");
$this->assertTrue(testConnect("192.168.12.252", 17494), "connect by ip and port");
$this->assertFalse(testConnect("192.168.12.253", 17494), "connect wrong ip");
$this->assertFalse(testConnect("192.168.12.252", 17495), "connect wrong port");
$this->assertFalse(testConnect("192.168.12.253", 17495), "connect wrong ip and port");
}
public function testAllOn(){
$this->assertEquals(0, setRelays('11111111'));
$this->assertEquals("11111111", getRelays());
sleep(3);
}
public function testSetRelays(){
$this->assertEquals(0, setRelays('10101010'));
$this->assertEquals("10101010", getRelays());
sleep(3);
}
public function testMultiple(){
$this->assertEquals(0, setRelays('00000000'));
$this->assertEquals(0, setMultiple([ [1,1,50],[2,1,50],[7,1,50],[8,1,50] ]));
$this->assertEquals("11000011", getRelays());
sleep(7);
$this->assertEquals("00000000", getRelays());
}
public function testMultipleWithTimes(){
$this->assertEquals(0, setRelays('00000000'));
$this->assertEquals(0, setMultiple([ [1,1,10],[2,1,20],[7,1,50],[8,1,50] ]));
$this->assertEquals("11000011", getRelays());
sleep(7);
$this->assertEquals("00000000", getRelays());
}
public function testSingleOnWithDuration(){
$this->assertEquals(0, setRelays('00000000'));
$this->assertEquals(0, turnOn(1,10));
$this->assertEquals("00000001", getRelays());
sleep(2);
$this->assertEquals("00000000", getRelays());
}
public function testSingleOnWithoutDuration(){
$this->assertEquals(0, setRelays('00000000'));
$this->assertEquals(0, turnOn(1,0));
$this->assertEquals("00000001", getRelays());
sleep(2);
$this->assertEquals("00000001", getRelays());
sleep(2);
$this->assertEquals(0, setRelays('00000000'));
}
public function testMultipleOnOffs(){
$this->assertEquals(0, setRelays('11111111'));
$this->assertEquals(0, setMultiple([ [1,0,10],[2,1,20],[7,0,50],[8,1,50] ]));
$this->assertEquals("10111110", getRelays());
sleep(7);
$this->assertEquals("01111101", getRelays());
}
public function testAllOff(){
$this->assertEquals(0, setRelays('00000000'));
$this->assertEquals("00000000", getRelays());
}
// public function testSetStringRelays(){
// $this->assertEquals(0, setRelays('00000000'));
// $this->assertEquals(0, setRelayString(':DOA,2,30 '));
// $this->assertEquals("00000010", getRelays());
// }
}
?>
(
@girub
Copy link

girub commented Feb 15, 2017

Hello I found your fantastic script. the model referenced by I see it has 8 relays how do I want to use all 8? you can then add other buttons also control the other by the same method?

you went by the script attached to this page?
http://www.robot-electronics.co.uk/products/relay-modules/ethernet-relay/eth008-8-x-16a-ethernet-relay.html

grazie mille e sorry for my english
giuseppe

@fhorlaville
Copy link
Author

fhorlaville commented Feb 16, 2017

Hi Giuseppe,

I started with their script yes, and made the general version in functions.php

You can see how to use the functions with test.php

index.html and ajax.php are a sample use case using 4 of the 8 relays, but you can expand to all 8 by adding to this array:

$config = array(
'blue'=> 1,
'red'=> 4,
'yellow'=> 7,
'green'=> 8
);

Here we are using relays 1, 4, 7, 8 but you can add 'purple' => 2, 'orange'=>3 etc.

Tell me if you need more help

Franck Horlaville
TAM Software

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment