main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "esp_wifi.h"
  7. #include "esp_event.h"
  8. #include "esp_log.h"
  9. #include "nvs_flash.h"
  10. #include "lwip/sockets.h"
  11. #define WIFI_SSID "TOCHTECH"
  12. #define WIFI_PASS "Smarturns2017"
  13. #define ASTERISK_IP "192.168.68.93"
  14. #define ASTERISK_PORT 5038 // Asterisk AMI port (safe TCP test)
  15. static const char *TAG = "PING_TEST";
  16. /* ---------------- WIFI ---------------- */
  17. static void wifi_event_handler(void *arg, esp_event_base_t event_base,
  18. int32_t event_id, void *event_data)
  19. {
  20. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  21. esp_wifi_connect();
  22. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  23. ESP_LOGI(TAG, "WiFi disconnected, retrying...");
  24. esp_wifi_connect();
  25. } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  26. ESP_LOGI(TAG, "WiFi connected!");
  27. }
  28. }
  29. void wifi_init(void)
  30. {
  31. nvs_flash_init();
  32. esp_netif_init();
  33. esp_event_loop_create_default();
  34. esp_netif_create_default_wifi_sta();
  35. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  36. esp_wifi_init(&cfg);
  37. esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL);
  38. esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL);
  39. wifi_config_t wifi_config = {
  40. .sta = {
  41. .ssid = WIFI_SSID,
  42. .password = WIFI_PASS,
  43. },
  44. };
  45. esp_wifi_set_mode(WIFI_MODE_STA);
  46. esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
  47. esp_wifi_start();
  48. }
  49. /* ---------------- TCP CHECK ---------------- */
  50. void check_asterisk(void)
  51. {
  52. while (1) {
  53. struct sockaddr_in dest;
  54. dest.sin_addr.s_addr = inet_addr(ASTERISK_IP);
  55. dest.sin_family = AF_INET;
  56. dest.sin_port = htons(ASTERISK_PORT);
  57. int sock = socket(AF_INET, SOCK_STREAM, 0);
  58. if (sock < 0) {
  59. ESP_LOGE(TAG, "Socket create failed");
  60. vTaskDelay(pdMS_TO_TICKS(2000));
  61. continue;
  62. }
  63. ESP_LOGI(TAG, "Checking Asterisk at %s:%d...", ASTERISK_IP, ASTERISK_PORT);
  64. int err = connect(sock, (struct sockaddr*)&dest, sizeof(dest));
  65. if (err == 0) {
  66. ESP_LOGI(TAG, "TCP connected → sending AMI login");
  67. const char *login =
  68. "Action: Login\r\n"
  69. "Username: esp32s3\r\n"
  70. "Secret: 1234\r\n"
  71. "Events: off\r\n"
  72. "\r\n";
  73. send(sock, login, strlen(login), 0);
  74. char buf[512];
  75. int len = recv(sock, buf, sizeof(buf) - 1, 0);
  76. if (len > 0) {
  77. buf[len] = 0;
  78. ESP_LOGI(TAG, "AMI RESPONSE:\n%s", buf);
  79. if (strstr(buf, "Success")) {
  80. ESP_LOGI(TAG, "✅ AMI LOGIN SUCCESS");
  81. } else {
  82. ESP_LOGW(TAG, "❌ AMI LOGIN FAILED");
  83. }
  84. } else {
  85. ESP_LOGW(TAG, "No AMI response");
  86. }
  87. } else {
  88. ESP_LOGW(TAG, "❌ Asterisk NOT reachable");
  89. }
  90. close(sock);
  91. vTaskDelay(pdMS_TO_TICKS(5000));
  92. }
  93. }
  94. /* ---------------- MAIN ---------------- */
  95. void app_main(void)
  96. {
  97. ESP_LOGI(TAG, "Starting minimal Asterisk connectivity test");
  98. wifi_init();
  99. vTaskDelay(pdMS_TO_TICKS(5000)); // wait for WiFi
  100. xTaskCreate(check_asterisk, "check_asterisk", 4096, NULL, 5, NULL);
  101. }