The code

Introduction

The project is a PlatformIO Arduino project. What I’ve taught myself is to add a extra_configs = myplatformio.ini line and data_dir = /$PROJECT_DIR/mydata/ in the [platformio] section of platformio.ini, and to exclude these files and folders in the .gitignore file.

Why? To prevent private data from accidentally ending up in the repository.

To provide an example, I’ve included a data folder and a myplatformio.ini.example file; this is generally a good starting point for your own settings.

Next comes the [env:esprocket] section; this section is intended for the ‘USB’ upload. The [env:esprocket_ota] section is a copy of [env:esprocket] created by extends = env:esprocket. You should then enter your OTA address and OTA password in the myplatformio.ini file.

The standard elements I carry over from project to project are:

  • configuration.h, containing fixed details such as pin numbers and other hardware-related information that do not belong in Config.
  • Esplog, which provides a uniform logging method and allows you to configure different logging destinations.
  • Config: this module provides a relatively standard way of storing the standard configuration items.

In addition, I tend to include the following for each class:

  • a constructor to initialise the variables
  • a setup function to configure the hardware correctly
  • a loop function to handle the loop logic.

And the class files are located in a folder (that is the .cpp and .h files) and explicitly not in the include or lib folders.

Classes

Esplog

Esplog uses a global variable logger and, in principle, two logging functions:

  • log( loglevel, ‘logline’);
  • vlogf( loglevel, ‘log line with vars %d’, somevar);

The most exciting feature is the new callback capability. This allows you to create your own log exit. For this project, I wanted to create an mqtt logging function, which could be implemented quite simply using this:

	logger.setCallback([](const char *message) {
        if (mqttClient.connected()) {
            char topic[128];
            mqttClient.publish(topicLog.c_str(), message);
        }
    });

Config

The config class ensures that the config.json file is loaded and saved in littleFS, and the setters and getters ensure that the values can be set and retrieved.

When you set a value, the file is not written to; as the values do not change very often, it is not a problem to call the load() and save() methods yourself.

Here too, we use a global variable, namely config.

NVState

The NVState class uses the global variable state. Unlike the Config class, this one does deal with values that change more frequently.

When one of the setter functions is called, a timer starts running and the change is saved after 5 seconds. When the next setter is called, the 5-second timer restarts. If nothing else changes within 5 seconds, the changes are saved in the espjet-state prefs.

Here, we use an internal version number; whenever the structure changes, the version number must be incremented, and a migration function must be created to migrate from the previous versions to the current one.

BootstrapServer

The BootstrapServer class is a name that has evolved; it was originally intended to boot the ESP in Access Point mode if no Wi-Fi had been configured.

If Wi-Fi has not been configured, the ESP will come up with the network ESP-<MAC address> and the password ESP123456789.

You can then go to 192.168.4.1 to configure Wi-Fi.

The page served is boot.html, which is stored on littleFS.

Once this is done, the ESP reboots, connects to the configured Wi-Fi network and then serves index.html.

config.html is the page where changes can be made to the ‘config’ settings.

Operational changes (NVSState) take place on index.html.

Button’s

The whole button system is designed to allow you to control various functions without a web interface.

The Button class is only called directly in setup to link the ESP pin to a button. Everything else is handled by the ButtonGroup.

    menuButtons.setup();
    menuButtons.setOnBtn1Pressed([]() {
        jet.incMode();
        logger.vlogf(LOG_INFO, ‘Mode button pressed %d’, jet.getMode());
    });

    menuButtons.setOnBtn2Pressed([]() {
        jet.incBrightness();
        logger.vlogf(LOG_INFO, ‘Brightness button pressed %d’, jet.getBrightness());
    });


    menuButtons.setOnBtn1HoldBtn2Pressed([]() { logger.log(LOG_INFO, ‘Mode button held, Brightness button pressed’); });
    menuButtons.setOnBtn2HoldBtn1Pressed([]() { logger.log(LOG_INFO, ‘Brightness button held, Mode button pressed’); });

Jet

It all happens in Jet. This is where the various modes are translated for the WS2812B/SK6812 LED.

More to come when the SK6812 ledstrip has arrived, together with the PCB

Last modified: November 1, 2026