From e15d936bb4db1088325f88f1bc0d9a5148ec50a0 Mon Sep 17 00:00:00 2001 From: Manuel Bl <10954524+manuelbl@users.noreply.github.com> Date: Sat, 1 Aug 2020 10:23:30 +0200 Subject: [PATCH] Merge send_recv example into hello_world --- examples/hello_world/CMakeLists.txt | 11 ++- examples/hello_world/main/CMakeLists.txt | 8 +- examples/hello_world/main/main.cpp | 23 +++-- examples/mac_address/main/main.cpp | 2 +- examples/provisioning/main/main.cpp | 2 +- examples/send_recv/.gitignore | 5 -- examples/send_recv/CMakeLists.txt | 8 -- examples/send_recv/Makefile | 5 -- examples/send_recv/main/CMakeLists.txt | 4 - examples/send_recv/main/component.mk | 0 examples/send_recv/main/main.cpp | 109 ----------------------- examples/send_recv/platformio.ini | 10 --- src/TheThingsNetwork.cpp | 2 +- 13 files changed, 29 insertions(+), 160 deletions(-) delete mode 100644 examples/send_recv/.gitignore delete mode 100644 examples/send_recv/CMakeLists.txt delete mode 100644 examples/send_recv/Makefile delete mode 100644 examples/send_recv/main/CMakeLists.txt delete mode 100644 examples/send_recv/main/component.mk delete mode 100644 examples/send_recv/main/main.cpp delete mode 100644 examples/send_recv/platformio.ini diff --git a/examples/hello_world/CMakeLists.txt b/examples/hello_world/CMakeLists.txt index d9b69cd..63bda8b 100644 --- a/examples/hello_world/CMakeLists.txt +++ b/examples/hello_world/CMakeLists.txt @@ -1,9 +1,8 @@ -# The following 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) - -get_filename_component(TTN_DIR ../.. ABSOLUTE) -set(EXTRA_COMPONENT_DIRS "${TTN_DIR}") - include($ENV{IDF_PATH}/tools/cmake/project.cmake) + +# Update the below line to match the path to the ttn-esp32 library, +# e.g. list(APPEND EXTRA_COMPONENT_DIRS "/Users/me/Documents/ttn-esp32") +list(APPEND EXTRA_COMPONENT_DIRS "../..") + project(hello_world) diff --git a/examples/hello_world/main/CMakeLists.txt b/examples/hello_world/main/CMakeLists.txt index e931882..8aa291e 100644 --- a/examples/hello_world/main/CMakeLists.txt +++ b/examples/hello_world/main/CMakeLists.txt @@ -1,4 +1,4 @@ -set(COMPONENT_SRCS "main.cpp") -set(COMPONENT_ADD_INCLUDEDIRS "") - -register_component() +idf_component_register( + SRCS "main.cpp" + INCLUDE_DIRS "." + REQUIRES ttn-esp32) diff --git a/examples/hello_world/main/main.cpp b/examples/hello_world/main/main.cpp index a96fe5a..ef314df 100644 --- a/examples/hello_world/main/main.cpp +++ b/examples/hello_world/main/main.cpp @@ -7,12 +7,12 @@ * Licensed under MIT License * https://opensource.org/licenses/MIT * - * Sample program showing how to send a test message every 30 second. + * Sample program showing how to send and receive messages. *******************************************************************************/ #include "freertos/FreeRTOS.h" -#include "driver/gpio.h" #include "esp_event.h" +#include "driver/gpio.h" #include "nvs_flash.h" #include "TheThingsNetwork.h" @@ -37,9 +37,9 @@ const char *appKey = "????????????????????????????????"; #define TTN_PIN_SPI_MISO 19 #define TTN_PIN_NSS 18 #define TTN_PIN_RXTX TTN_NOT_CONNECTED -#define TTN_PIN_RST TTN_NOT_CONNECTED +#define TTN_PIN_RST 14 #define TTN_PIN_DIO0 26 -#define TTN_PIN_DIO1 33 +#define TTN_PIN_DIO1 35 static TheThingsNetwork ttn; @@ -54,17 +54,25 @@ void sendMessages(void* pvParameter) TTNResponseCode res = ttn.transmitMessage(msgData, sizeof(msgData) - 1); printf(res == kTTNSuccessfulTransmission ? "Message sent.\n" : "Transmission failed.\n"); - vTaskDelay(TX_INTERVAL * 1000 / portTICK_PERIOD_MS); + vTaskDelay(TX_INTERVAL * pdMS_TO_TICKS(1000)); } } +void messageReceived(const uint8_t* message, size_t length, port_t port) +{ + printf("Message of %d bytes received on port %d:", length, port); + for (int i = 0; i < length; i++) + printf(" %02x", message[i]); + printf("\n"); +} + extern "C" void app_main(void) { esp_err_t err; // Initialize the GPIO ISR handler service err = gpio_install_isr_service(ESP_INTR_FLAG_IRAM); ESP_ERROR_CHECK(err); - + // Initialize the NVS (non-volatile storage) for saving and restoring the keys err = nvs_flash_init(); ESP_ERROR_CHECK(err); @@ -86,6 +94,9 @@ extern "C" void app_main(void) // The below line can be commented after the first run as the data is saved in NVS ttn.provision(devEui, appEui, appKey); + // Register callback for received messages + ttn.onMessage(messageReceived); + printf("Joining...\n"); if (ttn.join()) { diff --git a/examples/mac_address/main/main.cpp b/examples/mac_address/main/main.cpp index 8737b77..4b13fa7 100644 --- a/examples/mac_address/main/main.cpp +++ b/examples/mac_address/main/main.cpp @@ -51,7 +51,7 @@ void sendMessages(void* pvParameter) TTNResponseCode res = ttn.transmitMessage(msgData, sizeof(msgData) - 1); printf(res == kTTNSuccessfulTransmission ? "Message sent.\n" : "Transmission failed.\n"); - vTaskDelay(TX_INTERVAL * 1000 / portTICK_PERIOD_MS); + vTaskDelay(TX_INTERVAL * pdMS_TO_TICKS(1000)); } } diff --git a/examples/provisioning/main/main.cpp b/examples/provisioning/main/main.cpp index b0426b3..e50333c 100644 --- a/examples/provisioning/main/main.cpp +++ b/examples/provisioning/main/main.cpp @@ -46,7 +46,7 @@ void sendMessages(void* pvParameter) TTNResponseCode res = ttn.transmitMessage(msgData, sizeof(msgData) - 1); printf(res == kTTNSuccessfulTransmission ? "Message sent.\n" : "Transmission failed.\n"); - vTaskDelay(TX_INTERVAL * 1000 / portTICK_PERIOD_MS); + vTaskDelay(TX_INTERVAL * pdMS_TO_TICKS(1000)); } } diff --git a/examples/send_recv/.gitignore b/examples/send_recv/.gitignore deleted file mode 100644 index 89cc49c..0000000 --- a/examples/send_recv/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -.pio -.vscode/.browse.c_cpp.db* -.vscode/c_cpp_properties.json -.vscode/launch.json -.vscode/ipch diff --git a/examples/send_recv/CMakeLists.txt b/examples/send_recv/CMakeLists.txt deleted file mode 100644 index 49aac14..0000000 --- a/examples/send_recv/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -include($ENV{IDF_PATH}/tools/cmake/project.cmake) - -# Update the below line to match the path to the ttn-esp32 library, -# e.g. list(APPEND EXTRA_COMPONENT_DIRS "/Users/me/Documents/ttn-esp32") -list(APPEND EXTRA_COMPONENT_DIRS "../..") - -project(send_recv) diff --git a/examples/send_recv/Makefile b/examples/send_recv/Makefile deleted file mode 100644 index b9ba20a..0000000 --- a/examples/send_recv/Makefile +++ /dev/null @@ -1,5 +0,0 @@ -PROJECT_NAME := send_recv - -EXTRA_COMPONENT_DIRS := $(abspath ../..) - -include $(IDF_PATH)/make/project.mk diff --git a/examples/send_recv/main/CMakeLists.txt b/examples/send_recv/main/CMakeLists.txt deleted file mode 100644 index 8aa291e..0000000 --- a/examples/send_recv/main/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -idf_component_register( - SRCS "main.cpp" - INCLUDE_DIRS "." - REQUIRES ttn-esp32) diff --git a/examples/send_recv/main/component.mk b/examples/send_recv/main/component.mk deleted file mode 100644 index e69de29..0000000 diff --git a/examples/send_recv/main/main.cpp b/examples/send_recv/main/main.cpp deleted file mode 100644 index 0f2132c..0000000 --- a/examples/send_recv/main/main.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/******************************************************************************* - * - * ttn-esp32 - The Things Network device library for ESP-IDF / SX127x - * - * Copyright (c) 2018 Manuel Bleichenbacher - * - * Licensed under MIT License - * https://opensource.org/licenses/MIT - * - * Sample program showing how to send and receive messages. - *******************************************************************************/ - -#include "freertos/FreeRTOS.h" -#include "esp_event.h" -#include "driver/gpio.h" -#include "nvs_flash.h" - -#include "TheThingsNetwork.h" - -// NOTE: -// The LoRaWAN frequency and the radio chip must be configured by running 'make menuconfig'. -// Go to Components / The Things Network, select the appropriate values and save. - -// Copy the below hex string from the "Device EUI" field -// on your device's overview page in the TTN console. -const char *devEui = "????????????????"; - -// Copy the below two lines from bottom of the same page -const char *appEui = "????????????????"; -const char *appKey = "????????????????????????????????"; - -// Pins and other resources -#define TTN_SPI_HOST HSPI_HOST -#define TTN_SPI_DMA_CHAN 1 -#define TTN_PIN_SPI_SCLK 5 -#define TTN_PIN_SPI_MOSI 27 -#define TTN_PIN_SPI_MISO 19 -#define TTN_PIN_NSS 18 -#define TTN_PIN_RXTX TTN_NOT_CONNECTED -#define TTN_PIN_RST 14 -#define TTN_PIN_DIO0 26 -#define TTN_PIN_DIO1 35 - -static TheThingsNetwork ttn; - -const unsigned TX_INTERVAL = 30; -static uint8_t msgData[] = "Hello, world"; - - -void sendMessages(void* pvParameter) -{ - while (1) { - printf("Sending message...\n"); - TTNResponseCode res = ttn.transmitMessage(msgData, sizeof(msgData) - 1); - printf(res == kTTNSuccessfulTransmission ? "Message sent.\n" : "Transmission failed.\n"); - - vTaskDelay(TX_INTERVAL * 1000 / portTICK_PERIOD_MS); - } -} - -void messageReceived(const uint8_t* message, size_t length, port_t port) -{ - printf("Message of %d bytes received on port %d:", length, port); - for (int i = 0; i < length; i++) - printf(" %02x", message[i]); - printf("\n"); -} - -extern "C" void app_main(void) -{ - esp_err_t err; - // Initialize the GPIO ISR handler service - err = gpio_install_isr_service(ESP_INTR_FLAG_IRAM); - ESP_ERROR_CHECK(err); - - // Initialize the NVS (non-volatile storage) for saving and restoring the keys - err = nvs_flash_init(); - ESP_ERROR_CHECK(err); - - // Initialize SPI bus - spi_bus_config_t spi_bus_config; - spi_bus_config.miso_io_num = TTN_PIN_SPI_MISO; - spi_bus_config.mosi_io_num = TTN_PIN_SPI_MOSI; - spi_bus_config.sclk_io_num = TTN_PIN_SPI_SCLK; - spi_bus_config.quadwp_io_num = -1; - spi_bus_config.quadhd_io_num = -1; - spi_bus_config.max_transfer_sz = 0; - err = spi_bus_initialize(TTN_SPI_HOST, &spi_bus_config, TTN_SPI_DMA_CHAN); - ESP_ERROR_CHECK(err); - - // Configure the SX127x pins - ttn.configurePins(TTN_SPI_HOST, TTN_PIN_NSS, TTN_PIN_RXTX, TTN_PIN_RST, TTN_PIN_DIO0, TTN_PIN_DIO1); - - // The below line can be commented after the first run as the data is saved in NVS - ttn.provision(devEui, appEui, appKey); - - ttn.onMessage(messageReceived); - - printf("Joining...\n"); - if (ttn.join()) - { - printf("Joined.\n"); - xTaskCreate(sendMessages, "send_messages", 1024 * 4, (void* )0, 3, nullptr); - } - else - { - printf("Join failed. Goodbye\n"); - } -} diff --git a/examples/send_recv/platformio.ini b/examples/send_recv/platformio.ini deleted file mode 100644 index 9d18874..0000000 --- a/examples/send_recv/platformio.ini +++ /dev/null @@ -1,10 +0,0 @@ -[platformio] -src_dir = main - -[env:esp32dev] -platform = espressif32 -board = esp32dev -framework = espidf -build_flags = -Wno-expansion-to-defined -upload_port = /dev/cu.usb* -monitor_speed = 115200 diff --git a/src/TheThingsNetwork.cpp b/src/TheThingsNetwork.cpp index 0aadb87..60d9302 100644 --- a/src/TheThingsNetwork.cpp +++ b/src/TheThingsNetwork.cpp @@ -169,7 +169,7 @@ void TheThingsNetwork::waitForProvisioning() } while (!provisioning.haveKeys()) - vTaskDelay(1000 / portTICK_PERIOD_MS); + vTaskDelay(pdMS_TO_TICKS(1000)); ESP_LOGI(TAG, "Device successfully provisioned"); #else