advait_projects/duckdnsproj/main/main.c

139 lines
5.1 KiB
C

#include <WiFi.h>
#include <HTTPClient.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include "esp_mac.h"
#include "esp_http_client.h"
/* WiFi credentials */
#define STATIC_IP "192.168.1.100"
#define STATIC_GW "192.168.1.1"
#define STATIC_NETMASK "255.255.255.0"
#define EXAMPLE_ESP_WIFI_SSID "UCLA_WEB"
#define EXAMPLE_ESP_WIFI_PASS "" // Empty password for the open WiFi
#define EXAMPLE_ESP_MAXIMUM_RETRY 5
/* DuckDNS URL */
#define DUCKDNS_URL "https://www.duckdns.org/update?domains=advaitsrushty&token=7432970b-84d6-454f-a024-9b255b65933a&ip=" // Replace with your DuckDNS domain and token
/* Event group for signaling connection events */
static EventGroupHandle_t s_wifi_event_group;
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
static const char *TAG = "wifi station";
static int s_retry_num = 0;
static void 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(); // Attempt to connect to the network
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
esp_wifi_connect(); // Retry connection
s_retry_num++;
ESP_LOGI(TAG, "Retrying to connect to the AP");
} else {
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); // Set fail bit if max retries are reached
}
ESP_LOGI(TAG,"Connection to AP failed");
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "Got IP address: " IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0; // Reset retry count
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); // Set connected bit
// Update DuckDNS with the current IP
char ip_str[16];
snprintf(ip_str, sizeof(ip_str), IPSTR, IP2STR(&event->ip_info.ip)); // Get the IP as a string
update_duckdns(ip_str); // Update DuckDNS with the current IP address
}
}
void update_duckdns(const char* ip_address) {
HTTPClient http;
String url = "https://www.duckdns.org/update?domains=advaitsrushty&token=7432970b-84d6-454f-a024-9b255b65933a&ip=";
url += ip_address;
// Make HTTP GET request to DuckDNS to update the IP
http.begin(url); // Specify the URL
int httpCode = http.GET(); // Send the GET request
if (httpCode == 200) {
ESP_LOGI(TAG, "DuckDNS IP updated successfully to: %s", ip_address);
} else {
ESP_LOGI(TAG, "Failed to update DuckDNS. HTTP Code: %d", httpCode);
}
http.end();
}
void wifi_init_sta(void) {
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
esp_netif_ip_info_t ip_info;
ip_info.ip.addr = esp_ip4addr_aton(STATIC_IP);
ip_info.gw.addr = esp_ip4addr_aton(STATIC_GW);
ip_info.netmask.addr = esp_ip4addr_aton(STATIC_NETMASK);
esp_netif_set_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip_info);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS,
.threshold.authmode = WIFI_AUTH_OPEN,
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
esp_netif_set_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip_info);
ESP_LOGI(TAG, "WiFi initialization completed.");
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "Successfully connected to SSID: %s", EXAMPLE_ESP_WIFI_SSID);
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID: %s", EXAMPLE_ESP_WIFI_SSID);
} else {
ESP_LOGE(TAG, "Unexpected event occurred.");
}
}
void app_main(void) {
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
ESP_LOGI(TAG, "Starting WiFi Station Mode");
wifi_init_sta();
}