The Blog

Conditionals in uPHP

Conditionals

Every programming language requires some form of conditional operator. The if operator in uPHP has the following syntax:

if (condition) {
  // your code if true
} else {
  // optional code if false
}

Note: Single if statements can be executed without curly braces. However, all else statements need to be enclosed in curly braces. The following code snippet illustrates different comparisons:

$x = 3;

// use == notation to compare without assigning.
if ($x == 3)	// this is a single line statement and does not require braces
  print("x is 3");
if ($x = 4) // this will always return true since x is assigned the value 4
  print("x is 4");

A more complex example shows the use of Boolean comparisons:

if (($x > 1) && ($x <=5)) || ($x == 7)) {
  print("x in range");
} else {
  print("x out of range");
}

Here, if x has a value between 2 and 5, or 7 then the expression will evaluate to true.

Comment (0) Hits: 2237

Introduction to uPHP

The scripts that run off the sd card perform all tasks necessary for the application to operate, such as data logging, device control, ui layout and access control. The scripts are interpreted and firmware functions are called to carry out the actual function implementation. uPHP functions take variables as parameters and optionally return a variable. This tutorial is not meant as a primer in programming so much as a quick introduction for existing PHP coders into the internals of the language.
All variables start with $ symbol. Variables are automatically type cast by default. uPHP variables can be of the following types: integer, float, string and array. Assigning a variable a value will determine what type the variable takes. For example:

$x = 1; // this will create an int variable $x with a value of 1
$y = 1.1; // this will create a float variable $y with a value of 1.1
$z = "1"; // this will create a string $z with the value of 1

Mathematical operations can be performed on these variables, and you can type cast any variable using intval, floatval and strval.

$total = floatval($x)+ $y; // $total is now a float of value 2.1
$inttotal = intval($y) + $x; // $inttotal is now 2

You can use expressions with various parts just as in PHP:

$x = (sin($y) / cos($y)) * (sqrt($z / 8.7));

Arrays

Working with arrays is simple. First, if an array doesn't exist, create it like this:

$arr = array(); // blank array
$arr[0] = 1;
$arr[1] = 9;
$arr[2] = 3;

Or

$arr2 = array(1,9,3); // array with values in one go

One difference between PHP is that you cannot use => to assign name/value pairs. Instead, access the values by index.

For hashes, just use whatever string you want as a key:

$arr["name"] = "tom";
$arr["age"] = 76;

Use the sizeof function to get the number of elements, and the array_keys function to retrieve an array of the keys. To example, to print out GET parameters, you could do this:

$keys = array_keys($_GET);
for ($i = 0: $i < sizeof($_GET); $i++) {
	print("key=" . $keys[$i] . " and val = " . $_GET[$keys[$i]]. "<br>");
}

The print function will pass the output to the web page. Note the . concatenation for string values. Although PHP does not support the + operator on strings, uPHP would accept it.

Loops

uPHP handles for and while loops. For loops have the following syntax in pseudo code:

for (start condition; end condition; increment) {
 // your loop code
}

In a real example, the example show above in the array section illustrates how to loop through an array.
A while loop has the following syntax:

while (condition) {
  //your loop code
}

For example:

$x = 0;
while ($x < 10) { 
  print($x);
  $x++;
}

To be continued...

Comment (0) Hits: 2687

Wattmon firmware part 2

In the last post I described part of the workings of the os. I will now briefly go over the communication aspect.

Any modular system needs to be able to interact with its components reliably. Rather than reinventing the wheel I decided to use an industry standard that is still widely used today: Modbus RTU. I came to this conclusion after looking at many protocols, uLan being the runner up.  The downside of uLan is that it is 9 bit which makes it difficult to work with when interacting with a pc. I used it on my previous ebike management system and it is a pretty cool protocol, allowing multi-master communications. 
I decided on an IEE-485 bus which supports multiple nodes over a twisted pair cable. One of my requirements for the devices on the bus was that they should be powered by the master, making it very easy to install. Therefore, an RJ45 socket was used, where power (5v and gnd) are provided in addition to d+ and d-. A 500 mA resettable fuse on the wattmon box assures safety to the power section.
Modbus devices are described to wattmon through device description files, which are ini files with a fixed format.  New devices which are wattmon compliant can be automatically detected with a scan, whereas non-wattmon modbus devices need to be manually added to the main device.ini file after which they will be picked up by the polling engine.  The wattmon polled will automatically poll each modbus device at a predefined interval and update internal registers for the scripting engine to use.
In addition to the modbus port, I included a cc1100 wireless 433mhz transceiver  which runs a custom protocol. I looked at various existing protocols but found none that does exactly what I want. Devices need to be connected reliably, which means that a stack with transmission control was required.  I also wanted to be able to transmit different types of data, from short request-reply data such as an encapsulated modbus packet, to a type of stream that could be used to update remote display devices. This is not yet implemented fully but will probably use bencoded data which is currently used in the BitTorrent network.
An onboard serial port is also available for debugging or can also be accessed via a 3-pin header and connected to external devices.  All serial i/o can be controller through file operations in uPHP such as fopen, fread, fwrite and fclose.
More to come...

 

Comment (0) Hits: 2543

Description of the firmware part 1

Designing something that is functional and future proof turned out to be quite a daunting task. Looking at the feature set that I wanted to implement in wattmon,  it became clear that a powerful microcontroller needed to be used. The pic32mx795 is a great little 32 bit mcu with 512k of onboard flash and 128k of ram. At the beginning of the project this seemed like a lot! My experience so far has taught me that with a bit of thought you can make this go a long way. Wattmon uses FreeRTOS threads to coordinate the various os tasks, from network stack to script parsing and cron scheduling. The os makes extensive use of scheduled tasks and limits the number of concurrently executing process to ensure that memory doesn't run out. The task scheduler uses a crontab style format with an additional field making it possible to run things up to once a second.

Scripts
All executable scripts are written in uPHP (micro PHP) which is a name I came up with since the language is based completely on the PHP syntax. At system boot a startup script can be set to execute, which could either run in an infinite loop or be used to initialise system variables.  Every script has access to the $_GLOBALS array and can use this as a rudimentary inter process communication method. Scripts that run from within a web page with .cgi extension have an additional set of variables accessible to them: $_GET, $_POST, $_SESSION. These are identical to PHPs arrays and let you access the parameters to the script.  The session array is preserved through uses of a cookie sent back to the browser, making it possible to implement an access control mechanism with a user login page.
The uPHP function library implements a subset of the actual functionality of PHP but functions that are implemented are identical in functionality and syntax.  These are entirely written from scratch and are not in any way based on the PHP source code.  Since most of the language anyway uses lower level C calls this was relatively easy to implement.
More to come...
Comment (0) Hits: 2309