summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--esp32/main/main.c74
-rw-r--r--recv.py7
2 files changed, 37 insertions, 44 deletions
diff --git a/esp32/main/main.c b/esp32/main/main.c
index d18b137..344a995 100644
--- a/esp32/main/main.c
+++ b/esp32/main/main.c
@@ -27,10 +27,6 @@
const char *TAG = "mimir";
-static int sock;
-static size_t sock_addr_len;
-static struct sockaddr *sock_addr;
-
static i2s_chan_handle_t chan;
static void i2s_read_task(void *args)
@@ -39,14 +35,43 @@ static void i2s_read_task(void *args)
size_t n;
char *buf = calloc(1, BUFLEN);
+ char *ip_addr;
+ int family, port;
+ struct timeval timeout;
+ struct sockaddr_in dest_addr;
+
+ family = AF_INET;
+ port = CONFIG_UDP_PORT;
+ ip_addr = CONFIG_UDP_ADDR;
+
+ int sock = socket(family, SOCK_DGRAM, IPPROTO_IP);
+
+ if (sock < 0) {
+ ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
+ return;
+ }
+
+ timeout.tv_sec = 10;
+ timeout.tv_usec = 0;
+ setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout);
+
+ dest_addr.sin_addr.s_addr = inet_addr(ip_addr);
+ dest_addr.sin_family = family;
+ dest_addr.sin_port = htons(port);
+
+ ESP_LOGI(TAG, "Socket created, sending to %s:%d", ip_addr, port);
+
ESP_ERROR_CHECK(i2s_channel_enable(chan));
for (;;) {
if (i2s_channel_read(chan, buf, BUFLEN, &n, 1000) == ESP_OK) {
if (n > 0) {
- rc = sendto(sock, buf, n, 0, sock_addr, sock_addr_len);
+ rc = sendto(sock, buf, n, 0, (struct sockaddr *) &dest_addr,
+ sizeof dest_addr);
if (rc < 0)
ESP_LOGE(TAG, "sendto() failed: %s", strerror(errno));
+
+ vTaskDelay(1 / portTICK_PERIOD_MS);
}
} else
printf("Read Task: i2s read failed\n");
@@ -86,52 +111,13 @@ static inline void i2s_init(void)
ESP_ERROR_CHECK(i2s_channel_init_std_mode(chan, &std_cfg));
}
-static void udp_client_init(void)
-{
- char *ip_addr;
- int family, port;
- struct timeval timeout;
- struct sockaddr_in *dest_addr;
-
- family = AF_INET;
- port = CONFIG_UDP_PORT;
- ip_addr = CONFIG_UDP_ADDR;
-
- sock = socket(family, SOCK_DGRAM, IPPROTO_IP);
-
- if (sock < 0) {
- ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
- return;
- }
-
- timeout.tv_sec = 10;
- timeout.tv_usec = 0;
- setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout);
-
- if (!(dest_addr = malloc(sizeof(dest_addr)))) {
- ESP_LOGE(TAG, "malloc() failed for destination address");
- return;
- }
-
- dest_addr->sin_addr.s_addr = inet_addr(ip_addr);
- dest_addr->sin_family = family;
- dest_addr->sin_port = htons(port);
-
- sock_addr = (struct sockaddr *) dest_addr;
- sock_addr_len = sizeof dest_addr;
-
- ESP_LOGI(TAG, "Socket created, sending to %s:%d", ip_addr, port);
-}
-
void app_main(void)
{
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_connect();
-
i2s_init();
- udp_client_init();
xTaskCreate(i2s_read_task, "i2s_read_task", 4096, NULL, 5, NULL);
diff --git a/recv.py b/recv.py
index 66ad203..81ce287 100644
--- a/recv.py
+++ b/recv.py
@@ -10,11 +10,18 @@ wav.setnchannels(1)
wav.setframerate(8000)
wav.setsampwidth(4)
+is_first = True
+
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('', 3333))
while datetime.datetime.now() < read_until:
+ if is_first:
+ read_until = datetime.datetime.now() \
+ + datetime.timedelta(seconds = 30)
+ is_first = False
+
msg, _ = sock.recvfrom(1024)
wav.writeframes(msg)
print("writing data")