- How PHPoC system run PHPoC files
- Which file is mandatory in PHPoC system?
- Which PHP file is run locally in PHPoC system loop
- Which PHP files is run in response to HTTP request from outside?
- How to force a PHP file to run in system loop?
- How to make PHP file not run in response to HTTP request
To create IoT devices with PHPoC, we write PHPoC script files and upload them to PHPoC devices. We can upload many files to PHPoC.
How PHPoC system run PHPoC files?
There are two ways PHPoC system run PHPoC script file.
- Run in infinite system loop
- Run in response to HTTP request
PHPoC files that are run in infinite system loop
Like other embedded systems, there is code that run on infinite loop to do some specific tasks. This kind of PHPoC files contain codes that run in infinite loop to do some of the following tasks:
- Reading value from sensors
- Controlling actuators
- Sending/receiving data through Internet via various network protocols
PHPoC files that are only run in response to HTTP request
This kind of file is web app files. Apart from PHPoC script, it may contains HTML, JavaScript and CSS code.
It is only run when there is an HTTP request.
If I create my_file.php files and upload it to PHPoC, how can I make it run in system loop or in response to HTTP request?
When you upload my_file.php to PHPoC:
- It can run when there is an HTTP request without doing anything. You can test it by typing: http://replace_phpoc_ip_addess/my_file.php on web browser to run this file.
- To make it run in system loop, you need to create init.php file with the following code to tell system that you want to run my_file.php in system loop:
PHP Code:
<?php // init.php
system("php my_file.php");
?>
What is init.php files?
init.php file is file that run when PHPoC devices is powered up or restarted.
It is mainly used to register which file is run in system loop.
Which file is mandatory
If the application includes code that need to run in system loop, the init.php file is mandatory. This file is run when PHPoC device is powered or reset. It is mainly used to tell PHPoC system run which files in system loop. For example, the following init.php file tell PHPoC system run file blink_led.php in system loop:
PHP Code:
<?php
system("php blink_led.php");
?>
If the application does NOT include code that need to run in system loop, no file is mandatory.
Which PHPoC file is run internally in PHPoC system loop?
The files, which is set in init.php file are run in system loop.
What is task0.php file?
It's just a frequently used name of PHPoC file run in PHPoC system loop. The name is not mandatory. You can change it to any name, a long as you specify it on init.php file.
Which PHPoC files is run in response to HTTP request from outside
Any files uploaded to PHPoC can be run in response to HTTP request. Assume that IP address of PHPoC device is 192.168.0.2, and there is an PHPoC files with name blink_led.php on PHPoC devices. If we open a web browser and type: 192.168.0.2/blink_led.php , blink_led.php file is run in response to request from web browser, regardless it is running on system loop or not. However, if a file is running in system loop and in response to HTTP request at the same time, it may get errors. Therefore, the PHPoC file running in system loop should not serve HTTP request.
What is index.php file?
It's just a frequently used name of PHPoC file run in response for HTTP request. If you type URL or IP address of PHPoC on Web browser without name of file, index.php file is run by default.
For example, you create two files on PHPoC: index.php and any_name.php. Suppose that IP address of PHPoC is 192.168.0.2.
If you want to access index.php from web browser, you can type:
- 192.168.0.2
- http://192.168.0.2
- http://192.168.0.2/index.php
- 192.168.0.2/any_name.php
- http://192.168.0.2/any_name.php
How to make PHPoC file refuse to serve HTTP request
It is very simple, just add the following code at the beginning of the files:
PHP Code:
if(_SERVER("REQUEST_METHOD"))
exit; // avoid php execution via http request
Is it possible that one file run both on system loop and in response to HTTP request?
This scenario occurs when a file is running on system loop but there is a request from web browser (HTTP request).
It's possible. However, in practice, It usually cause errors. Therefore, the PHPoC file running in system loop should not serve HTTP request.
How to make PHPoC file run in system loop
It just need to create init.php file and using "php" system command to tell system run that file . For example, If you want blink_led.php to run in system loop, you just need to create init.php file with the following code:
PHP Code:
<?php
system("php blink_led.php");
?>
References