esp8266_web_settings beta
esp8266_device_framework Detailed Documentation

Introduction

A generic device framework using the esp8266_web_settings library

This framework supports settings, control, and publishing of multiple ESP8266-attached devices in concert with the WebSettings class from esp_web_settings, https://github.com/grmcdorman/esp8266_web_settings.

Overview

The base class for Devices is Device. The framework contains seven Devices:

The VindriktningAirQuality class is derived from work by Hypfer's GitHub project, https://github.com/Hypfer/esp8266-vindriktning-particle-sensor. All work on message deciphering comes from that project.

An additional helper class, ConfigFile, provides basic JSON configuration file loading and saving for the list of devices.

The sketch using the devices must have two strings, a firmware name, and a system identifier. The former is a static PROGMEM string identifying the name or purpose of the sketch; the latter should be a unique name for the specific ESP device; typically this can be a derivative of the firmware name and the ESP chip ID, in hex.

In the sketch setup() function, the following operations should be performed, in sequence, on the list of devices; this assumes you a global std::vector<Device *> devices, a global configuration object ConfigFile config, and a global WebSettings web_settings.

  • Initialization:
    for (auto &device: devices)
    {
    device->set_system_identifiers(FPSTR(firmware_name), identifier);
    device->set_defaults();
    }
  • Optionally, set some defaults different from the built-in ones:
    // Device index # 0 is InfoDisplay. It has no relevant settings.
    // Device index # 1 is System Details Display. It has no relevant settings.
    // Device index # 2 is WiFi display. It has no relevant settings.
    // Device index # 3 is the WiFi setup. A default AP and password could be set:
    devices[3]->set("ssid", "my access point");
    // Note that this will *not* be shown in the web page UI.
    devices[3]->set("password", "my password");
    // Device index # 4 is the SHT31-D. Set the default sda to D2, scl to D3.
    devices[4]->set("sda", "D2");
    devices[4]->set("scl", "D3");
  • load settings:
    config.load(devices);
  • Setup devices and add to the web settings:
    for (auto &device : devices)
    {
    device->setup();
    device->set_devices(devices);
    webServer.add_setting_set(device->name(), device->identifier(), device->get_settings());
    }

In the sketch loop() function, each device's loop() method must be called:

for (auto &device : devices)
{
device->loop();
}

Finally, in the on_save callback from the WebSettings class, the device configurations must be saved:

void on_save(::grmcdorman::WebSettings &)
{
config.save(devices);
}