This project span out of request, doubt and why not running a PHP framework on microcontrollers. After researching on the internet about possibility of getting a PHP software to run directly on micro-controllers like its counterparts - Python (Micropyhton, PyMite, PyonChip, Pumbaa, etc.), JavaScript (Espruino, Mjs, etc...), Basic Language (GreatCow, B4M, RfoBasic, PicBasic, etc.) and others. Even Forth, Rust, Go, Pawn, Falcon, TinyC, MicroPascal are some of the example of portable main languages into the small memory device platforms.
Why is PHP on MCU difficult? The answer here is that PHP itself can be view as a transpiller, a translator or a code generator. PHP takes your scripted code as an input/source language formatted e.g.:
<?php echo "I am a Php script"; ?>
It then parses the code, generates an output in HTML format before sending it back to a webserver which then send the result back to a browser. So as you can see, PHP is a middleware interpreter that sits just underneath the webserver to act upon a special formatted server side web scripts.
Running a PHP on small memory devices, like ESP8266 for example, means you need a micro-webserver on that device, since PHP does not run on its own. Secondly, you need a CGI (Common Gateway Interface) protocol implemented for the Webserver <----> PHP communications (this is bidirectional) or inter-process communication or whatever used to communicate. But we will stick with web standard as much as possible, but there is no reason not to break or modify any of the standard or even create one from scratch.
I look into many attempts made on the internet, while the majority of them just have a webserver running remotely from PCs, there are two notable attempts that I came across. The first one: PHPoC at https://www.phpoc.com/ and https://github.com/phpoc?tab=overview&from=2016-12-01&to=2016-12-31. There is a Hackster community page here, as well, see: https://www.hackster.io/phpoc.
PHPoC is specially developed for a family of embedded hardware and gateways, so the firmware are not opensource but the libraries are. So this means that it is not opensource in the truest sense. Also since I am focusing on compilers and interpreters for embedded systems this year (2018), I have decided to go along in the way that I already planned as part of my 2018 resolution.
The second attempt that I came across was that of PH7 at http://ph7.symisc.net/ and https://github.com/symisc/PH7. After initial communication with the moderator, I found out that there is not much happening with PH7 and not much interest for it. PH7 had been amalgamated into two files of C source and header file. The source file merged is about 16 pages long. It is a hand-crafted PHP interpreter with a VM. PH7 while being developed for a router was targeted at Unix and Windows platforms since it was developed specifically for a product on Unix and Windows. A compiled Linux binary stood at around 500k - 800k without being striped so it means it can be striped down to about 300k - 500k in size.
PH7 grammar for its interpreter was also hand coded directly and not EBNF (Extended Backus-Naur Form) friendly, which I believe led to it not being maintained any longer, no friendly adoption or people working or using it for other purposes.
Well I went the way of PH7 since it is written in C language. I use C language myself. After playing around with its source code, I was able to get it to compile on my Ubuntu box. Next I was able to compile it for an STM32F4VET6 Black, which is an ARM Cortex M4 which is a 512K flash with 192+4 RAM. The steps raise the hope and motivation along the path of modifying PH7 and to server as a jump-start for this adventure.
After couple of weeks in an on/off mode, I finally got back to it and started to add a webserver while porting its interpreter to EBNF. There are other educational attempt of this at IFJ Czech proofing that this is best to go.
Screenshots below show my first attempt on an Ubuntu Linux box where PH7 is running with a Mongoose webserver: (https://mongoose-os.com/)
Out of curiosity I tested some of the PHPoc library just to see if PH7 will fail or not?
Surprisingly PH7 was able to parse all of the PHPoC scripts. This is a proof of concept showing that it is possible to re-purpose PHP language for embedded platform. Please stay tuned in for more and for real embedded platforms testing.
In our next series:
- In Part-2 we will move to the stage of getting PH7 to run on ESP8266 family.
- In Part-3 we will explore the STM32F4xxxx family.
- In Part-4 We will be working on PIC from Microchip.
- In Part-5, we will be going through some applications that could benefits from PHP4MCU.
I will be providing a Github repository for all code, attributions, credits and collaborations. Please stay tuned in and follow.
God bless!
Best regards,
Sanyaade
Code for Part-1:
Comments
Please log in or sign up to comment.