TIP & TRICKS

You can find an example of all this tip&tricks implementations into the Php Controller package.

DIRECTORIES

The Php Controller package consists of the following folders and files:

VARIABLES

Use php session variables to store, manage and share process data in your project

// WRITE YOUR PHP SCRIPT TO LOOP if ($_SESSION['safetySensor'] == 1){ $_SESSION['motorLeft'] = 0; } elseif ($_SESSION['motorEnable'] == 1) { $_SESSION['motorLeft'] = 1; } else { $_SESSION['motorLeft'] = 0; }

PAGES

The suggested technique to browse between pages is to reload the same index.php page and passing the php variable $module, using the HTTP method GET.

The following code inside the file index.php is an example of a Php Controller menu, composed by four buttons which make load differents page modules as appropriate.

// Menu echo '<div> <button onclick="window.location.href=\''.$httpPath.'/index.php?module=start\'">Start Session</button> <button onclick="window.location.href=\''.$httpPath.'/index.php?module=save\'">Save Session</button> <button onclick="window.location.href=\''.$httpPath.'/pages/manualSessionDestroy.php\'">Emergency stop</button> <button onclick="window.location.href=\''.$httpPath.'/index.php?module=websocket\'">Websocket</button> </div>';

After, always into the file index.php, you need to develop a modular content in this way

// Pages if ($module == 'none'){ require ('pages/welcome.php'); } if ($module == 'start'){ echo '<div id="stats"><script>refreshStats()</script></div>'; echo '<div id="status"><script>refreshStats()</script></div>'; } if ($module == 'websocket'){ echo '<div id="message"></div>'; }

In order to load a specific content (or also a specific php script) according to the required page.

NOTE - don't worry if you don't see any line like $module = $_GET['module']: this command is contained into the file ./script/init/indexRequirements.php, called into the page header.

DATA REFRESH

via jQUERY & AJAX REQUESTS

To refresh a div content in your graphical interface, automatically every X milliseconds, consider to implement the following basis code inside the file ./scripts/custom/headers/headers.php

if ($module == 'start'){ echo "<script> function refreshStats(){ $('#stats').load('".$_SESSION['root_http']."/scripts/plc/plc_showingStats.php'); $('#status').load('".$_SESSION['root_http']."/scripts/plc/plc_showingStatusMod.php'); } setInterval(refreshStats, ".$_SESSION['statistics']."); </script>"; }

Using the php function echo you can send the javascript code to the client browser, which will require the new content to the server every refreshing clock. The value of this timing is stored into the session varable $_SESSION['statistics'], which is configured by editing the parameter statistics into the file ./scripts/custom/system/initData.ini. In this example, the function refreshStats() will update the content inside the divs with ID stats and status in the page index.php.

To complete the implementation you need to add and customize the following lines into the index.php file

echo "if ($module == 'start'){ echo '<div id="stats"><script>refreshStats()</script></div>'; echo '<div id="status"><script>refreshStats()</script></div>'; }";

Now consider to have a slider in your interface which sends to the server the new value for the variable $_SESSION['var1'], when the status of the slider is changed by user.

You can implement the previuos software architecture using and customizing the following code, which is contained into the file ./scripts/custom/work/ajaxRequest.php

// Data sending via AJAX request from client browser to the web server echo '<script type="text/javascript"> function changeValue(var, value) { $.ajax({ type: "GET", url: "'.$_SESSION['root_http'].'/scripts/custom/work/changeValue.php?var=" + var + "&value=" + value, async: false, data: {id:id}, success: function() { $("#led").load(location.href + " #led > *"); } }); return false; } </script> <form method="POST"> <input type="range" name="slider" value="'.$_SESSION['var1'].'" step="0.1" onChange="changeValue(\'var1\', this.value)\" min=\"0\" max=\"100\"/> </form>';

As you can see, when the user changes the slider value, his browser calls the function changeValue(var, value), passing the name of the variable and its new value. These data are sent to the server via AJAX request, running the script ./scripts/custom/work/changeValue.php which acquires the updated data and store in its session variable.

If the AJAX request ends correctly, the content inside the div with ID led is refreshed with the last data values. In the previous image you can see that the LED turns green.

AUTO SHUTDOWN
& AUTO START

Maybe your application requires Php Controller to work continuously, 24 hours a day 7 days a week.

In this case (but this depends on how much resources your application needs) you must consider to set a managed autorestart, in order to free the memory and continue to get good performance from your system, because it's possible that custom scripts can allocate more and more memory day by day. We're speaking about a managed autorestart because we must be sure that Php Controller automatically stops its elaboration and closes the working session correctly before platform restarting and, during reboot, it must be started automatically in order to continue its elaboration.

In other cases you may simply desire to automatically start Php Controller at system startup, or define an automatic shutdown of your platform at a predetermined time.

In all the cases you need to configure your platform as follows.

Edit the file crontab

sudo crontab -e

At the end of the file add these lines

0 2 * * * sudo -H -u www-data php /var/www/phpcontroller/scripts/autorun/stopController.php /var/www/phpcontroller 1 2 * * * sudo reboot

Where 0 2 * * * means "execute the following command at 02:00 AM o'clock, every day, every month, every year" and 1 2 * * * means "execute the following command at 02:01 AM o'clock, every day, every month, every year". In this way you can correctly stop the controller and make an automatic system shutdown.

Pay attention to write the right path of your project.

Now edit the file rc.local

sudo nano /etc/rc.local

and add these line in order to automatically start Php Controller at system startup

sudo -H -u www-data php /var/www/phpcontroller/scripts/autorun/startController.php /var/www/phpcontroller

Also in this case pay attention to write the right path of your project.

That's all!