Git Product home page Git Product logo

webthing-arduino's Introduction

Gitpod ready-to-code

webthing-arduino

A simple server for the ESP8266, the ESP32, boards with Ethernet, or any WiFi101-compatible board that implements Mozilla's proposed Web of Things API. The LED example exposes an OnOffSwitch named "Built-in LED" which controls the board's built-in LED. The LED Lamp example ups the ante by introducing a level property to expose a dimmable Light.

Arduino

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.

Continuing onwards

Make sure to install the current release of the ArduinoJson library (6) if you don't have it installed already.

ArduinoJson install process

Next, download this library from the same library manager by searching for webthing.

add zip library and LED example

You should be able to upload the example sketch onto your board and use it as a simple Web Thing. This Web Thing can be talked to using the WoT API or added to the WebThings Gateway using the "Add Thing by URL" feature. Note that right now, WiFi101-based Things must be manually added by typing the full URL to the Web Thing, e.g. http://192.168.0.103/things/led.

If you want to create a Web Thing from scratch, make sure to include both "Thing.h" and "WebThingAdapter.h" (or "EthernetWebThingAdapter.h", if using an Ethernet board). You can then add Things and Properties to your board using our proposed API.

PlatformIO

Add the webthing-arduino library through PlatformIO's package management interface. Ensure that you get the latest release by examining the entries in the version number dropdown list. It may be sorted counter-intuitively. You may also need to manually add the ArduinoJson and other libraries to your project.

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>

Adding to Gateway

To add your web thing to the WebThings Gateway, install the "Web Thing" add-on and follow the instructions here.

webthing-arduino's People

Contributors

hobinjk avatar mrstegeman avatar rzr avatar ivankravets avatar nedw avatar aschmidt75 avatar diegojuc avatar clvs7-gh avatar download13 avatar haydngreatnews avatar novski avatar lundsholm avatar twobraids avatar lglindstrom avatar ardentempiricist avatar mozilla-github-standards avatar njh avatar yaayyaaayy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.