Skip to content

Commit

Permalink
Init: Add node code. Copy paste from EspNowNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
Johboh committed Aug 11, 2024
1 parent fc5c315 commit ba718a2
Show file tree
Hide file tree
Showing 32 changed files with 2,071 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BreakBeforeBraces: Attach
ColumnLimit: 120
17 changes: 17 additions & 0 deletions .github/workflows/clang-format.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Clang Format CI
on: [workflow_call, push]
jobs:
formatting-check:
name: Formatting Check
runs-on: ubuntu-latest
strategy:
matrix:
path:
- 'src'
steps:
- uses: actions/checkout@v3
- name: Run clang-format style check for C/C++/Protobuf programs.
uses: jidicula/clang-format-action@v4.11.0
with:
clang-format-version: '13'
check-path: ${{ matrix.path }}
28 changes: 28 additions & 0 deletions .github/workflows/esp_upload_component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Push EspNowNetworkNode to Espressif Component Service
on:
release:
types: [created]
jobs:
build_examples_for_verification:
uses: ./.github/workflows/espidf.yaml
with:
target_path: "examples/espidf"

upload_components:
runs-on: ubuntu-latest
needs: [build_examples_for_verification]
steps:
- uses: actions/checkout@v4.1.1

- name: Remove arduino examples and library related files
run: rm -rf examples/arduino library.json library.properties

- name: Remove github actions
run: rm -rf .github

- name: Upload EspNowNetworkNode to component registry
uses: espressif/upload-components-ci-action/@v1
with:
name: "EspNowNetworkNode"
namespace: "johboh"
api_token: ${{ secrets.ESP_IDF_COMPONENT_API_TOKEN }}
45 changes: 45 additions & 0 deletions .github/workflows/espidf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: ESP-IDF CI

on:
workflow_call:
inputs:
target_path:
type: string
description: 'Path value to select a specific target in the matrix'
required: true
push:

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
version:
- name: v44_build
version_number: release-v4.4
target: esp32c3
- name: v51_build
version_number: release-v5.1
target: esp32c6
- name: v52_build
version_number: release-v5.2
target: esp32c6
- name: v53_build
version_number: release-v5.3
target: esp32c6
path:
- name: examples/espidf

steps:
- if: github.event_name == 'workflow_call' && matrix.path.name != inputs.target_path
run: exit 0

- uses: actions/checkout@v4.1.1

- name: ESP-IDF Build
uses: espressif/esp-idf-ci-action@v1
with:
esp_idf_version: ${{ matrix.version.version_number }}
target: ${{ matrix.version.target }}
path: ${{ matrix.path.name }}
56 changes: 56 additions & 0 deletions .github/workflows/platformio.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: PlatformIO CI

on:
workflow_call:
inputs:
target_path:
type: string
description: 'Path value to select a specific target in the matrix'
required: true
push:

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
target:
- path: examples/arduino
library: library.json
remove: ""
extra_dependencies: ""

steps:
- if: github.event_name == 'workflow_call' && matrix.target.path != inputs.target_path
run: exit 0

- uses: actions/checkout@v4.1.1

- uses: actions/cache@v3
with:
path: |
~/.cache/pip
~/.platformio/.cache
key: ${{ runner.os }}-pio

- uses: actions/setup-python@v4
with:
python-version: '3.9'

- name: Install PlatformIO Core
run: pip install --upgrade platformio

- name: Remove files
run: rm -rf ${{ matrix.target.remove }}
if: ${{ matrix.target.remove }}

- name: Copy library.json to root (if not src is also in root)
run: mv ${{ matrix.target.library }} ./ || true
if: ${{ matrix.target.library != './library.json' }}

- name: Build PlatformIO targets
run: ${{ env.PLATFORMIO_CI_BASE_CMD }} --project-option="lib_deps=${{ matrix.target.extra_dependencies }}"
env:
PLATFORMIO_CI_BASE_CMD: pio ci --lib="." --board=lolin_c3_mini --project-option="build_unflags=-std=gnu++11" --project-option="build_flags=-std=gnu++17" --project-option="platform=espressif32@6.4.0" --project-option="lib_ldf_mode=deep"
PLATFORMIO_CI_SRC: ${{ matrix.target.path }}
29 changes: 29 additions & 0 deletions .github/workflows/platformio_publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Push EspNowNetworkNode to PlatformIO registry
on:
release:
types: [created]
jobs:
build_examples_for_verification:
uses: ./.github/workflows/platformio.yaml
with:
target_path: "examples/arduino"

upload_library:
runs-on: ubuntu-latest
needs: [build_examples_for_verification]
steps:
- uses: actions/checkout@v4.1.1
- uses: actions/setup-python@v4
with:
python-version: '3.9'

- name: ESP IDF CMake stuff
run: rm -rf CMakeLists.txt idf_component.yml

- name: Install PlatformIO Core
run: pip install --upgrade platformio

- name: Publish PlatformIO library
run: pio pkg publish --owner johboh --no-notify --no-interactive
env:
PLATFORMIO_AUTH_TOKEN: ${{ secrets.PLATFORMIO_AUTH_TOKEN }}
13 changes: 13 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: PR verification
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
build_espidf_examples_for_verification:
uses: ./.github/workflows/espidf.yaml

build_platformio_examples_for_verification:
uses: ./.github/workflows/platformio.yaml

formatting_check:
uses: ./.github/workflows/clang-format.yaml
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
/.idea
/.build
/.pio
/out
/.vscode
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FILE(GLOB_RECURSE sources "./src/impl/*.*")

if(IDF_VERSION_MAJOR GREATER_EQUAL 5)
set(required_components mbedtls esp_wifi esp_http_client esp_partition esp-tls nvs_flash bootloader_support app_update)
else()
set(required_components mbedtls esp_wifi esp_http_client esp-tls nvs_flash bootloader_support app_update)
endif()

idf_component_register(COMPONENT_NAME "esp_now_network_node"
SRCS ${sources}
INCLUDE_DIRS "./src/"
REQUIRES ${required_components})


if(IDF_VERSION_MAJOR LESS 5) # 5+ compiles with c++23.
target_compile_options(${COMPONENT_LIB} PRIVATE -std=gnu++17)
endif()
108 changes: 108 additions & 0 deletions examples/arduino/arduino.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <Arduino.h>
#include <EspNowCrypt.h>
#include <EspNowNode.h>
#include <EspNowPreferences.h>
#include <esp_crt_bundle.h>

#define SLEEP_TIME_US (1000LL * 1000LL * 60LL * 1LL) // 1 minute

// Should be generated by build script.
#define FIRMWARE_VERSION 90201

// These structs are the application messages shared across the host and node device.
// Ideally these should live in a shared header file.
#pragma pack(1)
struct MyApplicationMessage {
uint8_t id = 0x01;
double temperature;
};
#pragma pack(0)

// Encyption key used for our own packet encryption (GCM).
// We are not using the esp-now encryption due to the peer limitation.
// The key should be the same for both the host and the node.
const char esp_now_encryption_key[] = "0123456789ABCDEF"; // Must be exact 16 bytes long. \0 does not count.

// Used to validate the integrity of the messages.
// The secret should be the same for both the host and the node.
const char esp_now_encryption_secret[] = "01234567"; // Must be exact 8 bytes long. \0 does not count.

unsigned long _turn_of_led_at_ms = 0;

// Callback for logging. Can be omitted.
EspNowNode::OnLog _on_log = [](const std::string message, const esp_log_level_t log_level) {
if (log_level == ESP_LOG_NONE) {
return; // Weird flex, but ok
}

std::string level;
switch (log_level) {
case ESP_LOG_NONE:
level = "none";
break;
case ESP_LOG_ERROR:
level = "error";
break;
case ESP_LOG_WARN:
level = "warning";
break;
case ESP_LOG_INFO:
level = "info";
break;
case ESP_LOG_DEBUG:
level = "debug";
break;
case ESP_LOG_VERBOSE:
level = "verbose";
break;
default:
level = "unknown";
break;
}

Serial.println(("EspNowNode (" + level + "): " + message).c_str());
};

// Status callback. Can be omitted.
EspNowNode::OnStatus _on_status = [](EspNowNode::Status status) {
switch (status) {
case EspNowNode::Status::HOST_DISCOVERY_STARTED:
break;
case EspNowNode::Status::HOST_DISCOVERY_SUCCESSFUL:
break;
case EspNowNode::Status::HOST_DISCOVERY_FAILED:
break;
case EspNowNode::Status::INVALID_HOST:
break;
case EspNowNode::Status::FIRMWARE_UPDATE_STARTED:
break;
case EspNowNode::Status::FIRMWARE_UPDATE_SUCCESSFUL:
break;
case EspNowNode::Status::FIRMWARE_UPDATE_FAILED:
break;
case EspNowNode::Status::FIRMWARE_UPDATE_WIFI_SETUP_FAILED:
break;
}
};

EspNowPreferences _esp_now_preferences;
EspNowCrypt _esp_now_crypt(esp_now_encryption_key, esp_now_encryption_secret);
EspNowNode _esp_now_node(_esp_now_crypt, _esp_now_preferences, FIRMWARE_VERSION, _on_status, _on_log,
arduino_esp_crt_bundle_attach);

void setup() {
Serial.begin(115200);

_esp_now_preferences.initalizeNVS();

// Setup node, send message, and then go to sleep.
if (_esp_now_node.setup()) {
MyApplicationMessage message;
message.temperature = 25.6;
_esp_now_node.sendMessage(&message, sizeof(MyApplicationMessage));
}

esp_deep_sleep(SLEEP_TIME_US);
}

void loop() {}
5 changes: 5 additions & 0 deletions examples/espidf/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(example)
8 changes: 8 additions & 0 deletions examples/espidf/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#

PROJECT_NAME := example

include $(IDF_PATH)/make/project.mk
8 changes: 8 additions & 0 deletions examples/espidf/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FILE(GLOB_RECURSE app_sources *.*)

idf_component_register(
SRCS ${app_sources}
INCLUDE_DIRS "."
)

target_compile_options(${COMPONENT_LIB} PRIVATE -std=gnu++17)
4 changes: 4 additions & 0 deletions examples/espidf/main/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#
# "main" pseudo-component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
3 changes: 3 additions & 0 deletions examples/espidf/main/idf_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies:
EspNowNetworkNode:
path: ../../../.
Loading

0 comments on commit ba718a2

Please sign in to comment.