PlugWeb: Creating the driver

In the previous post I introduced the concepts for the PlugWeb application. In this blogpost I will cover developing the driver for the plugwise network.

The plugwise network

The plugwise network consist of a set wireless devices communicating with each other using the zigbee protocol. The devices are called Circle's and each network contains one special device, called a Circle+. Communication between the Plugwise network and a computer can be done using the Plugwise USB Stick, which is essentially a serial device.

Device commands

Commands can be sent to the network to switch on/off devices or gather device information. Below are the actions that will be implemented in the PlugWeb system.

Driver classes

The driver consists of the following classes:

Using the driver

Below is an example how to use the driver for a single device.

$mac = 'XXXXXX';
$device = new \Plugwise\Driver\Device($mac);
$powerInfo = $device->getPowerInfo();
if (isset($powerInfo)) && ($powerInfo['state']==1)) {
    $powerInfo->switchOff();
} else {
    $powerInfo->switchOn();
}

The example below illustrates how to use the driver for querying the plugwise network.

$network = new \Plugwise\Driver\Network();
$devices = $network->getDevices();
foreach($devices as $device) {
    $device->switchOff();
}

Example action: Device info

A single action will be shown in more detail; retrieving device information. The action is accessible through deviceInfo method of the device class:

public function deviceInfo() {
    $input = $this->getRequest()->actionDeviceInfo($this->_mac);
    $this->getSerial()->sendData($input);

    $data  = $this->getSerial()->readData();
    $out   = $this->_formatData($data);
    return $out;
}

The first step is to create the input string, which is handled by the request object. After that the input string is sent to the serial port and reads data from the serial port. A serial class handles communications with a serial port. The last step interprets the data from the serial port and converts the information into a usable array.

Building the input string

Below is the method actionDeviceInfo of the request class. The _renderActionString method used here finishes the input string by adding a checksum.

public function actionDeviceInfo($mac) {
    // 0012 000D6F0000 729310
    $str = self::ACTION_DEVICE_INFO . self::MAC_PREFIX . $mac;
    return $this->_renderActionString($str);
}

Reading the response

The readString method of the request class reads the first four characters of the string which represents the plugwise device action. Based on the device action the appropiate method for the input string will be invoked. Below is the method for reading the input string of a device info response.

protected function _processDeviceInfo($string) {
    if (strlen($string)!=66) {
        return array('error' => 'Wrong length of Response::DeviceInfo string');
    }
    // Prepare output data
    $data = array(
        'sequenceNumber'   => hexdec(substr($string, 0, 4)),
        'macAddress'       => substr($string, 14, 6),
        'clockYear'        => hexdec(substr($string, 20, 2)),
        'clockMonth'       => hexdec(substr($string, 22, 2)),
        'clockMinutes'     => hexdec(substr($string, 24, 4)),
        'logAddress'       => hexdec(substr($string, 28, 8)),
        'currentState'     => substr($string, 36, 2),
        'currentFrequency' => substr($string, 38, 2),
        'currentVersion'   => substr($string, 40, 12),
        'currentFirmware'  => substr($string, 52, 8),
        'unknownParam'     => substr($string, 48, 2),
        'otherParam1'      => hexdec(substr($string, 50, 12)),
        'crcString'        => substr($string, 60, 4)
    );
    return $data;
}

Getting the source

The source is available on GitHub as a seperate project: PlugWeb-Driver. The driver library will be included within the PlugWeb application.

Future blog posts in this series

Next time I will blog about the following aspects of the PlugWeb system.

Rendered Content in 0.003 seconds

Rendered UI Widgets in 0.002 seconds

Want to see the cheezy internals?
twig layout template | twig page template | json data