Re: Lots of Gadgets think I need a server.
Posted: Thu Jul 07, 2016 2:37 pm
Let's see if I can explain how this works, bit by bit:
This creates a two dimensional array. An array of arrays if you will. Each element of the $dev array is an array consisting of a device name, the IP of the ESP that handles that device, and the pin number controlling the device. I use an array like this because it is easy to add/delete/modify devices in the list without knowing how to code PHP.
This sets up a loop to go through each device definition in the $dev array one at a time.
This line does a lot! It starts with the name of the device being examined by the loop ($dev[$k]['name]). It then attempts to pull from the command line sent to your server whatever value has been passed for that variable name. So it you passed, http://{ip_address}/{this_script}.php?Light1=1 then $cmd will be set to "1" IF the device entry we are looking at is Light1 - otherwise, $cmd will be blank. So this both looks for a device name and retrieves the value for it.
If we haven't gotten to the "Light1" entry yet, then nothing happens and we loop past. If the device name was found, a 0, 1, or r is returned in $cmd and the remaining code executes:
Start constructing a URL to address the correct ESP for this device. The IP address comes from that device's array entry within the $dev array. So now we have http://{ESP_ip}/msg=? - We still need to add arguments to the URL.
If the command (value returned for the device) is not an "r" (ie. 0 or 1 command to change device state) then we add pin={pin_number}& to the URL. {pin_number} is taken from the device definition in the $dev array. Notes: ".=" is an append operator in PHP. We add the "&" in anticipation of the next argument added below.
Now we add stat=x where x is 0, 1, or r, depending on what the command is for the device. We end up with a URL like, http://192.168.0.20/msg?pin=1&stat=0 or http://192.168.0.20/msg?pin=1&stat=1 or http://192.168.0.20/msg?stat=r
CURL is a tool that fetches data from a URL, just like your browser. This code creates a CURL instance, calls the URL, and places the output in $output. This effectively sends a message to the right ESP with a pin number and state. When the ESP handles the pin, it sends any output desired back to CURL which relays it to $output.
Finally, we send the output from the ESP back to the calling browser.
This just closes the "if" and "loop"
It'll work.
If you name this script "index.php" then you should be able to access your devices with a browser command like, http://{server_ip}/?Light1=1
Code: Select all
$dev=array(
array('name'=>'Gdoor', 'esp_ip'=>'192.168.1.133', 'pin'=>3 ),
array('name'=>'ACunit', 'esp_ip'=>'192.168.1.133', 'pin'=>1 ),
array('name'=>'Light1', 'esp_ip'=>'192.168.1.134', 'pin'=>1 )
);
This creates a two dimensional array. An array of arrays if you will. Each element of the $dev array is an array consisting of a device name, the IP of the ESP that handles that device, and the pin number controlling the device. I use an array like this because it is easy to add/delete/modify devices in the list without knowing how to code PHP.
Code: Select all
foreach ($dev as $k=>$v) {
This sets up a loop to go through each device definition in the $dev array one at a time.
Code: Select all
$cmd=$_REQUEST[$dev[$k]['name']];
This line does a lot! It starts with the name of the device being examined by the loop ($dev[$k]['name]). It then attempts to pull from the command line sent to your server whatever value has been passed for that variable name. So it you passed, http://{ip_address}/{this_script}.php?Light1=1 then $cmd will be set to "1" IF the device entry we are looking at is Light1 - otherwise, $cmd will be blank. So this both looks for a device name and retrieves the value for it.
Code: Select all
if ($cmd!='') {
If we haven't gotten to the "Light1" entry yet, then nothing happens and we loop past. If the device name was found, a 0, 1, or r is returned in $cmd and the remaining code executes:
Code: Select all
$url="http://" . $dev[$k]['esp_ip'] . "/msg?";
Start constructing a URL to address the correct ESP for this device. The IP address comes from that device's array entry within the $dev array. So now we have http://{ESP_ip}/msg=? - We still need to add arguments to the URL.
Code: Select all
if ($cmd!='r') $url .= "pin=" . $dev[$k]['pin'] . "&";
If the command (value returned for the device) is not an "r" (ie. 0 or 1 command to change device state) then we add pin={pin_number}& to the URL. {pin_number} is taken from the device definition in the $dev array. Notes: ".=" is an append operator in PHP. We add the "&" in anticipation of the next argument added below.
Code: Select all
$url .= "stat=" . $cmd;
Now we add stat=x where x is 0, 1, or r, depending on what the command is for the device. We end up with a URL like, http://192.168.0.20/msg?pin=1&stat=0 or http://192.168.0.20/msg?pin=1&stat=1 or http://192.168.0.20/msg?stat=r
Code: Select all
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
CURL is a tool that fetches data from a URL, just like your browser. This code creates a CURL instance, calls the URL, and places the output in $output. This effectively sends a message to the right ESP with a pin number and state. When the ESP handles the pin, it sends any output desired back to CURL which relays it to $output.
Code: Select all
print $output;
Finally, we send the output from the ESP back to the calling browser.
Code: Select all
}
}
This just closes the "if" and "loop"
It'll work.
If you name this script "index.php" then you should be able to access your devices with a browser command like, http://{server_ip}/?Light1=1