Thanks ErnieAlex; Just need a few optimisations now

[php]<?php

Get state from module and set required variables

define(DEST, “192.168.0.100”); # wifly IP
define(PORT, “2000”); # wifly Port

function hexbin($value) {
return(decbin(hexdec($value)));
}

function wiflyCommand($commands) {
$script = 'log_user 0; spawn telnet '.DEST.' '.PORT.'; expect -exact "*HELLO*"; send -- "\$\$\$\r"; expect -exact "CMD\r"; '.$commands.'; expect -exact "> "; send_user "$expect_out(buffer)"; send -- "close\r"; expect eof;';
#print("Start = /usr/bin/expect -c ".$script);
exec("/usr/bin/expect -c '".$script."'", $stdout);
return($stdout);

}

$GPIOHex = wiflyCommand(‘send – “show io\r”;’);
$GPIODec = hexdec($GPIOHex[4]);

$bit = array();
for ($i = 0; $i <= 14; $i++) {
$bit[$i]=0;
}

$bit[‘0’]=(($GPIODec>>0)&1); # checks if each bit in the GPIOBin word is set to 1 or 0
$bit[‘1’]=(($GPIODec>>1)&1); # by shifting the bit to the right by the number of the bit
$bit[‘2’]=(($GPIODec>>2)&1); # this puts the bit we cant to check in the bit 0 position
$bit[‘3’]=(($GPIODec>>3)&1); # we then &1 to check if the bit is 1, 1 &1 = 1, 0 &1 = 0
$bit[‘4’]=(($GPIODec>>4)&1); # this value is then saves as a variable and is updated
$bit[‘5’]=(($GPIODec>>5)&1); # when the page is refreshed
$bit[‘6’]=(($GPIODec>>6)&1);
$bit[‘7’]=(($GPIODec>>7)&1);
$bit[‘8’]=(($GPIODec>>8)&1);
$bit[‘9’]=(($GPIODec>>9)&1);
$bit[‘10’]=(($GPIODec>>10)&1); # bits 10-13 are inacessible through the wifi module we will diplay the
$bit[‘11’]=(($GPIODec>>11)&1); # current state however changing it has no effect
$bit[‘12’]=(($GPIODec>>12)&1);
$bit[‘13’]=(($GPIODec>>13)&1);
$bit[‘14’]=(($GPIODec>>14)&1);

function GPIO14() {
global $bit;
if($bit[‘14’]==0){
wiflyCommand(‘send – “set sys output 0x4000 0x4000\r”’);
}else{
wiflyCommand(‘send – “set sys output 0x0000 0x4000\r”’);
}
}
function GPIO1() {
global $bit;
if($bit[‘1’]==0){
wiflyCommand(‘send – “set sys output 0x0002 0x0002\r”’);
}else{
wiflyCommand(‘send – “set sys output 0x0000 0x0002\r”’);
}
}

function GPIO2() {
global $bit;
if($bit[‘2’]==0){
wiflyCommand(‘send – “set sys output 0x0004 0x0004\r”’);
}else{
wiflyCommand(‘send – “set sys output 0x0000 0x0004\r”’);
}
}

function GPIO3() {
global $bit;
if($bit[‘3’]==0){
wiflyCommand(‘send – “set sys output 0x0008 0x0008\r”’);
}else{
wiflyCommand(‘send – “set sys output 0x0000 0x0008\r”’);
}
}

function GPIO4() {
global $bit;
if($bit[‘4’]==0){
wiflyCommand(‘send – “set sys output 0x0010 0x0010\r”’);
}else{
wiflyCommand(‘send – “set sys output 0x0000 0x0010\r”’);
}
}

function GPIO5() {
global $bit;
if($bit[‘5’]==0){
wiflyCommand(‘send – “set sys output 0x0020 0x0020\r”’);
}else{
wiflyCommand(‘send – “set sys output 0x0000 0x0020\r”’);
}
}

function GPIO6() {
global $bit;
if($bit[‘6’]==0){
wiflyCommand(‘send – “set sys output 0x0040 0x0040\r”’);
}else{
wiflyCommand(‘send – “set sys output 0x0000 0x0040\r”’);
}
}

function GPIO7() {
global $bit;
if($bit[‘7’]==0){
wiflyCommand(‘send – “set sys output 0x0080 0x0080\r”’);
}else{
wiflyCommand(‘send – “set sys output 0x0000 0x0080\r”’);
}
}

function GPIO8() {
global $bit;
if($bit[‘8’]==0){
wiflyCommand(‘send – “set sys output 0x0100 0x0100\r”’);
}else{
wiflyCommand(‘send – “set sys output 0x0000 0x0100\r”’);
}
}

function GPIO9() {
global $bit;
if($bit[‘9’]==0){
wiflyCommand(‘send – “set sys output 0x0200 0x0200\r”’);
}else{
wiflyCommand(‘send – “set sys output 0x0000 0x0200\r”’);
}
}

if (isset($_POST[“GPIO”])){
$postF = ($_POST[“GPIO”]);
$postF();
}

print("<html><body>\n");
print("<form action=index.php method=POST>\n");

for ($i=0; $i<=14; $i++){
if ($bit[$i]==1){
print("
\n");
}else{
print("
\n");
}
}
print("\n");
print("\n");
?>[/php]

This is what I’ve got now and it works :smiley: But I was thinking there much be a way to optimise the 10 functions I have as they seem kind of messy atm. Thanks.

Sponsor our Newsletter | Privacy Policy | Terms of Service