-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reading soil moisture + deep sleep
- Loading branch information
Marcel Hofmann
committed
Jul 14, 2024
1 parent
e739ef4
commit 2878cb4
Showing
15 changed files
with
255 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
idf_component_register(SRCS "main.c" "nvs_helper.c" "wifi_helper.c" "ota.c" | ||
idf_component_register(SRCS "main.c" "nvs_helper.c" "wifi_helper.c" "ota.c" "logging.c" "read_measurements.c" "send_data.c" | ||
INCLUDE_DIRS "." "include" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef LOGGING_H | ||
#define LOGGING_H | ||
|
||
#include <stdio.h> | ||
|
||
void udp_socket_init(); | ||
int custom_log_handler(const char *format, va_list args); | ||
|
||
#endif // LOGGING_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef MAIN_H | ||
#define MAIN_H | ||
|
||
void enter_deep_sleep(); | ||
|
||
#endif // MAIN_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef READ_MEASUREMENTS_H | ||
#define READ_MEASUREMENTS_H | ||
|
||
void read_measurements_task(void *pvParameters); | ||
|
||
#endif // READ_MEASUREMENTS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef SEND_DATA_H | ||
#define SEND_DATA_H | ||
|
||
void send_data_task(void *pvParameters); | ||
|
||
#endif // SEND_DATA_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include "esp_log.h" | ||
#include "lwip/inet.h" | ||
#include "lwip/sockets.h" | ||
#include <stdio.h> | ||
#include <sys/_timeval.h> | ||
|
||
char *TAG = "LOGGING"; | ||
|
||
static struct sockaddr_in server_addr; | ||
int udp_socket; | ||
|
||
void udp_socket_init() { | ||
udp_socket = socket(AF_INET, SOCK_DGRAM, 0); | ||
if (udp_socket <= 0) { | ||
ESP_LOGE(TAG, "Unable to create socket: errno %d", errno); | ||
} | ||
|
||
memset(&server_addr, 0, sizeof(server_addr)); | ||
server_addr.sin_family = AF_INET; | ||
server_addr.sin_port = htons(CONFIG_HOME_SERVER_UDP_PORT); | ||
inet_pton(AF_INET, CONFIG_HOME_SERVER_IP, &server_addr.sin_addr.s_addr); | ||
|
||
ESP_LOGI(TAG, "UDP socket initialized."); | ||
} | ||
|
||
const char *log_level_to_str(esp_log_level_t level) { | ||
switch (level) { | ||
case ESP_LOG_ERROR: | ||
return "ERROR"; | ||
case ESP_LOG_WARN: | ||
return "WARN"; | ||
case ESP_LOG_INFO: | ||
return "INFO"; | ||
case ESP_LOG_DEBUG: | ||
return "DEBUG"; | ||
case ESP_LOG_VERBOSE: | ||
return "VERBOSE"; | ||
default: | ||
return ""; | ||
} | ||
} | ||
|
||
static void udp_log_handler(const char *message) { | ||
sendto(udp_socket, message, strlen(message), 0, | ||
(struct sockaddr *)&server_addr, sizeof(server_addr)); | ||
} | ||
|
||
int custom_log_handler(const char *format, va_list args) { | ||
vprintf(format, args); | ||
|
||
char log_buffer[256]; | ||
int len = vsnprintf(log_buffer, sizeof(log_buffer), format, args); | ||
|
||
if (len > 0) { | ||
char message[300]; | ||
snprintf(message, sizeof(message), "2;%s", log_buffer); | ||
udp_log_handler(message); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include "esp_adc/adc_oneshot.h" | ||
#include "esp_log.h" | ||
#include "freertos/idf_additions.h" | ||
#include "freertos/projdefs.h" | ||
static const char *R_TAG = "READ_MEASUREMENTS"; | ||
|
||
static int adc_raw; | ||
|
||
extern QueueHandle_t measurements_queue; | ||
extern TaskHandle_t send_data_task_handle; | ||
|
||
void read_measurements_task(void *pvParameters) { | ||
adc_oneshot_unit_handle_t adc1_handle; | ||
adc_oneshot_unit_init_cfg_t init_config = {.unit_id = ADC_UNIT_1}; | ||
adc_oneshot_chan_cfg_t config = {.bitwidth = ADC_BITWIDTH_DEFAULT, | ||
.atten = ADC_ATTEN_DB_12}; | ||
|
||
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &adc1_handle)); | ||
|
||
ESP_ERROR_CHECK( | ||
adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_2, &config)); | ||
|
||
float moisture_percent; | ||
float sum = 0.0; | ||
|
||
for (int i = 0; i < 5; i++) { | ||
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, ADC_CHANNEL_2, &adc_raw)); | ||
ESP_LOGI(R_TAG, "Moisture raw data: %d", adc_raw); | ||
|
||
moisture_percent = 100.0 - ((float)adc_raw / 4095.0 * 100.0); | ||
ESP_LOGI(R_TAG, "Moisture percentage %.2f", moisture_percent); | ||
|
||
sum = sum + moisture_percent; | ||
vTaskDelay(pdMS_TO_TICKS(500)); | ||
} | ||
|
||
float avg = sum / 5; | ||
ESP_LOGI(R_TAG, "Moisture average %.2f", avg); | ||
|
||
ESP_ERROR_CHECK(adc_oneshot_del_unit(adc1_handle)); | ||
|
||
xQueueSend(measurements_queue, &avg, 0); | ||
vTaskDelete(NULL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include "send_data.h" | ||
#include "FreeRTOSConfig.h" | ||
#include "esp_log.h" | ||
#include "freertos/idf_additions.h" | ||
#include "freertos/projdefs.h" | ||
#include "include/main.h" | ||
#include "portmacro.h" | ||
#include <sys/time.h> | ||
|
||
extern QueueHandle_t measurements_queue; | ||
|
||
static const char *S_TAG = "SEND_DATA"; | ||
|
||
void send_data_task(void *pvParameters) { | ||
float measurement; | ||
|
||
if (xQueueReceive(measurements_queue, &measurement, portMAX_DELAY) == | ||
pdTRUE) { | ||
ESP_LOGI(S_TAG, "Received measurement %0.2f", measurement); | ||
} | ||
|
||
enter_deep_sleep(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.