General area when it fits no where else

Moderator: Mmiscool

User avatar
By livetv
#50371 Let's see if I can explain how this works, bit by bit:

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 allforeach ($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
User avatar
By forlotto
#50382 Man this is excellent I think I am understanding PHP now to some degree obviously there is lots of documentation on PHP but this is even sweeter imho the best way to learn code is how you did it right there this post will be useful to anyone who reads it ;) beautifully done in an ELI5 manner ....

Thank you for taking the time out of your day to help some random fella you do not know it means a lot to see people go to this length now I don't expect it but when I see an answer done this well much like mmiscool was able to help me get started and still aids me on certain things I feel this kind of thing deserves credit.

This is what forums used to be ahhh some 10 12 years ago people helping people not just canned responses as if you were doing this as a job at some service center.

If anyone see's this post make note and remember be excellent to livetv!

Take Care this is much appreciated!

-forlotto