udp_client.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* BSD Socket API Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <string.h>
  8. #include <sys/param.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "freertos/event_groups.h"
  12. #include "esp_system.h"
  13. #include "esp_event.h"
  14. #include "esp_log.h"
  15. #include "nvs_flash.h"
  16. #include "esp_netif.h"
  17. #include "protocol_examples_common.h"
  18. #include "lwip/err.h"
  19. #include "lwip/sockets.h"
  20. #include "lwip/sys.h"
  21. #include <lwip/netdb.h>
  22. #ifdef CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN
  23. #include "addr_from_stdin.h"
  24. #endif
  25. #if defined(CONFIG_EXAMPLE_IPV4)
  26. #define HOST_IP_ADDR CONFIG_EXAMPLE_IPV4_ADDR
  27. #elif defined(CONFIG_EXAMPLE_IPV6)
  28. #define HOST_IP_ADDR CONFIG_EXAMPLE_IPV6_ADDR
  29. #else
  30. #define HOST_IP_ADDR ""
  31. #endif
  32. #define PORT CONFIG_EXAMPLE_PORT
  33. static const char *TAG = "example";
  34. static const char *payload = "Message from ESP32 ";
  35. static void udp_client_task(void *pvParameters)
  36. {
  37. char rx_buffer[128];
  38. char host_ip[] = HOST_IP_ADDR;
  39. int addr_family = 0;
  40. int ip_protocol = 0;
  41. while (1) {
  42. #if defined(CONFIG_EXAMPLE_IPV4)
  43. struct sockaddr_in dest_addr;
  44. dest_addr.sin_addr.s_addr = inet_addr(HOST_IP_ADDR);
  45. dest_addr.sin_family = AF_INET;
  46. dest_addr.sin_port = htons(PORT);
  47. addr_family = AF_INET;
  48. ip_protocol = IPPROTO_IP;
  49. #elif defined(CONFIG_EXAMPLE_IPV6)
  50. struct sockaddr_in6 dest_addr = { 0 };
  51. inet6_aton(HOST_IP_ADDR, &dest_addr.sin6_addr);
  52. dest_addr.sin6_family = AF_INET6;
  53. dest_addr.sin6_port = htons(PORT);
  54. dest_addr.sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE);
  55. addr_family = AF_INET6;
  56. ip_protocol = IPPROTO_IPV6;
  57. #elif defined(CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN)
  58. struct sockaddr_storage dest_addr = { 0 };
  59. ESP_ERROR_CHECK(get_addr_from_stdin(PORT, SOCK_DGRAM, &ip_protocol, &addr_family, &dest_addr));
  60. #endif
  61. int sock = socket(addr_family, SOCK_DGRAM, ip_protocol);
  62. if (sock < 0) {
  63. ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
  64. break;
  65. }
  66. // Set timeout
  67. struct timeval timeout;
  68. timeout.tv_sec = 10;
  69. timeout.tv_usec = 0;
  70. setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout);
  71. ESP_LOGI(TAG, "Socket created, sending to %s:%d", HOST_IP_ADDR, PORT);
  72. while (1) {
  73. int err = sendto(sock, payload, strlen(payload), 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
  74. if (err < 0) {
  75. ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
  76. break;
  77. }
  78. ESP_LOGI(TAG, "Message sent");
  79. struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6
  80. socklen_t socklen = sizeof(source_addr);
  81. int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&source_addr, &socklen);
  82. // Error occurred during receiving
  83. if (len < 0) {
  84. ESP_LOGE(TAG, "recvfrom failed: errno %d", errno);
  85. break;
  86. }
  87. // Data received
  88. else {
  89. rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
  90. ESP_LOGI(TAG, "Received %d bytes from %s:", len, host_ip);
  91. ESP_LOGI(TAG, "%s", rx_buffer);
  92. if (strncmp(rx_buffer, "OK: ", 4) == 0) {
  93. ESP_LOGI(TAG, "Received expected message, reconnecting");
  94. break;
  95. }
  96. }
  97. vTaskDelay(2000 / portTICK_PERIOD_MS);
  98. }
  99. if (sock != -1) {
  100. ESP_LOGE(TAG, "Shutting down socket and restarting...");
  101. shutdown(sock, 0);
  102. close(sock);
  103. }
  104. }
  105. vTaskDelete(NULL);
  106. }
  107. void app_main(void)
  108. {
  109. ESP_ERROR_CHECK(nvs_flash_init());
  110. ESP_ERROR_CHECK(esp_netif_init());
  111. ESP_ERROR_CHECK(esp_event_loop_create_default());
  112. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  113. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  114. * examples/protocols/README.md for more information about this function.
  115. */
  116. ESP_ERROR_CHECK(example_connect());
  117. xTaskCreate(udp_client_task, "udp_client", 4096, NULL, 5, NULL);
  118. }