mirror of
				https://github.com/manuelbl/ttn-esp32.git
				synced 2025-10-31 10:40:35 +01:00 
			
		
		
		
	Save and restore for deep sleep
This commit is contained in:
		
							
								
								
									
										10
									
								
								examples/deep_sleep_in_c/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								examples/deep_sleep_in_c/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,10 @@ | ||||
| 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 "../..") | ||||
|  | ||||
| #add_definitions(-DLMIC_ENABLE_event_logging=1) | ||||
|  | ||||
| project(deep_sleep) | ||||
							
								
								
									
										4
									
								
								examples/deep_sleep_in_c/main/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								examples/deep_sleep_in_c/main/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,4 @@ | ||||
| idf_component_register( | ||||
|     SRCS "main.c" | ||||
|     INCLUDE_DIRS "." | ||||
|     REQUIRES ttn-esp32) | ||||
							
								
								
									
										117
									
								
								examples/deep_sleep_in_c/main/main.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										117
									
								
								examples/deep_sleep_in_c/main/main.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,117 @@ | ||||
| /******************************************************************************* | ||||
|  *  | ||||
|  * ttn-esp32 - The Things Network device library for ESP-IDF / SX127x | ||||
|  *  | ||||
|  * Copyright (c) 2021 Manuel Bleichenbacher | ||||
|  *  | ||||
|  * Licensed under MIT License | ||||
|  * https://opensource.org/licenses/MIT | ||||
|  * | ||||
|  * Sample program (in C) sending messages and going to deep sleep in-between. | ||||
|  *******************************************************************************/ | ||||
|  | ||||
| #include "freertos/FreeRTOS.h" | ||||
| #include "esp_event.h" | ||||
| #include "driver/gpio.h" | ||||
| #include "nvs_flash.h" | ||||
| #include "esp_log.h" | ||||
| #include "esp_sleep.h" | ||||
|  | ||||
| #include "ttn.h" | ||||
|  | ||||
| // 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. | ||||
|  | ||||
| // Copy the below hex strings from the TTN console (Applications > Your application > End devices | ||||
| // > Your device > Activation information) | ||||
|  | ||||
| // AppEUI (sometimes called JoinEUI) | ||||
| const char *appEui = "????????????????"; | ||||
| // DevEUI | ||||
| const char *devEui = "????????????????"; | ||||
| // AppKey | ||||
| 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 | ||||
|  | ||||
| #define TX_INTERVAL 60 // in sec | ||||
| static uint8_t msgData[] = "Hello, world"; | ||||
|  | ||||
|  | ||||
| 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 = { | ||||
|         .miso_io_num = TTN_PIN_SPI_MISO, | ||||
|         .mosi_io_num = TTN_PIN_SPI_MOSI, | ||||
|         .sclk_io_num = TTN_PIN_SPI_SCLK, | ||||
|         .quadwp_io_num = -1, | ||||
|         .quadhd_io_num = -1 | ||||
|     };  | ||||
|     err = spi_bus_initialize(TTN_SPI_HOST, &spi_bus_config, TTN_SPI_DMA_CHAN); | ||||
|     ESP_ERROR_CHECK(err); | ||||
|  | ||||
|     // Initialize TTN | ||||
|     ttn_init(); | ||||
|  | ||||
|     // Configure the SX127x pins | ||||
|     ttn_configure_pins(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_set_adr_enabled(false); | ||||
|     // ttn_set_data_rate(TTN_DR_US915_SF7); | ||||
|     // ttn_set_max_tx_pow(14); | ||||
|  | ||||
|     if (ttn_resume_after_deep_sleep()) { | ||||
|         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"); | ||||
|     ttn_response_code_t res = ttn_transmit_message(msgData, sizeof(msgData) - 1, 1, false); | ||||
|     printf(res == TTN_SUCCESSFUL_TRANSMISSION ? "Message sent.\n" : "Transmission failed.\n"); | ||||
|  | ||||
|     // Wait until TTN communication is idle and save state | ||||
|     ttn_wait_for_idle(); | ||||
|     ttn_prepare_for_deep_sleep(); | ||||
|  | ||||
|     // Schedule wake up | ||||
|     esp_sleep_enable_timer_wakeup(TX_INTERVAL * 1000000LL); | ||||
|  | ||||
|     printf("Going to deep sleep...\n"); | ||||
|     esp_deep_sleep_start(); | ||||
| } | ||||
		Reference in New Issue
	
	Block a user