Skip to content

W3C Web of Things TD 1.0 comptaible library for the Arduino IDE. Especially the ESP32

License

Notifications You must be signed in to change notification settings

Citrullin/web-of-thing-arduino

Repository files navigation

Gitpod ready-to-code

Web of Things TD for Arduino

⚠️ This library does not support the Mozilla WebThings RFC. It supports only the industry standard recommended by the W3C. (Web of Things TD 1.0)

A simple Web of Things Thing Description compatible library, for the ESP32 mainly. Available examples

Beginner Tutorials

If you need a more extensive explanation of the usage of this library, we have some step by step tutorials available with more context.

Web of Things Arduino + Node-WoT

Node-WoT is a Javascript client library to consume Web of Things devices.

Web of Things Arduino + Node-RED

Node-RED is a drag and drop tool for connecting devices. It requires little Javascript programming knowledge.

Web of Things Arduino

Installation

ESP8266 or ESP32

To run on either of these boards, download the Arduino IDE and set it up for board-specific development. These Adafruit guides explain how to set up for an ESP8266 and how to set up for an ESP32. You will also need to download the ESP Async WebServer library and unpack it in your sketchbook's libraries folder.

MKR1000, MKR1010, etc.

  • MKR1000 (and similar): Install the WiFi101 library from the Arduino library manager.
  • MKR1010 (and similar): Install the WiFiNINA library from the Arduino library manager.

PlatformIO

The PlatformIO integegration isn't complete yet.

Example

#include <Arduino.h>
#include "Thing.h"
#include "WebThingAdapter.h"

// TODO: Hardcode your wifi credentials here (and keep it private)
const char *ssid = "public";
const char *password = "";

#if defined(LED_BUILTIN)
const int ledPin = LED_BUILTIN;
#else
const int ledPin = 13; // manually configure LED pin
#endif

WebThingAdapter *adapter;

const char *ledTypes[] = {"OnOffSwitch", "Light", nullptr};
ThingDevice led("led", "Built-in LED", ledTypes);
ThingProperty ledOn("on", "", BOOLEAN, "OnOffProperty");

bool lastOn = false;

void setup(void) {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  Serial.begin(115200);
  Serial.println("");
  Serial.print("Connecting to \"");
  Serial.print(ssid);
  Serial.println("\"");
#if defined(ESP8266) || defined(ESP32)
  WiFi.mode(WIFI_STA);
#endif
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  bool blink = true;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    digitalWrite(ledPin, blink ? LOW : HIGH); // active low led
    blink = !blink;
  }
  digitalWrite(ledPin, HIGH); // active low led

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  adapter = new WebThingAdapter("w25", WiFi.localIP());

  led.addProperty(&ledOn);
  adapter->addDevice(&led);
  adapter->begin();
  Serial.println("HTTP server started");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.print("/things/");
  Serial.println(led.id);
}

void loop(void) {
  adapter->update();
  bool on = ledOn.getValue().boolean;
  digitalWrite(ledPin, on ? LOW : HIGH); // active low led
  if (on != lastOn) {
    Serial.print(led.id);
    Serial.print(": ");
    Serial.println(on);
  }
  lastOn = on;
}

Configuration

  • If you have a complex device with large thing descriptions, you may need to increase the size of the JSON buffers. The buffer sizes are configurable as such:

    // By default, buffers are 256 bytes for small documents, 1024 for larger ones
    
    // To use a pre-defined set of larger JSON buffers (4x larger)
    #define LARGE_JSON_BUFFERS 1
    
    // Else, you can define your own size
    #define SMALL_JSON_DOCUMENT_SIZE <something>
    #define LARGE_JSON_DOCUMENT_SIZE <something>
    
    #include <Thing.h>
    #include <WebThingAdapter.h>