The Blog

Wireless Communication

Wireless Hardware

Implementing a wireless network of any sort requires both a hardware and software solution. For the hardware I have chosen the cc1100 module which gives decent range, has a built in antenna and runs at 433 MHz. But the description of the communication mechanism described below could be used with any physical device provided you can get a reliable data stream. I use the packet management on the cc1100 along with automatic crc calculation for the data. This means that I can easily discard corrupt packets without much overhead.

Wireless Protocol

I have researched a bunch of different protocols out the such as 6lowpan, miwi and others. I needed a protocol that is reliable and flexible but at the same time with minimum overhead. I settled for a custom protocol which I call the wattmon protocol. It has an 8 byte packet header with the following fields:

  • src_node_id
  • dest_node_id
  • group_id
  • flags
  • frame_number
  • length

This pretty much takes care of any scenario I could come up with. It is not designed for multi-hop type setups and assumes that every node is within direct communication distance with the wattmon box. In addition, I have defined the following packet flags:


Packets with this flag will need to be acknowledged with an ACKREP packet back which has a length set to 0 but has the same frame number in its header, along with source and dest nodes flipped. The packet will be automatically retransmitted several times until it marks it as failed.


This is set automatically when a packet requiring an ACK is sent.


This packet type differentiates between modbus encoded packets and standard text packets. When a device receives a modbus flagged packet it will pass it on to the modbus handler, which will perform identical processing on it as if it came in on a serial line. The engine will then push out the response back to the wireless engine.

So essentially with a little extra work I got a reliable communication mechanism that I can use with my existing modbus modules to make them wireless or dual mode. Ideally I would like to leave the modbus port on so every device can be accessed over a cable for easy configuration in case any wireless settings change.

Each modbus wireless device has the following registers specifically for the wireless configuration:


Wireless channel to use


Group id to use, should be same as wattmon master

Node Id

Node Id of device, same for both cabled and non cabled version

Announce interval

Interval in ms of a period HELLO packet to help keep the wireless system alive and in sync.

Baud rate

Baud rate of wireless transmissions. Default is 4800 bps. Needs to be identical on all devices and wattmon.

I will be making all this information available in a more structured way along with source code examples for people who wish to develop add on wireless modules to their wattmon devices.

Comment (0) Hits: 2857

Conditionals in uPHP


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: 2563

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));


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;


$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.


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) { 

To be continued...

Comment (0) Hits: 3052

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: 2938