mirror of
https://github.com/manuelbl/ttn-esp32.git
synced 2025-06-15 12:24:27 +02:00
Fix ttn_wait_for_idle for MAC cmd processing
This commit is contained in:
parent
e71d584fca
commit
0130928601
@ -5,4 +5,6 @@ include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
|||||||
# e.g. list(APPEND EXTRA_COMPONENT_DIRS "/Users/me/Documents/ttn-esp32")
|
# e.g. list(APPEND EXTRA_COMPONENT_DIRS "/Users/me/Documents/ttn-esp32")
|
||||||
list(APPEND EXTRA_COMPONENT_DIRS "../..")
|
list(APPEND EXTRA_COMPONENT_DIRS "../..")
|
||||||
|
|
||||||
project(shutdown)
|
#add_definitions(-DLMIC_ENABLE_event_logging=1)
|
||||||
|
|
||||||
|
project(deep_sleep)
|
@ -2,22 +2,22 @@
|
|||||||
*
|
*
|
||||||
* ttn-esp32 - The Things Network device library for ESP-IDF / SX127x
|
* ttn-esp32 - The Things Network device library for ESP-IDF / SX127x
|
||||||
*
|
*
|
||||||
* Copyright (c) 2018 Manuel Bleichenbacher
|
* Copyright (c) 2021 Manuel Bleichenbacher
|
||||||
*
|
*
|
||||||
* Licensed under MIT License
|
* Licensed under MIT License
|
||||||
* https://opensource.org/licenses/MIT
|
* https://opensource.org/licenses/MIT
|
||||||
*
|
*
|
||||||
* Sample program showing how to send and receive messages.
|
* Sample program sending messages and going to deep sleep in-between.
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
#include "freertos/FreeRTOS.h"
|
|
||||||
#include "esp_event.h"
|
|
||||||
#include "driver/gpio.h"
|
#include "driver/gpio.h"
|
||||||
|
#include "esp_event.h"
|
||||||
|
#include "esp_sleep.h"
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
|
|
||||||
#include "TheThingsNetwork.h"
|
#include "TheThingsNetwork.h"
|
||||||
|
|
||||||
|
|
||||||
// NOTE:
|
// NOTE:
|
||||||
// The LoRaWAN frequency and the radio chip must be configured by running 'idf.py menuconfig'.
|
// 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.
|
// Go to Components / The Things Network, select the appropriate values and save.
|
||||||
@ -32,7 +32,6 @@ const char *devEui = "????????????????";
|
|||||||
// AppKey
|
// AppKey
|
||||||
const char *appKey = "????????????????????????????????";
|
const char *appKey = "????????????????????????????????";
|
||||||
|
|
||||||
|
|
||||||
// Pins and other resources
|
// Pins and other resources
|
||||||
#define TTN_SPI_HOST HSPI_HOST
|
#define TTN_SPI_HOST HSPI_HOST
|
||||||
#define TTN_SPI_DMA_CHAN 1
|
#define TTN_SPI_DMA_CHAN 1
|
||||||
@ -47,56 +46,10 @@ const char *appKey = "????????????????????????????????";
|
|||||||
|
|
||||||
static TheThingsNetwork ttn;
|
static TheThingsNetwork ttn;
|
||||||
|
|
||||||
const unsigned TX_INTERVAL = 30;
|
const unsigned TX_INTERVAL = 60;
|
||||||
static uint8_t msgData[] = "Hello, world";
|
static uint8_t msgData[] = "Hello, world";
|
||||||
|
|
||||||
bool join()
|
void messageReceived(const uint8_t *message, size_t length, ttn_port_t port)
|
||||||
{
|
|
||||||
printf("Joining...\n");
|
|
||||||
if (ttn.join())
|
|
||||||
{
|
|
||||||
printf("Joined.\n");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("Join failed. Goodbye\n");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void sendMessages(void* pvParameter)
|
|
||||||
{
|
|
||||||
while (1) {
|
|
||||||
|
|
||||||
// Send 2 messages
|
|
||||||
for (int i = 0; i < 2; i++)
|
|
||||||
{
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
// shutdown
|
|
||||||
ttn.shutdown();
|
|
||||||
|
|
||||||
// go to sleep
|
|
||||||
printf("Sleeping for 30s...\n");
|
|
||||||
vTaskDelay(pdMS_TO_TICKS(30000));
|
|
||||||
|
|
||||||
// startup
|
|
||||||
ttn.startup();
|
|
||||||
|
|
||||||
// join again
|
|
||||||
if (!join())
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void messageReceived(const uint8_t* message, size_t length, ttn_port_t port)
|
|
||||||
{
|
{
|
||||||
printf("Message of %d bytes received on port %d:", length, port);
|
printf("Message of %d bytes received on port %d:", length, port);
|
||||||
for (int i = 0; i < length; i++)
|
for (int i = 0; i < length; i++)
|
||||||
@ -132,10 +85,42 @@ extern "C" void app_main(void)
|
|||||||
// The below line can be commented after the first run as the data is saved in NVS
|
// The below line can be commented after the first run as the data is saved in NVS
|
||||||
ttn.provision(devEui, appEui, appKey);
|
ttn.provision(devEui, appEui, appKey);
|
||||||
|
|
||||||
|
// Register callback for received messages
|
||||||
ttn.onMessage(messageReceived);
|
ttn.onMessage(messageReceived);
|
||||||
|
|
||||||
if (join())
|
// ttn.setAdrEnabled(false);
|
||||||
|
// ttn.setDataRate(kTTNDataRate_US915_SF7);
|
||||||
|
// ttn.setMaxTxPower(14);
|
||||||
|
|
||||||
|
if (ttn.resumeAfterDeepSleep())
|
||||||
{
|
{
|
||||||
xTaskCreate(sendMessages, "send_messages", 1024 * 4, (void* )0, 3, nullptr);
|
printf("Resumed from deep sleep.\n");
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Joining...\n");
|
||||||
|
if (ttn.join())
|
||||||
|
{
|
||||||
|
printf("Joined.\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Join failed. Goodbye\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Sending message...\n");
|
||||||
|
TTNResponseCode res = ttn.transmitMessage(msgData, sizeof(msgData) - 1);
|
||||||
|
printf(res == kTTNSuccessfulTransmission ? "Message sent.\n" : "Transmission failed.\n");
|
||||||
|
|
||||||
|
// Wait until TTN communication is idle and save state
|
||||||
|
ttn.waitForIdle();
|
||||||
|
ttn.prepareForDeepSleep();
|
||||||
|
|
||||||
|
// Schedule wake up
|
||||||
|
esp_sleep_enable_timer_wakeup(TX_INTERVAL * 1000000LL);
|
||||||
|
|
||||||
|
printf("Going to deep sleep...\n");
|
||||||
|
esp_deep_sleep_start();
|
||||||
}
|
}
|
@ -49,6 +49,14 @@ const char *appKey = "????????????????????????????????";
|
|||||||
static uint8_t msgData[] = "Hello, world";
|
static uint8_t msgData[] = "Hello, world";
|
||||||
|
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
{
|
{
|
||||||
esp_err_t err;
|
esp_err_t err;
|
||||||
@ -80,6 +88,9 @@ void app_main(void)
|
|||||||
// The below line can be commented after the first run as the data is saved in NVS
|
// The below line can be commented after the first run as the data is saved in NVS
|
||||||
ttn_provision(devEui, appEui, appKey);
|
ttn_provision(devEui, appEui, appKey);
|
||||||
|
|
||||||
|
// Register callback for received messages
|
||||||
|
ttn_on_message(messageReceived);
|
||||||
|
|
||||||
// ttn_set_adr_enabled(false);
|
// ttn_set_adr_enabled(false);
|
||||||
// ttn_set_data_rate(TTN_DR_US915_SF7);
|
// ttn_set_data_rate(TTN_DR_US915_SF7);
|
||||||
// ttn_set_max_tx_pow(14);
|
// ttn_set_max_tx_pow(14);
|
||||||
|
@ -391,7 +391,12 @@ TickType_t hal_esp32_get_timer_duration(void)
|
|||||||
return 1; // busy, not waiting
|
return 1; // busy, not waiting
|
||||||
|
|
||||||
if (alarm_time != 0)
|
if (alarm_time != 0)
|
||||||
return pdMS_TO_TICKS((alarm_time - get_current_time() + 999) / 1000);
|
{
|
||||||
|
TickType_t dur = pdMS_TO_TICKS((alarm_time - get_current_time() + 999) / 1000);
|
||||||
|
if (dur > pdMS_TO_TICKS(30000))
|
||||||
|
dur = pdMS_TO_TICKS(200);
|
||||||
|
return dur;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return 0; // waiting indefinitely
|
return 0; // waiting indefinitely
|
||||||
|
@ -337,6 +337,7 @@ bool ttn_is_provisioned(void)
|
|||||||
void ttn_prepare_for_deep_sleep(void)
|
void ttn_prepare_for_deep_sleep(void)
|
||||||
{
|
{
|
||||||
ttn_rtc_save();
|
ttn_rtc_save();
|
||||||
|
stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -357,8 +358,8 @@ TickType_t ttn_busy_duration(void)
|
|||||||
if (duration != 0)
|
if (duration != 0)
|
||||||
return duration; // busy or timer scheduled
|
return duration; // busy or timer scheduled
|
||||||
|
|
||||||
if (current_rx_tx_window != TTN_WINDOW_IDLE)
|
if ((LMIC.opmode & (OP_JOINING | OP_TXDATA | OP_POLL | OP_TXRXPEND)) != 0)
|
||||||
return pdMS_TO_TICKS(100); // within TX/RX window
|
return pdMS_TO_TICKS(100); // pending action
|
||||||
|
|
||||||
return 0; // idle
|
return 0; // idle
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user