PHP Classes

Mozilla WebThings PHP IOT Framework: Control Internet connected devices via Web service

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 130 All time: 9,364 This week: 38Up
Version License PHP version Categories
webthing-php 1.0.0Custom (specified...5PHP 5, Web services, Hardware, PSR
Description 

Author

This package can control internet connected devices via Web service.

It can start a Web server that will handle requests via Web sockets to control devices available on the device maching on which PHP is running.

The package allows add devices to be controlled like for instance lights, as well properties of the device that can be controlled by sending HTTP requests to the Web server started by this package.

Innovation Award
PHP Programming Innovation award nominee
January 2020
Number 3
Internet Of the Things (or IOT) is a concept that refers to use Internet based protocols to communicate with devices that perform useful tasks like controlling the temperature of a room using thermostats that sensors that measure the temperature as well heater or cooler parts to change the room temperature.

This package can be used to run inside one of those devices to connect to sensors and control the device parts that perform some useful task by running a Web server inside the device using code that is all written in PHP.

Manuel Lemos
Picture of Malik Naik
  Performance   Level  
Innovation award
Innovation award
Nominee: 5x

 

Documentation

Web Things PHP

travis GitHub forks GitHub version Source Code PHP Version Software License

Implementation of an HTTP Web Thing. This library is compatible with PHP 7.1+.

Installation

The `webthingcan be installed usingcomposer` via the following command:

composer require webthing/webthing:^0.0.1

Running the Example

The following list of commands clones this repository and installs all dependencies using the composer and runs the single-thing.php example.

git clone https://github.com/maliknaik16/webthing-php.git
cd webthing-php
composer install
php examples/single-thing.php

Example Implementation

In this code-walkthrough we will set up a dimmable light and a humidity sensor (both using fake data, of course). Both working examples can be found in here.

Dimmable Light

Imagine you have a dimmable light that you want to expose via the web of things API. The light can be turned on/off and the brightness can be set from 0% to 100%. Besides the name, description, and type, a Light is required to expose two properties:

- `on`: the state of the light, whether it is turned on or off

- Setting this property via a ``PUT {"on": true/false}`` call to the REST API toggles the light.

- `brightness`: the brightness level of the light from 0-100%

- Setting this property via a PUT call to the REST API sets the brightness level of this light.

First we create a new Thing:

$light = new Thing(
  'urn:dev:ops:my-lamp-1234',
  'My Lamp',
  ['OnOffSwitch', 'Light'],
  'A web connected lamp'
);

Now we can add the required properties.

The `onproperty reports and sets the on/off state of the light. For this, we need to have aValue` object which holds the actual state and also a method to turn the light on/off. For our purposes, we just want to log the new state if the light is switched on/off.

$light->addProperty(new Property(
  $light,
  'on',
  new Value(TRUE, function($v) {
    echo "On-State is now " . $v . "\n";
  }),
  [
    '@type' => 'OnOffProperty',
    'title' => 'On/Off',
    'type' => 'boolean',
    'description' => 'Whether the lamp is turned on',
  ])
);

The `brightness` property reports the brightness level of the light and sets the level. Like before, instead of actually setting the level of a light, we just log the level.

$light->addProperty(new Property(
  $light,
  'brightness',
  new Value(50, function($v) {
    echo "Brightness is now " . $v . "\n";
  }),
  [
    '@type' => 'BrightnessProperty',
    'title' => 'Brightness',
    'type' => 'integer',
    'description' => 'The level of light from 0-100',
    'minimum' => 0,
    'maximum' => 100,
    'unit' => 'percent',
  ])
);

Now we can add our newly created thing to the server and start it:

// If adding more than one thing, use MultipleThings() with a name.
// In the single thing case, the thing's name will be broadcast.
$server = new WebThingServer(new SingleThing($thing), '127.0.0.1', 8888, 8081);

$server->start();
$server->startWebSocket();

This will start the server, making the light available via the WoT REST API and announcing it as a discoverable resource on your local network via mDNS.

Sensor

Let's now also connect a humidity sensor to the server we set up for our light.

A MultiLevelSensor (a sensor that returns a level instead of just on/off) has one required property (besides the name, type, and optional description): `level`. We want to monitor this property and get notified if the value changes.

First we create a new Thing:

$sensor = new Thing(
 'urn:dev:ops:my-humidity-sensor-1234',
  'My Humidity Sensor',
  ['MultiLevelSensor'],
  'A web connected humidity sensor'
);

Then we create and add the appropriate property:

- `level`: tells us what the sensor is actually reading

- Contrary to the light, the value cannot be set via an API call, as it wouldn't make much sense, to SET what a sensor is reading. Therefore, we are creating a readOnly property.

  ```php
  $level = new Value(0.0);
  $sensor->addProperty(new Property(
    $sensor,
    'level',
    $level,
    [
      '@type' => 'LevelProperty',
      'title' => 'Humidity',
      'type' => 'number',
      'description' => 'The current humidity in %',
      'minimum' => 0,
      'maximum' => 100,
      'unit' => 'percent',
      'readOnly' => TRUE,
    ])
  );
  ```

Now we have a sensor that constantly reports 0%. To make it usable, we need a thread or some kind of input when the sensor has a new reading available. For this purpose we start a thread that queries the physical sensor every few seconds. For our purposes, it just calls a fake method.

// $level is a `Value` object.
// $loop is a `React\EventLoop\Factory` object.
$loop->addPeriodicTimer(7, function() use ($level) {
  $new_level = readFromGpio();
  printf("Setting new humidity level: %s\n", $new_level);
  $level->notifyOfExternalUpdate($new_level);
});


function readFromGpio() {
  return abs(70.0 rand() (-0.5 + rand()));
}

This will update our `Valueobject with the sensor readings via the$level->notifyOfExternalUpdate(readFromGpio());call. TheValue` object now notifies the property and the thing that the value has changed, which in turn notifies all websocket listeners.

Resources

- https://iot.mozilla.org/wot - https://iot.mozilla.org/framework/ - https://iot.mozilla.org/gateway/ - https://www.w3.org/WoT/IG/

License

Mozilla Public License Version 2.0


  Files folder image Files (32)  
File Role Description
Files folder imageexamples (2 files)
Files folder imagesrc (1 directory)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE.txt Lic. Documentation
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file travis.sh Data Auxiliary data
Accessible without login Plain text file _config.yml Data Auxiliary data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:130
This week:0
All time:9,364
This week:38Up