Settings

Простой конструктор вебморды для настроек esp8266/esp32

MIT License

Stars
19
Committers
2

Settings

-   esp8266/esp32
    • 10 gzip
  • OTA ( )
  • GyverDB
  •  HTTP ,     : GyverHTTP,  esp-WebServer, ESPAsyncWebserver
    
  • GyverHub

Android-

ESP8266, ESP32

  • GTL v1.1.1+
  • GyverDB v1.1.2+
  • StringUtils v1.4.24+
  • GyverHTTP v1.0.17+
  • GSON v1.5.9+
PIO  Arduino IDE    
[env]
framework = arduino
lib_deps =
    GyverLibs/Settings
    ;esphome/ESPAsyncWebServer-esphome   ;   SettingsAsync
    ;esphome/ESPAsyncTCP-esphome         ;   SettingsAsync

[env:d1_mini]
platform = espressif8266
board = d1_mini
upload_speed = 921600
monitor_speed = 115200
monitor_filters = esp8266_exception_decoder, default
build_type = debug
board_build.filesystem = littlefs

[env:esp32dev]
monitor_speed = 115200
platform = espressif32
board = esp32dev
upload_speed = 921600
board_build.filesystem = littlefs

[env:esp32-c3]
monitor_speed = 115200
platform = espressif32
board = esp32dev
board_build.mcu = esp32c3
upload_speed = 2000000
board_build.f_cpu = 80000000L
board_build.filesystem = littlefs

-, html/css/js. , gz PROGMEM . , IP . html , . http: , json.

Captive portal

    DNS    Captive portal -  ESP      (AP  AP_STA),            .

      ,     ,      .    -            .   /        .         (  ).       -     *255.255.255.0*,      .

v1.0.13+

   -   ,  ,     .         ,       .
//  
SettingsGyver sett;

void build(sets::Builder& b) {
    // b.Input(...);
    // b.Button(...);
}

void setup() {
    //   WiFi...

    sett.begin();
    sett.onBuild(build);
}

void loop() {
    sett.tick();
}

ID

   ID   ID. ID   :
  •  ,   UI  
    

ID , size_t ( ESP 32- ). ID:

  • enum
    • StringUtils: SH("my_input") "my_input"_h
    • DB_KEYS GyverDB

ID - ( ). ID - UINT32_MAX, 1 . ID - Duplicated ID

ID 0, .. b.build.id == 0

enum keys : size_t {
    my_inp = 1,
    button,
};

DB_KEYS(
    kk,
    my_inp,
    button
);

void build(sets::Builder& b) {
    b.Input("My input");                //  ID
    b.Input("my_inp"_h, "My input");    // -
    b.Input(SH("my_inp"), "My input");  // -
    b.Input(12, "My input");            //  
    b.Input(keys::my_inp, "My input");  // enum
    b.Input(kk::my_inp, "My input");    // GyverDB-
}

 , ..    :
  • ID,             GyverDB
    
  • ,          
    
  • ID (0 ),
  • ( ) true ,
String str;
char cstr[20];

void build(sets::Builder& b) {
    //   id   
    //       
    if (b.Input("My input")) {
        Serial.println(b.build.value);
    }

    //   id   String-
    //        
    b.Input("My input", &str);
    b.Input("My input", AnyPtr(cstr, 20));  //  char-

    //   id   
    //        
    b.Input("my_inp"_h, "My input");

    //      id
    if (b.Button()) Serial.println("btn 1");
    if (b.Button("my_btn2"_h)) Serial.println("btn 2");
}

AnyPtr - , (, ). char-

void build(sets::Builder& b) {
    //  ,     
    if (b.build.isAction()) {
        Serial.print("Set: 0x");
        Serial.print(b.build.id, HEX);
        Serial.print(" = ");
        Serial.println(b.build.value);
    }
}
//  UI  
void build(sets::Builder& b) {
    //  UI
    b.Input("my_inp"_h, "My input");
    b.Button("my_btn"_h, "My button");

    //  
    switch (b.build.id) {
        case "my_inp"_h:
            Serial.print("input: ");
            Serial.println(b.build.value);
            break;

        case "my_btn"_h:
            Serial.println("btn click");
            break;
    }
}

ID - . -

Text

Text - StringUtils, . . Text , .

Serial.println(b.build.value);  // 
b.build.value == 123;           // 
b.build.value == "123";         // 
b.build.value.toFloat();        // 
byte b = b.build.value;         // -

GyverDB - . Settings , GyverDB. GyverDBFile , flash . , . , . , .. Settings . :

//       Settings
#include <GyverDBFile.h>
#include <LittleFS.h>
GyverDBFile db(&LittleFS, "/data.db");

#include <SettingsESP.h>
SettingsESP sett("My Settings", &db);

//  -   .  ,  ""  enum, 
//            
DB_KEYS(
    keys,
    input,
    slider
);

// 
void build(sets::Builder& b) {
    b.Input(kk::input);
    b.Slider(kk::slider);
}

void setup() {
    Serial.begin(115200);
    Serial.println();

    // WIFI
    WiFi.mode(WIFI_STA);
    WiFi.begin("WIFI_SSID", "WIFI_PASS");
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println();
    Serial.println(WiFi.localIP());

    // settings
    sett.begin();
    sett.onBuild(build);

    //   
#ifdef ESP32
    LittleFS.begin(true); // format on fail
#else
    LittleFS.begin();
#endif

    //      
    db.begin();
    
    //    
    db.init(keys::input, "text");
    db.init(keys::slider, 30);
}

void loop() {
    sett.tick();
}

,       ,   "" ,      : `GyverDBFile`   ,   `GyverDB` - ,     .        ,            . 
GyverDBFile db_flash(&LittleFS, "/data.db");
GyverDB db_ram;

void build(sets::Builder& b) {
    settings.attachDB(&db_ram);
    b.Input("input2"_h, "...");

    settings.attachDB(&db_flash);
    b.Input("input1"_h, "...");
}
    ,     ,        .      ,    ,      .

, . , , id. :

int numbers[5];

void build(sets::Builder& b) {
    //   
    for (int i = 0; i < 5; i++) {
        b.Input();
    }
    
    //     
    for (int i = 0; i < 5; i++) {
        if (b.Input()) Serial.println(b.build.value);
    }
    
    //  number   
    for (int i = 0; i < 5; i++) {
        b.Number(String("number #") + i, &numbers[i]);
    }

    //   
    for (int i = 0; i < 5; i++) {
        if (b.Number(String("number #") + i, &numbers[i])) {
            Serial.print(String("number #") + i + ": ");
            Serial.println(numbers[i]);
        }
    }

    //   
    for (int i = 0; i < 5; i++) {
        b.Number(String("number #") + i, &numbers[i]);

        if (b.wasSet()) {
            Serial.print(String("number #") + i + ": ");
            Serial.println(numbers[i]);
            b.clearSet();
        }
    }
}
, 
void build(sets::Builder& b) {
    if (flag) {
        b.Input();
        b.Slider();
        // ...
    }
}
    ,    :
void build(sets::Builder& b) {
    if (b.Switch()) {
        b.reload(); //      
    }

    //  flag       
    if (flag) {
        b.Input();
        b.Slider();
        // ...
    }
}
DB_KEYS(
    kk,
    mode_sw
);

void build(sets::Builder& b) {
    //     
    if (b.Switch(kk::mode_sw)) b.reload();

    //   
    if (db[kk::mode_sw]) {
        b.Input();
        b.Slider();
        // ...
    }
}

 -      .     -                  (        ). :        -        ,    .     ,          - .            id .
void build(sets::Builder& b) {
    b.Label("lbl1"_h, "Random");
    b.Label("lbl2"_h, "millis()", "", sets::Colors::Red);
}
void update(sets::Updater& upd) {
    upd.update("lbl1"_h, random(100));
    upd.update("lbl2"_h, millis());
}

void setup() {
    sett.begin();
    sett.onBuild(build);
    sett.onUpdate(update);
}

  • , offline . , - , . .
  •     ,      **error***   
    

.     ,            .  `begin`   true       :
void build(sets::Builder& b) {
    if (b.beginGroup("Group 1")) {
        b.Input("input1"_h, "Text");

        b.endGroup();  //  
    }
}
  •  ,      .          :
    
void build(sets::Builder& b) {
    {
        sets::Group g(b, "Group 2");  //     

        b.Input("input1"_h, "Text");
    }
}

. . , , . .

void build(sets::Builder& b) {
    b.Input("input1"_h, "Text 1");

    {
        sets::Menu g(b, "Submenu");

        b.Input("input2"_h, "Text 2");
    }
}
  • , :
void build(sets::Builder& b) {
    {
        sets::Buttons btns(b);

        //   true  
        if (b.Button("btn1"_h, "Button 1")) {
            Serial.println("Button 1");
        }

        if (b.Button("btn2"_h, "Button 2", sets::Colors::Blue)) {
            Serial.println("Button 2");
        }
    }
}

: - "" : , OTA . ( ) . , - , - . - . .

     Guest.        -        .          . :
if (b.beginGroup("Group 1")) {
    //   
    b.Pass(kk::pass, "Password");

    // ,     
    {
        sets::GuestAccess g(b);
        b.Input(kk::uintw, "uint");
        b.Input(kk::intw, "int");
        b.Input(kk::int64w, "int 64");
    }

    //   
    {
        sets::Menu m(b, "sub sub");
        b.Label(kk::lbl2, "millis()", "", sets::Colors::Red);
    }

    b.endGroup();
}
   ,  .

: , - ,

emoji, .

b.Input(kk::intw, "");

  • SettingsGyver (SettingsGyver.h) - GyverHTTP
  • SettingsESP (SettingsESP.h) - ESP
  • SettingsAsync (SettingsAsync.h) - ESPAsyncWebserver

#define SETT_NO_DB  //    GyverDB

SettingsBase/SettingsGyver/SettingsESP/SettingsAsync

Settings(const String& title = "", GyverDB* db = nullptr);

//    .   ""  
void setPass(Text pass);

//  .     +   update
void reload();

//   
void setTitle(const String& title);

//    (. 2500), 0  
void setUpdatePeriod(uint16_t prd);

//   
void attachDB(GyverDB* db);

//      (       )
void useAutoUpdates(bool use);

//  
void onBuild(BuildCallback cb);

//  
void onUpdate(UpdateCallback cb);

// ,    
void tick();

Builder

//   
Build build;

// -ID  
size_t nextID();

//    SettingsXxx
void* thisSettings();

//   (  ,  if (...click() b.reload()))
void reload();

//    -   
bool wasSet();

//    wasSet
void clearSet();

// 
//     
bool beginGuest();

//   
void endGuest();

// 
bool beginGroup(Text title = Text());
void endGroup();

//  
bool beginMenu(Text title);
void endMenu();

//  
bool beginButtons();
void endButtons();

// 
// 
// ================= LABEL =================
//  ,    id
void Label(size_t id, Text label = "", Text text = Text(), uint32_t color = SETS_DEFAULT_COLOR);
void Label(size_t id, Text label, Text text, sets::Colors color);
void Label(Text label = "", Text text = Text(), uint32_t color = SETS_DEFAULT_COLOR);
void Label(Text label, Text text, sets::Colors color);

// ================= LED =================
//  (value 1  - , value 0  - )
void LED(size_t id, Text label, bool value);
void LED(size_t id, Text label = "");
void LED(Text label, bool value);
void LED(Text label = "");

// ================= TEXT =================
//  
void Paragraph(size_t id, Text label = "", Text text = Text());
void Paragraph(Text label = "", Text text = Text());

// active

// ================= INPUT =================
//     [ - ],   -  
bool Input(size_t id, Text label = "", AnyPtr value = nullptr);
bool Input(Text label = "", AnyPtr value = nullptr);

// ================= NUMBER =================
//   [ - ],   -  
bool Number(size_t id, Text label = "", AnyPtr value = nullptr);
bool Number(Text label = "", AnyPtr value = nullptr);

// ================= PASS =================
//   [ - ],   -  
bool Pass(size_t id, Text label = "", AnyPtr value = nullptr);
bool Pass(Text label = "", AnyPtr value = nullptr);

// ================= COLOR =================
//   [ - 24- DEC ],   - uint32_t
bool Color(size_t id, Text label = "", uint32_t* value = nullptr);
bool Color(Text label = "", uint32_t* value = nullptr);

// ================= SWITCH =================
//  [ 1/0],   - bool
bool Switch(size_t id, Text label = "", bool* value = nullptr);
bool Switch(Text label = "", bool* value = nullptr);

// ================= DATE =================
//  [ - unix ],   - uint32_t
bool Date(size_t id, Text label = "", uint32_t* value = nullptr);
bool Date(Text label = "", uint32_t* value = nullptr);

// ================= TIME =================
//  [ -    ],   - uint32_t
bool Time(size_t id, Text label = "", uint32_t* value = nullptr);
bool Time(Text label = "", uint32_t* value = nullptr);

// ================= DATETIME =================
//    [ - unix ],   - uint32_t
bool DateTime(size_t id, Text label = "", uint32_t* value = nullptr);
bool DateTime(Text label = "", uint32_t* value = nullptr);

// ================= SLIDER =================
//  [ - ],   -  
bool Slider(size_t id, Text label = "", float min = 0, float max = 100, float step = 1, Text unit = Text(), AnyPtr value = nullptr);
bool Slider(Text label = "", float min = 0, float max = 100, float step = 1, Text unit = Text(), AnyPtr value = nullptr);

// ================= SELECT =================
//   ; [ -  ()],   - uint8_t
bool Select(size_t id, Text label, Text options, uint8_t* value = nullptr);
bool Select(Text label, Text options, uint8_t* value = nullptr);

// ================= BUTTON =================
//       ,     
bool Button(size_t id, Text label = "", uint32_t color = SETS_DEFAULT_COLOR);
bool Button(Text label = "", uint32_t color = SETS_DEFAULT_COLOR);

bool Button(size_t id, Text label, sets::Colors color);
bool Button(Text label, sets::Colors color);

// misc
//  ,     update   id  update   
bool Confirm(size_t id, Text label = "");
  • Text - , . value . , . - Value, b.Color("col", "Color", Value(my_color));, my_color uint32_t.
  • AnyPtr - : float, double, , String, AnyPtr(char[], size_t len)

Build

//  
const Type type;

//  
const bool granted;

// id  ()
const size_t id;

//   ()
const Text value;

//  -  
bool isBuild();

//  -  (   )
bool isAction();

//   
class GuestAccess(Builder& b);

//   
class Group(Builder& b, Text title = Text());

//   
class Menu(Builder& b, Text title);

//  
class Buttons(Builder& b);

Updater

//   (   Confirm)
void update(size_t id);

//   
void update(size_t id, Text value);

//   float
void update(size_t id, float value, uint8_t dec = 2);

//   
void update(size_t id, <  > value);

  • v1.0
  • v1.0.2
    • Confirm ( )
    • Input (Input     AP WiFi   Xiaomi)
      
  • v1.0.5
    • LED
    • Select
  • v1.1.0
    • Number
    • ID ( ID)
    • (   )
      
    • GyverDB 
      
    • build(), value(), id() (build, value, id)

  •  **Settings**      :
    
    • Arduino IDE
    • Arduino IDE v2
    • PlatformIO
  • .zip :
    • *C:\Program Files (x86)\Arduino\libraries* (Windows x64)
      
    • *C:\Program Files\Arduino\libraries* (Windows x32)
      
    • */Arduino/libraries/*
      
    • (Arduino IDE) .zip: */ / .ZIP *
  •    [](https://alexgyver.ru/arduino-first/#%D0%A3%D1%81%D1%82%D0%B0%D0%BD%D0%BE%D0%B2%D0%BA%D0%B0_%D0%B1%D0%B8%D0%B1%D0%BB%D0%B8%D0%BE%D1%82%D0%B5%D0%BA)
    

  • : ,
  • IDE: ""
  • : ** **, . "" : , !

**Issue**,       [[email protected]](mailto:[email protected])  
  **Pull Request**'!

      :
  • SDK ( ESP)
  • Arduino IDE
  •  ,      ,      
    
  • ,
  • ,    .     ,   
    
Badges
Extracted from project README's
latest PIO Foo Foo Foo Foo