summaryrefslogtreecommitdiffstats
path: root/esp32/main/main.c
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2024-09-08 12:17:22 +0800
committerSadeep Madurange <sadeep@asciimx.com>2024-09-08 12:17:22 +0800
commit2dec577c5cc9604109852e396b5ca65f69b06994 (patch)
treed6ff0a037039a83f229f7328583281b7313ddd9b /esp32/main/main.c
parent623ea906def6b58be2f735db889abed5ea88199f (diff)
downloadesp32-inmp441-driver-2dec577c5cc9604109852e396b5ca65f69b06994.tar.gz
wip: udp with task.
Diffstat (limited to 'esp32/main/main.c')
-rw-r--r--esp32/main/main.c77
1 files changed, 67 insertions, 10 deletions
diff --git a/esp32/main/main.c b/esp32/main/main.c
index e0f3e05..758a71a 100644
--- a/esp32/main/main.c
+++ b/esp32/main/main.c
@@ -1,24 +1,34 @@
+#include <string.h>
+#include <sys/param.h>
+
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
-#include <esp_log.h>
#include <esp_event.h>
+#include <esp_log.h>
#include <esp_netif.h>
+#include <esp_system.h>
#include <nvs_flash.h>
#include <driver/gpio.h>
#include <driver/i2s_std.h>
+#include <lwip/err.h>
+#include <lwip/sockets.h>
+#include <lwip/sys.h>
+#include <lwip/netdb.h>
+
#include "wifi.h"
-#define BUFLEN 1024
-#define SAMP_RATE 8000
+#define BUFLEN 1024
+#define SAMPLE_RATE 8000
#define I2S_WS GPIO_NUM_4
#define I2S_SD GPIO_NUM_1
#define I2S_SCK GPIO_NUM_5
static i2s_chan_handle_t chan;
+static char *data = calloc(1, BUFLEN + 1);
static void i2s_read_task(void *args)
{
@@ -30,8 +40,8 @@ static void i2s_read_task(void *args)
for (;;) {
if (i2s_channel_read(chan, buf, BUFLEN, &n, 1000) == ESP_OK) {
if (n > 0) {
- // todo: copy to a separate buffer and send over udp
- // esp_mqtt_client_publish(client, "snd", buf, n, 1, 0);
+ memcpy(data, buf, n);
+ data[n] = '\0';
}
} else
printf("Read Task: i2s read failed\n");
@@ -44,16 +54,14 @@ static void i2s_read_task(void *args)
static inline void i2s_init(void)
{
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(
- I2S_NUM_AUTO,
- I2S_ROLE_MASTER);
+ I2S_NUM_AUTO, I2S_ROLE_MASTER);
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, NULL, &chan));
i2s_std_config_t std_cfg = {
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(SAMP_RATE),
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(
- I2S_DATA_BIT_WIDTH_24BIT,
- I2S_SLOT_MODE_MONO),
+ I2S_DATA_BIT_WIDTH_24BIT, I2S_SLOT_MODE_MONO),
.gpio_cfg = {
.ws = I2S_WS,
.din = I2S_SD,
@@ -73,15 +81,64 @@ static inline void i2s_init(void)
ESP_ERROR_CHECK(i2s_channel_init_std_mode(chan, &std_cfg));
}
+static void udp_send_task(void *args)
+{
+ const int family = AF_INET;
+ const int port = CONFIG_UDP_PORT;
+ const char[] ip_addr = CONFIG_UDP_ADDR;
+
+ int sock;
+ struct timeval timeout;
+ struct sockaddr_in addr;
+
+ addr_family = AF_INET;
+
+ dest_addr.sin_addr.s_addr = inet_addr(ip_addr);
+ dest_addr.sin_family = family;
+ dest_addr.sin_port = htons(port);
+
+ int sock = socket(family, SOCK_DGRAM, IPPROTO_IP);
+
+ if (sock < 0) {
+ ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
+ break;
+ }
+
+ timeout.tv_sec = 10;
+ timeout.tv_usec = 0;
+ setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout);
+
+ ESP_LOGI(TAG, "Socket created, sending to %s:%d", ip_addr, port);
+
+ for (;;) {
+ int err = sendto(sock, data, strlen(data), 0,
+ (struct sockaddr *)&addr, sizeof addr);
+
+ if (err < 0) {
+ ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
+ break;
+ }
+
+ if (sock != -1) {
+ ESP_LOGE(TAG, "Shutting down socket and restarting...");
+ shutdown(sock, 0);
+ close(sock);
+ }
+ }
+
+ vTaskDelete(NULL);
+}
+
void app_main(void)
{
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_connect();
-
i2s_init();
+
xTaskCreate(i2s_read_task, "i2s_read_task", 4096, NULL, 5, NULL);
+ xTaskCreate(udp_send_task, "udp_send_task", 4096, NULL, 5, NULL);
for (;;)
vTaskDelay(500 / portTICK_PERIOD_MS);