mirror of
https://github.com/manuelbl/ttn-esp32.git
synced 2025-06-15 12:24:27 +02:00
96 lines
2.9 KiB
C++
96 lines
2.9 KiB
C++
/*******************************************************************************
|
|
* Copyright (c) 2018 Manuel Bleichenbacher
|
|
*
|
|
* All rights reserved. This program and the accompanying materials
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
* which accompanies this distribution, and is available at
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
*
|
|
* Sample program showing how to send and receive messages.
|
|
*******************************************************************************/
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "esp_event.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 = "????????????????????????????????";
|
|
|
|
|
|
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.transmitBytes(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 = 19;
|
|
spi_bus_config.mosi_io_num = 27;
|
|
spi_bus_config.sclk_io_num = 5;
|
|
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(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");
|
|
if (ttn.join())
|
|
{
|
|
printf("Joined.\n");
|
|
xTaskCreate(sendMessages, "send_messages", 1024 * 4, (void* )0, 3, NULL);
|
|
}
|
|
else
|
|
{
|
|
printf("Join failed. Goodbye\n");
|
|
}
|
|
}
|