diff --git a/esp32-app/lib/LORA/platform/stm32cube/hal/lora-cube-hal.c b/esp32-app/lib/LORA/platform/stm32cube/hal/lora-cube-hal.c index ec50b17..d8b0872 100644 --- a/esp32-app/lib/LORA/platform/stm32cube/hal/lora-cube-hal.c +++ b/esp32-app/lib/LORA/platform/stm32cube/hal/lora-cube-hal.c @@ -19,8 +19,7 @@ bool lora_cube_hal_init(lora *const dev, lora_cube_hal *const cube_dev) //----------------------------------------------------------------------------- void lora_cube_hal_deinit(lora *const dev) { - // IO - lora_io_deinit(dev); + lora_end(dev); } //----------------------------------------------------------------------------- diff --git a/esp32-app/platformio.ini b/esp32-app/platformio.ini index 35c5b8f..e9a151e 100644 --- a/esp32-app/platformio.ini +++ b/esp32-app/platformio.ini @@ -22,7 +22,7 @@ build_flags = ;-DCONFIG_INT_WDT=0 ; Upload port -upload_port = /dev/ttyUSB1 +upload_port = /dev/ttyUSB0 ;upload_port = COM38 ; Extra scripts diff --git a/esp32-app/src/App/app_config.h b/esp32-app/src/App/app_config.h index 50ca10e..996f59f 100644 --- a/esp32-app/src/App/app_config.h +++ b/esp32-app/src/App/app_config.h @@ -12,13 +12,9 @@ #include "Weather/weather_settings.h" #include "Radio/radio_settings.h" #include "Display/display_settings.h" -#include "app_settings.h" #include "rtos_common.h" - - - -// Define the version number, also used for webserver as Last-Modified header and to check version for update. -#define APP_VERSION "0.0.1" +#include "app_settings.h" +#include "app_vars.h" // LedClock App can be updated (OTA) to the latest version from a remote server. #define APP_UPDATEHOST "github.com" // Host for software updates diff --git a/esp32-app/src/App/app_vars.h b/esp32-app/src/App/app_vars.h index 9a03a7c..2656731 100644 --- a/esp32-app/src/App/app_vars.h +++ b/esp32-app/src/App/app_vars.h @@ -7,6 +7,6 @@ */ // Define the app version number -#define APP_VERSION "0.0.1" +#define APP_VERSION "0.1.0" #define APP_DATE_LAST_MODIFIED "Tue, 24 Nov 2020 01:44:45 GMT" \ No newline at end of file diff --git a/esp32-app/src/Radio/radio.h b/esp32-app/src/Radio/radio.h index b58b65a..8ddb7e3 100644 --- a/esp32-app/src/Radio/radio.h +++ b/esp32-app/src/Radio/radio.h @@ -2,7 +2,6 @@ #define __RADIO_H__ #include "radio_settings.h" -//#include "../SX1278/platform/arduino/sx1278-arduino.h" #include "../LORA/platform/arduino/lora-arduino.h" /** diff --git a/esp32-app/src/Radio/radio_sensor_task.cpp b/esp32-app/src/Radio/radio_sensor_task.cpp index 295be42..2e6b451 100644 --- a/esp32-app/src/Radio/radio_sensor_task.cpp +++ b/esp32-app/src/Radio/radio_sensor_task.cpp @@ -100,8 +100,8 @@ bool RadioSensorTask::pushRadioSensorMsg(const RadioSensorData& data) String msg(tr(M_SENSOR_NAME) + col + spc + \ tr(M_SENSOR_TEMP) + col + String(data.temperature) + tr(M_COMMON_DEG_CELS) + com + spc + \ tr(M_SENSOR_PRESS) + col + String(uint32_t(data.pressure/100)) + tr(M_COMMON_PRESSURE_HPA) + com + spc + \ - tr(M_SENSOR_HUMID) + col + String(uint8_t(data.humidity)) + tr(M_COMMON_PERCENT) + com + spc + \ - tr(M_SENSOR_VBATT) + col + String(float(data.vbatt/1000.f)) + tr(M_COMMON_VOLTAGE)); + tr(M_SENSOR_HUMID) + col + String(uint8_t(data.humidity)) + tr(M_COMMON_PERCENT));// + com + spc + \ + //tr(M_SENSOR_VBATT) + col + String(float(data.vbatt/1000.f)) + tr(M_COMMON_VOLTAGE)); return AppSh.putDisplayMsg(msg.c_str(), msg.length()); } diff --git a/sensor-app/Core/Src/main.c b/sensor-app/Core/Src/main.c index 3e7a7f0..4b380af 100644 --- a/sensor-app/Core/Src/main.c +++ b/sensor-app/Core/Src/main.c @@ -100,6 +100,10 @@ uint8_t PWR_PVDGetLevel(void) // Last measured values static float temperature, pressure, humidity; +// Last measured values +const uint8_t radio_health_check_state = 4; +static uint8_t radio_health_check_state_cnt = 0; + // Msg frame static radio_msg_sensor_frame msgf = { @@ -149,9 +153,12 @@ int main(void) /* USER CODE BEGIN 2 */ dbg("LUK6"); + // Read current config from eprom app_settings_init(); - radio_init(); + // Enable sensor sensor_init(); + // Enable radio + radio_init(); /* USER CODE END 2 */ /* Infinite loop */ @@ -170,6 +177,9 @@ int main(void) { sprintf(dbg_buf, "M_LWP_EXIT:%d", HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR2)); dbg(dbg_buf); + // Enable sensor pin + HAL_GPIO_WritePin(SENSOR_VDD_GPIO_Port, SENSOR_VDD_Pin, GPIO_PIN_SET); + HAL_Delay(100); HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR2, 0); } @@ -189,7 +199,6 @@ int main(void) msgf.temperature = temperature; msgf.pressure = pressure; msgf.humidity = humidity; - //PWR_PVDCmd(ENABLE); msgf.vbatt = 3300;//adc_read_vbatt(); /* Send result data */ @@ -197,14 +206,31 @@ int main(void) /* Refresh watchdog, no more than 2[s] must expire before next refresh */ HAL_WWDG_Refresh(&hwwdg); - uint8_t rxMsgWaitCntSecs = 10; // 5 seconds - while (rxMsgWaitCntSecs--) + uint8_t rx_msg_wait_cnt_secs = 5; // 5 seconds + while (rx_msg_wait_cnt_secs--) { /* Wait 1 second for incoming data from ledclock */ HAL_Delay(1000); /* Refresh watchdog, no more than 2[s] must expire before next refresh */ HAL_WWDG_Refresh(&hwwdg); } + + // Check if any data from ledclock came + if (!radio_was_data_received()) + { + radio_health_check_state_cnt++; + } + else + { + radio_health_check_state_cnt = 0; + radio_data_received_clear(); + } + + // If any problem with radio exists, restrat it + if (radio_health_check_state_cnt >= radio_health_check_state) + { + radio_restart(); + } } /* USER CODE END 3 */ } @@ -620,7 +646,6 @@ static void enter_low_power_mode() /* Put radio into sleep mode */ radio_sleep(); - //HAL_GPIO_WritePin(SX1278_RESET_GPIO_Port, SX1278_RESET_Pin, GPIO_PIN_SET); /* Disable sensor */ HAL_GPIO_WritePin(SENSOR_VDD_GPIO_Port, SENSOR_VDD_Pin, GPIO_PIN_RESET); @@ -669,8 +694,6 @@ static void enter_low_power_mode() /* Enable SysTick. */ //HAL_ResumeTick(); //HAL_GPIO_WritePin(SX1278_RESET_GPIO_Port, SX1278_RESET_Pin, GPIO_PIN_RESET); - HAL_GPIO_WritePin(SENSOR_VDD_GPIO_Port, SENSOR_VDD_Pin, GPIO_PIN_SET); - HAL_Delay(100); dbg("M_LPM_RUN"); } diff --git a/sensor-app/Core/Src/radio.c b/sensor-app/Core/Src/radio.c index f870ffb..0af91fe 100644 --- a/sensor-app/Core/Src/radio.c +++ b/sensor-app/Core/Src/radio.c @@ -39,6 +39,8 @@ static lora_cube_hal cube_hal_dev = static lora dev; +static volatile uint8_t radio_data_received = false; + //----------------------------------------------------------------------------- void radio_init() { @@ -53,8 +55,8 @@ void radio_init() lora_delay_ms(1000); } lora_on_receive(&dev, on_rx_done); - // Switch into idle mode - lora_idle(&dev); + // Switch sleep idle mode + lora_sleep(&dev); } //----------------------------------------------------------------------------- @@ -88,6 +90,41 @@ void radio_sleep() lora_sleep(&dev); } +//----------------------------------------------------------------------------- +void radio_restart() +{ + // Deinitilaize radio + lora_cube_hal_deinit(&dev); + lora_delay_ms(100); + + // Set dev + dev.frequency = 433E6; + dev.on_receive = NULL; + dev.on_tx_done = NULL; + + while (!lora_cube_hal_init(&dev, &cube_hal_dev)) + { + dbg("R_RDN"); + lora_delay_ms(1000); + } + + lora_on_receive(&dev, on_rx_done); + // Switch into sleep mode + lora_sleep(&dev); +} + +//----------------------------------------------------------------------------- +uint8_t radio_was_data_received() +{ + return radio_data_received; +} + +//----------------------------------------------------------------------------- +void radio_data_received_clear() +{ + radio_data_received = false; +} + //----------------------------------------------------------------------------- uint16_t radio_msg_frame_checksum(const uint8_t *data, const uint8_t data_len) { @@ -128,10 +165,10 @@ static void on_rx_done(void *ctx, int packet_size) } // Read packet header bytes: - hdr.receiver_id = lora_read(dev); // recipient address - hdr.sender_id = lora_read(dev); // sender address - hdr.msg_id = lora_read(dev); // incoming msg ID - hdr.payload_len = lora_read(dev); // incoming msg length + hdr.receiver_id = lora_read(dev); // recipient address + hdr.sender_id = lora_read(dev); // sender address + hdr.msg_id = lora_read(dev); // incoming msg ID + hdr.payload_len = lora_read(dev); // incoming msg length // If the recipient isn't this device or broadcast, if (hdr.receiver_id != LORA_RADIO_SENSOR_ID && hdr.receiver_id != 0xFF) @@ -152,7 +189,8 @@ static void on_rx_done(void *ctx, int packet_size) } parse_incoming_msg_clock(p, hdr.payload_len); - + // Set global flag + radio_data_received = true; err: radio_sleep(); } diff --git a/sensor-app/Core/Src/radio.h b/sensor-app/Core/Src/radio.h index 157ec9f..730c994 100644 --- a/sensor-app/Core/Src/radio.h +++ b/sensor-app/Core/Src/radio.h @@ -66,4 +66,10 @@ void radio_send(radio_msg_sensor_frame *msgf); void radio_sleep(); +void radio_restart(); + +uint8_t radio_was_data_received(); + +void radio_data_received_clear(); + #endif /* RADIO_H_ */