Skip to content

Commit

Permalink
Add example app
Browse files Browse the repository at this point in the history
  • Loading branch information
cziter15 authored May 25, 2024
1 parent 42b04cf commit 809e2e2
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 0 deletions.
40 changes: 40 additions & 0 deletions examples/basic-config/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp8266dev]

# Define platform and framework.
platform = espressif8266
board = esp12e
framework = arduino

# Define cpu and flash configuration.
upload_speed = 921600
monitor_speed = 9600
board_build.f_cpu = 80000000L
board_build.f_flash = 80000000L
board_build.flash_mode = qio
board_build.ldscript = eagle.flash.4m1m.ld

# Define library dependencies.
lib_deps =
ksIotFrameworkLib=https://github.com/cziter15/ksIotFrameworkLib

# Define build flags.
build_flags =
-DPIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH
-DPIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK305

# Define OTA parameters.
upload_protocol = espota
upload_port = BasicConfigDevice.local
upload_flags =
--port=8266
--auth=ota_ksiotframework
22 changes: 22 additions & 0 deletions examples/basic-config/src/apps/ConfigApp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "ConfigApp.h"
#include "board.h"

namespace apps
{
const char ConfigApp::myDeviceName[] = "ExampleDevice";

bool ConfigApp::init()
{
addComponent<ksf::comps::ksWifiConfigurator>(myDeviceName);
addComponent<ksf::comps::ksMqttConfigProvider>();

addComponent<ksf::comps::ksLed>(CFG_STATUS_LED);

return true;
}

bool ConfigApp::loop()
{
return ksApplication::loop();
}
}
15 changes: 15 additions & 0 deletions examples/basic-config/src/apps/ConfigApp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <ksIotFrameworkLib.h>

namespace apps
{
class ConfigApp : public ksf::ksApplication
{
public:
static const char myDeviceName[];

bool init() override;
bool loop() override;
};
}
55 changes: 55 additions & 0 deletions examples/basic-config/src/apps/DeviceFunctionsApp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "ConfigApp.h"
#include "DeviceFunctionsApp.h"

#include "../board.h"

namespace apps
{
bool DeviceFunctionsApp::init()
{
/* Create required components (Wifi and Mqtt debug). */
addComponent<ksf::comps::ksWifiConnector>(ConfigApp::myDeviceName);
addComponent<ksf::comps::ksDevStatMqttReporter>();

/* Create mqttConnector component. */
mqttConnWp = addComponent<ksf::comps::ksMqttConnector>();

/* Create LED components. */
wifiLedWp = addComponent<ksf::comps::ksLed>(CFG_STATUS_LED);

/* Bind to MQTT callbacks. */
if (auto mqttConnSp{mqttConnWp.lock()})
{
mqttConnSp->onConnected->registerEvent(connEventHandleSp, std::bind(&DeviceFunctionsApp::onMqttConnected, this));
mqttConnSp->onDisconnected->registerEvent(disEventHandleSp, std::bind(&DeviceFunctionsApp::onMqttDisconnected, this));
}

/* Start LED blinking on finished init. */
if (auto statusLed_sp{wifiLedWp.lock()})
statusLed_sp->setBlinking(500);

/* Application finished initialization, return true as it succedeed. */
return true;
}

void DeviceFunctionsApp::onMqttDisconnected()
{
if (auto wifiLedSp{wifiLedWp.lock()})
wifiLedSp->setBlinking(500);
}

void DeviceFunctionsApp::onMqttConnected()
{
if (auto wifiLedSp{wifiLedWp.lock()})
wifiLedSp->setBlinking(0);
}

bool DeviceFunctionsApp::loop()
{
/*
Return to superclass application loop.
It handles all our components and whole app logic.
*/
return ksApplication::loop();
}
}
23 changes: 23 additions & 0 deletions examples/basic-config/src/apps/DeviceFunctionsApp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <ksIotFrameworkLib.h>

namespace apps
{
class DeviceFunctionsApp : public ksf::ksApplication
{
protected:

std::weak_ptr<ksf::comps::ksLed> wifiLedWp; // Weeak pointer to WiFi status LED component.
std::weak_ptr<ksf::comps::ksMqttConnector> mqttConnWp; // Weeak pointer to MQTT component.

std::unique_ptr<ksf::evt::ksEventHandle> connEventHandleSp, disEventHandleSp; // Handles to MQTT events.

void onMqttConnected();
void onMqttDisconnected();

public:
bool init() override;
bool loop() override;
};
}
3 changes: 3 additions & 0 deletions examples/basic-config/src/board.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

#define CFG_STATUS_LED 6
10 changes: 10 additions & 0 deletions examples/basic-config/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "apps/ConfigApp.h"
#include "apps/DeviceFunctionsApp.h"

using namespace apps;

KSF_IMPLEMENT_APP_ROTATOR
(
ConfigApp,
DeviceFunctionsApp
)

0 comments on commit 809e2e2

Please sign in to comment.