logo elektroda
logo elektroda
X
logo elektroda

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

cziter15 1050 3
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 9  
    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  
    @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  
    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):
ADVERTISEMENT