logo elektroda
logo elektroda
X
logo elektroda

ksIotFrameworkLib - IoT framework based on arduino port for Esp8266 and ESP32 platforms

cziter15 1425 3

TL;DR

  • ksIotFrameworkLib is an IoT framework for ESP8266 and ESP32 smart-home devices, built on Arduino core and PlatformIO.
  • It organizes software around an application rotator that runs instantiated apps in sequence, while components can be searched by type and swapped safely with weak_ptr and locks.
  • The framework ships with a local HTTP device portal, MQTT connectivity, configuration providers, device statistics reporting, and a reset-button component.
  • An EnergyMonitorApp example assembles WiFi, MQTT, LED, portal, sensor, and reset components, then binds MQTT events to LED status handling.
  • A status LED blinks every 500 ms, and removing Arduino support would probably mean dropping ESP8266 support.
Generated by the language model.
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
📢 Listen (AI):
  • Hi,

    I've been writing a framework for internet of things devices for a few years now, which drives entire smart-home solutions for me at home. Recently I built a gateway to Zigbee, which is also driven by this framework. You can find a description of it here: https://www.elektroda.pl/rtvforum/topic4103124.html .

    At first I supported the ESP8266, then also the ESP32. There is an Arduino core underneath and the whole thing runs on PlatformIO. This means only that there are structures known from Arduino underneath, i.e. functions such as millis(). However, the code runs natively on the esp, without any 'atmegs' along the way. In theory I could get rid of the Arduino as such but then I would rather decide to drop support for the ESP8266. The code is written in such a way that it enables and drastically simplifies doing many things at once.

    Functions .
    - I decided at the outset that I would like to go in the direction of a compzy, implemented along the lines of... Unity3D
    - the main object is the application rotator, which is created at the start of the application
    - the rotator has a list of applications that are instantiated and executed until the end - when the application finishes, the next one rises and at the end this loops
    usually the device software consists of a functional application (device behaviour) and a configuration application (own access point)
    - having a pointer to the application we can search for components by type and easily avoid rigid references
    components can be added and removed while the application is running and by using the lock mechanism and weak_ptr we can write stable and safe code

    Components .
    In addition to the framework itself, the user can use internal components as well as their own. Among the internal components, we have such as:
    - the device portal over HTTP on the local network, with terminal, status and also the possibility of software updates
    - the MQTT connection component
    - configuration components (provider and master)
    - a component for sending device statistics (RSSI, IP and others)
    - a component performing various RESET button functions

    Software architecture
    Flowchart of IoT application initialization and loop with various states. Let's get started.

    Example piece of code, showing how the application is initialised. The aim was clarity and simplicity.
    
    /*
        Budowanie listy komponentów, inicjalizacja aplikacji.
    */
    bool EnergyMonitorApp::init()
    {
        /* Add WiFi connector component. */
        addComponent<ksf::comps::ksWifiConnector>(apps::config::EnergyMonitorConfig::emonDeviceName);
    
        /* Add MQTT components. */
        auto mqttWp{addComponent<ksf::comps::ksMqttConnector>()};
        addComponent<ksf::comps::ksDevStatMqttReporter>();
    
        /* Add LED indicator components. */
        statusLedWp = addComponent<ksf::comps::ksLed>(STATUS_LED_PIN);
        eventLedWp = addComponent<ksf::comps::ksLed>(EVENT_LED_PIN);
    
        /* Create Device Portal component. */
        addComponent<ksf::comps::ksDevicePortal>();
    
        /* Add sensor component. */
        auto sensorCompWp{addComponent<components::EnergySensor>(ANA_PIN)};
    
        /* Setup reset button. */
        addComponent<ksf::comps::ksResetButton>(CFG_PUSH_PIN, LOW);
    
        /* Bind MQTT connect/disconnect events for LED status. */
        if (auto mqttSp{mqttWp.lock()})
        {
            mqttSp->onConnected->registerEvent(connEventHandleSp, std::bind(&EnergyMonitorApp::onMqttConnected, this));
            mqttSp->onDisconnected->registerEvent(disEventHandleSp, std::bind(&EnergyMonitorApp::onMqttDisconnected, this));
        }
    
        /* Set event LED. */
        if (auto sensorCompSp{sensorCompWp.lock()})
            sensorCompSp->setEventLed(eventLedWp);
    
        /* Start blinking status LED. */
        if (auto statusLedSp{statusLedWp.lock()})
            statusLedSp->setBlinking(500);
    
        return true;
    }
    
    .

    Gallery .
    Device details panel with information about ESP32 .

    Screenshot of Visual Studio Code editor with ZigbeeGateway code. .

    Repository .
    The code is available under the MIT license at:
    https://github.com/cziter15/ksIotFrameworkLib .

    Cool? Ranking DIY
    About Author
    cziter15
    Level 11  
    Offline 
    cziter15 wrote 34 posts with rating 21, helped 2 times. Live in city Bydgoszcz. Been with us since 2013 year.
  • ADVERTISEMENT
  • #2 21424315
    _johnny_
    Level 10  
    Posts: 38
    Rate: 6
    It feels gamedev or javascript in the code, so it won't be easy. Is there any point in making another boilerplate for these poor embedded?
  • ADVERTISEMENT
  • #3 21426274
    austin007
    Level 17  
    Posts: 758
    Help: 6
    Rate: 271
    @cziter15 Cool project. Do you know how to communicate the prototype via Zigbee including for example Tuya cloud. I have a registered dev account on Tuya and don't know how it looks like in practice. Is it free to get your unique device ID (how much?). How is it outside the Tuya cloud in e.g. Home Assistant i.e. design your own device under ESP32 and communicate via Zigbee? I also have 2 pieces of good hardware BT thermostat heads and maybe it would be possible to convert to Zigbee (yes I know this may not be cost effective). Have you tried using the ESP32-c6 with the Zigbee SDK? https://github.com/espressif/esp-zigbee-sdk
  • #4 21426295
    cziter15
    Level 11  
    Posts: 34
    Help: 2
    Rate: 21
    austin007 wrote:
    How is it outside the Tuya cloud in e.g. Home Assistant i.e. design your own device under ESP32 and communicate via Zigbee?
    .

    I didn't go into too much detail because this gateway is basically a UART proxy in terms of protocol implementation.
    In any case, the EZSP API allows you to form networks, run pairings, list properties and so on.
    As for ESP and Zigbee on it - I haven't touched it yet.

    Tuya unfortunately gave me the most problems, they have some of their strange combinations when it comes to implementing binding.
    Even the ZHA Toolkit (a plug-in for HA) has a "Tuya Magic Spell" function.

    ZHA, and therefore also HomeAssistant uses the zigpy stack -> https://github.com/zigpy/zigpy

    Maybe you can find the answers there :) .
📢 Listen (AI):

FAQ

TL;DR: ksIotFrameworkLib targets 2 platforms—ESP8266 and ESP32—and one reviewer said it feels "gamedev or javascript" in style. It helps makers who want a component-based IoT stack on PlatformIO with Wi‑Fi, MQTT, HTTP management, and Zigbee-gateway-friendly structure without writing rigid embedded code from scratch. [#21422227]

Why it matters: This thread shows how to build reusable smart-home firmware faster on ESP chips while keeping configuration, runtime services, and device logic separated.

Aspect ESP8266 ESP32
Framework support Supported first Added later
Runtime base Arduino core on ESP Arduino core on ESP
Author's long-term note More likely to lose support if Arduino is removed Better fit for continued support
Zigbee mention in thread Not discussed Asked about, including ESP32-C6

Key insight: The framework’s real value is not one feature like MQTT or HTTP. It is the Unity3D-like application rotator plus components, which lets one firmware juggle device behavior, configuration mode, and services safely at runtime.

Quick Facts

  • The framework supports 2 microcontroller families: ESP8266 and ESP32, and it runs on PlatformIO with the Arduino core underneath rather than on an external AVR board. [#21422227]
  • A typical firmware is split into 2 applications: a functional device app and a configuration app that can expose its own access point. [#21422227]
  • The built-in component set includes a local HTTP device portal, MQTT connector, configuration provider/master, device statistics reporting, and a reset-button handler. [#21422227]
  • The sample initialization creates 2 LED components, binds MQTT connect and disconnect events, and starts status blinking at 500 ms. [#21422227]
  • The Zigbee gateway described in the discussion uses a UART proxy approach, so protocol handling stays limited and is not a full native Zigbee stack on the ESP itself. [#21426295]

What is ksIotFrameworkLib and how does it simplify building IoT applications on ESP8266 and ESP32 with PlatformIO?

ksIotFrameworkLib is an IoT framework for ESP8266 and ESP32 that runs on PlatformIO with the Arduino core underneath. It simplifies development by organizing firmware into applications and reusable components, so you can add Wi‑Fi, MQTT, HTTP management, reset handling, and stats reporting without hard-wired module dependencies. The author says it has powered full smart-home solutions for a few years, which suggests it is already used beyond a demo. [#21422227]

How does the application rotator in ksIotFrameworkLib work, and why was it designed in a Unity3D-like component style?

The application rotator is the main object created at startup, and it cycles through a list of applications until each finishes and the loop repeats. The Unity3D-like style matters because components can be searched by type, added or removed at runtime, and managed with locks plus weak_ptr, which reduces rigid references and improves stability on long-running devices. [#21422227]

What internal components are available in ksIotFrameworkLib, such as the HTTP device portal, MQTT connector, statistics reporter, and reset button handler?

The framework includes an HTTP device portal for local-network access, terminal functions, status display, and software updates. It also includes an MQTT connection component, configuration provider and master components, a device statistics reporter for values such as RSSI and IP, and a reset-button component that handles different RESET actions. [#21422227]

How do you initialize an ESP32 or ESP8266 application in ksIotFrameworkLib and add components like Wi-Fi, MQTT, LEDs, sensors, and a device portal?

You initialize the app by building a component list inside the application’s init() function. 1. Add core services such as Wi‑Fi, MQTT, device stats, LEDs, device portal, sensor, and reset button. 2. Lock weak pointers and bind MQTT connect and disconnect callbacks. 3. Configure runtime behavior, such as assigning an event LED and starting status blinking at 500 ms. [#21422227]

Why does this IoT framework use the Arduino core underneath while still running natively on ESP8266 and ESP32?

It uses the Arduino core mainly for familiar structures such as millis(), not because the code runs on an intermediate AVR layer. The author states that the firmware runs natively on the ESP itself, with no “atmegs” in the middle, so Arduino here is a software base rather than a separate hardware dependency. [#21422227]

ESP8266 vs ESP32 in ksIotFrameworkLib — which platform makes more sense for new IoT projects and why?

ESP32 makes more sense for new projects in this thread because support was added after ESP8266 and the author says that, if Arduino were removed, they would rather drop ESP8266 support. That does not mean ESP8266 is unsupported today, but it does show which platform appears safer for future development direction. [#21422227]

What is EZSP API in Zigbee development, and what can it do for forming networks, pairing devices, and reading properties?

“EZSP API” is an interface mentioned in the thread that lets a Zigbee solution form networks, run pairings, and list properties, making it useful for host-controlled gateway work. In this discussion, it is presented as the API layer that covers core coordinator tasks rather than as a full ESP-side Zigbee implementation. [#21426295]

What is zigpy and how does it relate to ZHA and Home Assistant when working with Zigbee devices?

“zigpy” is a Zigbee software stack referenced in the thread that underpins ZHA, which means Home Assistant uses it as a core layer for working with Zigbee devices. The practical point is that if you want to understand how Home Assistant talks Zigbee outside Tuya’s cloud, zigpy is the stack named directly in the discussion. [#21426295]

How does a Zigbee UART proxy gateway work, and what part of the Zigbee protocol is actually implemented in that approach?

A Zigbee UART proxy gateway forwards communication over UART instead of implementing the full Zigbee protocol stack directly on the ESP. The author says the gateway is “basically a UART proxy” in protocol terms, which means higher Zigbee behavior is delegated elsewhere and the ESP-side logic stays comparatively thin. [#21426295]

What makes Tuya Zigbee devices difficult to integrate, and why do people refer to a ZHA Toolkit 'Tuya Magic Spell' when dealing with binding?

Tuya Zigbee devices are difficult here because the author says Tuya uses “strange combinations” when implementing binding. That odd behavior is why the ZHA Toolkit for Home Assistant includes a function nicknamed “Tuya Magic Spell,” which highlights a real interoperability edge case rather than normal plug-and-play binding. [#21426295]

How do you design your own ESP32-based device to communicate over Zigbee with Home Assistant outside the Tuya cloud?

The thread points you toward Home Assistant’s Zigbee path through ZHA and the zigpy stack, not through Tuya cloud services. A practical takeaway is to study the ZHA/zigpy side first, because that is the software path explicitly named for pairing, properties, and Home Assistant integration when building your own device around ESP hardware. [#21426295]

What does Tuya's developer workflow look like in practice, including getting a unique device ID and understanding whether there are any fees involved?

The thread does not answer the device-ID or fee question with any concrete Tuya workflow details. It only says Tuya caused the most problems because of unusual binding behavior, so you should treat cost, unique ID issuance, and account process as unresolved in this discussion rather than assume they are free or standardized. [#21426295]

How can you convert Bluetooth thermostat heads to Zigbee, and what technical and cost trade-offs should you expect?

The thread raises Bluetooth-to-Zigbee thermostat conversion as an idea, but it gives no tested method or cost figure. The only concrete caution is that the person asking already notes it may not be cost effective, so the safe conclusion from this discussion is that feasibility remains open and economics may be the main blocker. [#21426274]

What is the best way to use ESP32-C6 with Espressif's esp-zigbee-sdk for custom Zigbee end devices or gateways?

The thread does not provide a best-practice workflow for ESP32-C6 with esp-zigbee-sdk because the author says they have not touched Zigbee on ESP yet. That is an important limit: the discussion mentions ESP32-C6 and links the SDK question, but it does not include implementation steps, code, or results for custom end devices or gateways. [#21426295]

Why might a component-based embedded framework feel like gamedev or JavaScript boilerplate, and when is that architecture actually useful on resource-constrained devices?

It can feel like boilerplate because one commenter said the code style feels “gamedev or javascript,” which implies more indirection than classic bare-metal embedded code. It becomes useful when firmware must juggle at least 2 application modes, runtime-added components, and cross-component events without brittle references, such as a smart-home node that needs device logic, configuration AP, MQTT, HTTP tools, and reset handling together. [#21424315]
Generated by the language model.
ADVERTISEMENT