BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Mozilla Revamps WebThings, its Open Source IoT Platform

Mozilla Revamps WebThings, its Open Source IoT Platform

Leia em Português

This item in japanese

Mozilla recently released its open source IoT platform, formerly called Project Things, as WebThings. Mozilla WebThings brings a series of logging, alarm, and networking features.

Mozilla WebThings is an open source implementation of emerging Web of Things standards at the W3C. W3C Web of Things is an initiative that aims to reduce the IoT fragmentation, through the recently launched Web of Things Working Group. W3C started to develop the initial standards for the Web of Things, aiming to reduce the costs of development, lessen the risks to both investors and customers, and encourage exponential growth in the market for IoT devices and services.

Mozilla WebThings is an open platform for monitoring and controlling devices over the web, and consists of two core components: WebThings Gateway, designed for smart home gateways focused on privacy, security and interoperability, and WebThings Framework, designed to help developers build their own web things.

WebThings Gateway

WebThings Gateway is a software which allows users to monitor and control their smart home over the web. By using WebThings Gateway, it is possible, for example, to check how many times a door was opened/closed, or how much energy plugged-in appliances are consuming. To do this, users should go to the "main menu > Settings > Experiments" and enable the "Logs" option.

WebThings Gateway now provides a new alarms capability for devices like smoke alarms, carbon monoxide alarms or burglar alarms; this means that users can now check whether an alarm is currently active, and configure rules to notify when an alarm is triggered when they are out.

With WebThing Gateway 0.8 it is possible to re-configure gateway network settings from the web interface. Furthermore, users can either configure the Ethernet port with a dynamic or static IP address, or re-scan available wireless networks and change the Wi-Fi access point that the gateway is connected to.

WebThings Framework

The WebThings Framework is a collection of reusable software components designed to help developers build their own web things, which directly exposes the Web Thing API. The Web Things API is intended to complement the W3C Web of Things Working Group's work on an abstract data model and API for the Web of Things, by defining a simple concrete serialisation and protocol. The Web Thing REST API and Web Thing WebSocket API allow a web client to access the properties of devices, request the execution of actions, and subscribe to events representing a change in state.

The WebThings library is officially available in languages such as Java, Python, Node.js, and Rust. Let's see an implementation of a dimmable light in Java .

First, add the following dependency to your project:

<dependencies>
    <dependency>
        <groupId>org.mozilla.iot</groupId>
        <artifactId>webthing</artifactId>
        <version>LATEST</version>
    </dependency>
</dependencies>

Now create a new Thing:

Thing light = new Thing("My Lamp",
                        new JSONArray(Arrays.asList("OnOffSwitch", "Light")),
                        "A web connected lamp");

Add the properties for on, and brightness:

JSONObject onDescription = new JSONObject();
onDescription.put("@type", "OnOffProperty");
onDescription.put("title", "On/Off");
onDescription.put("type", "boolean");
onDescription.put("description", "Whether the lamp is turned on");

Value<Boolean> on = new Value<>(true,
                                // Here, you could send a signal to
                                // the GPIO that switches the lamp
                                // off
                                v -> System.out.printf(
                                        "On-State is now %s\n",
                                        v));

light.addProperty(new Property(light, "on", on, onDescription));

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

try {
   WebThingServer server = new WebThingServer(new SingleThing(light), 8888);
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            server.stop();
        }
    });
    server.start(false);
} catch (IOException e) {
    System.out.println(e);
    System.exit(1);
}

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

More details are available on the Mozilla IoT website, and the developer documentation for the WebThings Gateway and WebThings Framework. Developers who want to contribute to this documentation can do so via GitHub repo.

Rate this Article

Adoption
Style

BT