diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..651e526 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "components/ttn-esp32"] + path = components/ttn-esp32 + url = https://git.mosad.xyz/localhorst/ttn-esp32.git diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..a917f70 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 3.5) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) + +project(msv_node) diff --git a/README.md b/README.md index 4a852bc..b628a6c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ # msv-clubhouse-node +``` +git submodule init +git submodule update +``` + diff --git a/components/ttn-esp32 b/components/ttn-esp32 new file mode 160000 index 0000000..8777846 --- /dev/null +++ b/components/ttn-esp32 @@ -0,0 +1 @@ +Subproject commit 877784640d7976aab84398de6a5b376033fd56d1 diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt new file mode 100644 index 0000000..8aa291e --- /dev/null +++ b/main/CMakeLists.txt @@ -0,0 +1,4 @@ +idf_component_register( + SRCS "main.cpp" + INCLUDE_DIRS "." + REQUIRES ttn-esp32) diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild new file mode 100644 index 0000000..f6aab17 --- /dev/null +++ b/main/Kconfig.projbuild @@ -0,0 +1,13 @@ +menu "MSV Node" + + config APPEUI + string "AppEUI" + default "0000000000000000" + config DEVEUI + string "DevEUI" + default "0000000000000000" + config APPKEY + string "AppKey" + default "00000000000000000000000000000000" + +endmenu \ No newline at end of file diff --git a/main/main.cpp b/main/main.cpp new file mode 100644 index 0000000..f57ba0a --- /dev/null +++ b/main/main.cpp @@ -0,0 +1,112 @@ +/******************************************************************************* + * + * 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 "driver/i2c.h" + +#include "TheThingsNetwork.h" + +#define SDA_PIN GPIO_NUM_4 +#define SCL_PIN GPIO_NUM_15 + + +// NOTE: +// The LoRaWAN frequency and the radio chip must be configured by running 'idf.py menuconfig'. +// Go to Components / The Things Network, select the appropriate values and save. + +// 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 = 5; +static uint8_t msgData[] = "0xBADC0DED"; + + +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 * pdMS_TO_TICKS(1000)); + } +} + +void messageReceived(const uint8_t* message, size_t length, ttn_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(CONFIG_DEVEUI, CONFIG_APPEUI, CONFIG_APPKEY); + + // Register callback for received messages + ttn.onMessage(messageReceived); + +// ttn.setAdrEnabled(false); +// ttn.setDataRate(kTTNDataRate_US915_SF7); +// ttn.setMaxTxPower(14); + + 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"); + esp_restart(); + } +}