Skip to content

Commit

Permalink
Examples: rename existing, add example for local broker bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidB137 committed Oct 30, 2023
1 parent 6d82a39 commit 87b2894
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 0 deletions.
File renamed without changes.
6 changes: 6 additions & 0 deletions examples/espidf/bridge-espnow-local-broker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SPSP bridge example

Simple example of how to integrate SPSP framework into ESP-IDF and create
*bridge* node.

Uses ESP-NOW as *local layer* and [local broker](/#local-broker-far-layer) as *far layer*.
File renamed without changes.
67 changes: 67 additions & 0 deletions examples/espidf/bridge-espnow-local-broker/main/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "spsp.hpp"

extern "C" void app_main()
{
/**
* Config
*/

// ESPNOW config
SPSP::LocalLayers::ESPNOW::Config espnowConfig = {};
espnowConfig.ssid = 0x00000000;
espnowConfig.password = "12345678123456781234567812345678";


/**
* Init
*/

// Initialize WiFi
static SPSP::WiFi::Station wifi{{}};

// Set WiFi channel
//
// As we use `LocalBroker` far layer, we don't need IP connectivity at all
// and thus WiFi will be used purely by ESP-NOW to receive and send data
// to clients.
// We can set any WiFi channel we want (allowed by local law) and clients
// will discover this bridge automatically.
//
// Remember, however, to use `wifi.setChannelRestrictions(...)` method on
// both bridge and client, if you intend to use channels 12 or 13.
wifi.setChannel(7);

// Create layers
static SPSP::LocalLayers::ESPNOW::Adapter llAdapter{};
static SPSP::LocalLayers::ESPNOW::ESPNOW ll{llAdapter, wifi, espnowConfig};
static SPSP::FarLayers::LocalBroker::LocalBroker fl{};

// Create bridge
static SPSP::Nodes::Bridge spsp{&ll, &fl};

/**
* Publish and subscribe
* API is the same as for the client.
* Typically, you publish and subscribe on clients only,
* but you can do the same on bridges too.
*/

bool delivered = spsp.publish("topic", "payload");

if (delivered) {
// ...
} else {
// ...
}

delivered = spsp.subscribe(
"topic2",
[](const std::string& topic, const std::string& payload) {
// Do something when data on `topic2` are received
// ...
}
);

// We don't need this subscription anymore
delivered = spsp.unsubscribe("topic2");
}
File renamed without changes.
5 changes: 5 additions & 0 deletions examples/espidf/bridge-espnow-mqtt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 3.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)

project(spsp_bridge_example)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions examples/espidf/client-espnow/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
idf_component_register(SRCS "main.cpp"
INCLUDE_DIRS ".")
3 changes: 3 additions & 0 deletions examples/espidf/client-espnow/main/idf_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies:
spsp:
git: https://github.com/DavidB137/spsp.git
File renamed without changes.
2 changes: 2 additions & 0 deletions examples/espidf/client-espnow/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_COMPILER_CXX_EXCEPTIONS=y

0 comments on commit 87b2894

Please sign in to comment.