Store app/dev EUI/key in NVS

This commit is contained in:
Manuel Bleichenbacher
2018-07-20 21:19:53 +02:00
parent fc34fed6de
commit 2c998f5acc
7 changed files with 218 additions and 36 deletions

View File

@ -1,3 +1,3 @@
PROJECT_NAME := hello_world
PROJECT_NAME := send_recv
include $(IDF_PATH)/make/project.mk

View File

@ -6,11 +6,12 @@
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* 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 "esp_event.h"
#include "nvs_flash.h"
#include "TheThingsNetwork.h"
@ -38,10 +39,7 @@ void sendMessages(void* pvParameter)
while (1) {
printf("Sending message...\n");
TTNResponseCode res = ttn.transmitBytes(msgData, sizeof(msgData) - 1);
if (res == kTTNSuccessfulTransmission)
printf("Message sent.\n");
else
printf("Transmission failed.\n");
printf(res == kTTNSuccessfulTransmission ? "Message sent.\n" : "Transmission failed.\n");
vTaskDelay(TX_INTERVAL * 1000 / portTICK_PERIOD_MS);
}
@ -57,7 +55,13 @@ void messageReceived(const uint8_t* message, size_t length, port_t port)
extern "C" void app_main(void)
{
gpio_install_isr_service(ESP_INTR_FLAG_IRAM);
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;
@ -68,18 +72,24 @@ extern "C" void app_main(void)
spi_bus_config.quadhd_io_num = -1;
spi_bus_config.max_transfer_sz = 0;
esp_err_t ret = spi_bus_initialize(HSPI_HOST, &spi_bus_config, 1);
assert(ret == ESP_OK);
err = spi_bus_initialize(HSPI_HOST, &spi_bus_config, 1);
ESP_ERROR_CHECK(err);
ttn.configurePins(HSPI_HOST, 18, TTN_NOT_CONNECTED, 14, 26, 33);
// 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");
ttn.join();
printf("Joined.\n");
xTaskCreate(sendMessages, "send_messages", 1024 * 4, (void* )0, 3, NULL);
if (ttn.join())
{
printf("Joined.\n");
xTaskCreate(sendMessages, "send_messages", 1024 * 4, (void* )0, 3, NULL);
}
else
{
printf("Join failed. Goodbye\n");
}
}