logo elektroda
logo elektroda
X
logo elektroda

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

cziter15 
📢 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 .

About Author
cziter15 wrote 34 posts with rating 21 , helped 2 times. Live in city Bydgoszcz. Been with us since 2013 year.

Comments

_johnny_ 04 Feb 2025 12:42

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? [Read more]

austin007 05 Feb 2025 17:57

@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... [Read more]

cziter15 05 Feb 2025 18:12

. 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... [Read more]

%}