| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "esp_wifi.h"
- #include "esp_event.h"
- #include "esp_log.h"
- #include "nvs_flash.h"
- #include "lwip/sockets.h"
- #define WIFI_SSID "TOCHTECH"
- #define WIFI_PASS "Smarturns2017"
- #define ASTERISK_IP "192.168.68.93"
- #define ASTERISK_PORT 5038 // Asterisk AMI port (safe TCP test)
- static const char *TAG = "PING_TEST";
- /* ---------------- WIFI ---------------- */
- static void wifi_event_handler(void *arg, esp_event_base_t event_base,
- int32_t event_id, void *event_data)
- {
- if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
- esp_wifi_connect();
- } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
- ESP_LOGI(TAG, "WiFi disconnected, retrying...");
- esp_wifi_connect();
- } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
- ESP_LOGI(TAG, "WiFi connected!");
- }
- }
- void wifi_init(void)
- {
- nvs_flash_init();
- esp_netif_init();
- esp_event_loop_create_default();
- esp_netif_create_default_wifi_sta();
- wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
- esp_wifi_init(&cfg);
- esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL);
- esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL);
- wifi_config_t wifi_config = {
- .sta = {
- .ssid = WIFI_SSID,
- .password = WIFI_PASS,
- },
- };
- esp_wifi_set_mode(WIFI_MODE_STA);
- esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
- esp_wifi_start();
- }
- /* ---------------- TCP CHECK ---------------- */
- void check_asterisk(void)
- {
- while (1) {
- struct sockaddr_in dest;
- dest.sin_addr.s_addr = inet_addr(ASTERISK_IP);
- dest.sin_family = AF_INET;
- dest.sin_port = htons(ASTERISK_PORT);
- int sock = socket(AF_INET, SOCK_STREAM, 0);
- if (sock < 0) {
- ESP_LOGE(TAG, "Socket create failed");
- vTaskDelay(pdMS_TO_TICKS(2000));
- continue;
- }
- ESP_LOGI(TAG, "Checking Asterisk at %s:%d...", ASTERISK_IP, ASTERISK_PORT);
- int err = connect(sock, (struct sockaddr*)&dest, sizeof(dest));
- if (err == 0) {
- ESP_LOGI(TAG, "TCP connected → sending AMI login");
- const char *login =
- "Action: Login\r\n"
- "Username: esp32s3\r\n"
- "Secret: 1234\r\n"
- "Events: off\r\n"
- "\r\n";
- send(sock, login, strlen(login), 0);
- char buf[512];
- int len = recv(sock, buf, sizeof(buf) - 1, 0);
- if (len > 0) {
- buf[len] = 0;
- ESP_LOGI(TAG, "AMI RESPONSE:\n%s", buf);
- if (strstr(buf, "Success")) {
- ESP_LOGI(TAG, "✅ AMI LOGIN SUCCESS");
- } else {
- ESP_LOGW(TAG, "❌ AMI LOGIN FAILED");
- }
- } else {
- ESP_LOGW(TAG, "No AMI response");
- }
- } else {
- ESP_LOGW(TAG, "❌ Asterisk NOT reachable");
- }
- close(sock);
- vTaskDelay(pdMS_TO_TICKS(5000));
- }
- }
- /* ---------------- MAIN ---------------- */
- void app_main(void)
- {
- ESP_LOGI(TAG, "Starting minimal Asterisk connectivity test");
- wifi_init();
- vTaskDelay(pdMS_TO_TICKS(5000)); // wait for WiFi
- xTaskCreate(check_asterisk, "check_asterisk", 4096, NULL, 5, NULL);
- }
|