Skip to main content

HTTP Web Thing implementation

Project description

webthing

https://travis-ci.org/mozilla-iot/webthing-python.svg?branch=master https://img.shields.io/pypi/v/webthing.svg https://img.shields.io/badge/license-MPL--2.0-blue.svg

Implementation of an HTTP Web Thing. This library is compatible with Python 3.5+.

Installation

webthing can be installed via pip, as such:

$ pip install webthing

Example

In this example 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 = Thing('My Lamp', ['OnOffSwitch', 'Light'], 'A web connected lamp')

Now we can add the required properties.

The on property reports and sets the on/off state of the light. For this, we need to have a Value 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.add_property(
    Property(
        light,
        'on',
        Value(True, lambda v: print('On-State is now', v)),
        metadata={
            '@type': 'OnOffProperty',
            'label': '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.add_property(
    Property(
        light,
        'brightness',
        Value(50, lambda v: print('Brightness is now', v)),
        metadata={
            '@type': 'BrightnessProperty',
            'label': 'Brightness',
            'type': 'number',
            '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 = WebThingServer(SingleThing(light), port=8888)

try:
    server.start()
except KeyboardInterrupt:
    server.stop()

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 = Thing('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 utilizing a read-only Value by omitting the value_forwarder parameter.

      level = Value(0.0);
      
      sensor.add_property(
          Property(
              sensor,
              'level',
              level,
              metadata={
                  '@type': 'LevelProperty',
                  'label': 'Humidity',
                  'type': 'number',
                  'description': 'The current humidity in %',
                  'minimum': 0,
                  'maximum': 100,
                  'unit': 'percent',
              }))

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.

self.sensor_update_task = \
    get_event_loop().create_task(self.update_level())

async def update_level(self):
    try:
        while True:
            await sleep(3)
            new_level = self.read_from_gpio()
            logging.debug('setting new humidity level: %s', new_level)
            self.level.notify_of_external_update(new_level)
    except CancelledError:
        pass

This will update our Value object with the sensor readings via the self.level.notify_of_external_update(read_from_gpio()) call. The Value object now notifies the property and the thing that the value has changed, which in turn notifies all websocket listeners.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

webthing-0.7.0.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

webthing-0.7.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file webthing-0.7.0.tar.gz.

File metadata

  • Download URL: webthing-0.7.0.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for webthing-0.7.0.tar.gz
Algorithm Hash digest
SHA256 6aad7601f3814a970a95caa4a1311c2e44daf022ac3b69ff6596c123d0097305
MD5 f9454f2ee7760145d400d7b5bcd75fd4
BLAKE2b-256 7f26e46aebc09c0b9538278d6d1611d9e349b5befa1e02b132e29ed6eae049a0

See more details on using hashes here.

File details

Details for the file webthing-0.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for webthing-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 03736e766f31fba4f1abe46e99fe1f480d7c70403f7fdfa245ba3bc8e8bbfe70
MD5 5080a9ba8cd4c7706c45a849bbe79abe
BLAKE2b-256 c027a00495d84741f0cfb9cd5d80dd4685e70452d3810f5a8636850a59cf692d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page