udp_client.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_wifi.h"
  14. #include "esp_event.h"
  15. #include "esp_log.h"
  16. #include "nvs_flash.h"
  17. #include "esp_netif.h"
  18. #include "protocol_examples_common.h"
  19. #include "lwip/err.h"
  20. #include "lwip/sockets.h"
  21. #include "lwip/sys.h"
  22. #include <lwip/netdb.h>
  23. #include "addr_from_stdin.h"
  24. #if defined(CONFIG_EXAMPLE_IPV4)
  25. #define HOST_IP_ADDR CONFIG_EXAMPLE_IPV4_ADDR
  26. #elif defined(CONFIG_EXAMPLE_IPV6)
  27. #define HOST_IP_ADDR CONFIG_EXAMPLE_IPV6_ADDR
  28. #else
  29. #define HOST_IP_ADDR ""
  30. #endif
  31. #define PORT CONFIG_EXAMPLE_PORT
  32. static const char *TAG = "example";
  33. static const char *payload = "Message from ESP32 ";
  34. static void udp_client_task(void *pvParameters)
  35. {
  36. char rx_buffer[128];
  37. char host_ip[] = HOST_IP_ADDR;
  38. int addr_family = 0;
  39. int ip_protocol = 0;
  40. while (1) {
  41. #if defined(CONFIG_EXAMPLE_IPV4)
  42. struct sockaddr_in dest_addr;
  43. dest_addr.sin_addr.s_addr = inet_addr(HOST_IP_ADDR);
  44. dest_addr.sin_family = AF_INET;
  45. dest_addr.sin_port = htons(PORT);
  46. addr_family = AF_INET;
  47. ip_protocol = IPPROTO_IP;
  48. #elif defined(CONFIG_EXAMPLE_IPV6)
  49. struct sockaddr_in6 dest_addr = { 0 };
  50. inet6_aton(HOST_IP_ADDR, &dest_addr.sin6_addr);
  51. dest_addr.sin6_family = AF_INET6;
  52. dest_addr.sin6_port = htons(PORT);
  53. dest_addr.sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE);
  54. addr_family = AF_INET6;
  55. ip_protocol = IPPROTO_IPV6;
  56. #elif defined(CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN)
  57. struct sockaddr_in6 dest_addr = { 0 };
  58. ESP_ERROR_CHECK(get_addr_from_stdin(PORT, SOCK_DGRAM, &ip_protocol, &addr_family, &dest_addr));
  59. #endif
  60. int sock = socket(addr_family, SOCK_DGRAM, ip_protocol);
  61. if (sock < 0) {
  62. ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
  63. break;
  64. }
  65. ESP_LOGI(TAG, "Socket created, sending to %s:%d", HOST_IP_ADDR, PORT);
  66. while (1) {
  67. int err = sendto(sock, payload, strlen(payload), 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
  68. if (err < 0) {
  69. ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
  70. break;
  71. }
  72. ESP_LOGI(TAG, "Message sent");
  73. struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6
  74. socklen_t socklen = sizeof(source_addr);
  75. int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&source_addr, &socklen);
  76. // Error occurred during receiving
  77. if (len < 0) {
  78. ESP_LOGE(TAG, "recvfrom failed: errno %d", errno);
  79. break;
  80. }
  81. // Data received
  82. else {
  83. rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
  84. ESP_LOGI(TAG, "Received %d bytes from %s:", len, host_ip);
  85. ESP_LOGI(TAG, "%s", rx_buffer);
  86. if (strncmp(rx_buffer, "OK: ", 4) == 0) {
  87. ESP_LOGI(TAG, "Received expected message, reconnecting");
  88. break;
  89. }
  90. }
  91. vTaskDelay(2000 / portTICK_PERIOD_MS);
  92. }
  93. if (sock != -1) {
  94. ESP_LOGE(TAG, "Shutting down socket and restarting...");
  95. shutdown(sock, 0);
  96. close(sock);
  97. }
  98. }
  99. vTaskDelete(NULL);
  100. }
  101. void app_main(void)
  102. {
  103. ESP_ERROR_CHECK(nvs_flash_init());
  104. ESP_ERROR_CHECK(esp_netif_init());
  105. ESP_ERROR_CHECK(esp_event_loop_create_default());
  106. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  107. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  108. * examples/protocols/README.md for more information about this function.
  109. */
  110. ESP_ERROR_CHECK(example_connect());
  111. xTaskCreate(udp_client_task, "udp_client", 4096, NULL, 5, NULL);
  112. }