From 5797d80c47bce3e5ce44f3ed748393dad130f375 Mon Sep 17 00:00:00 2001 From: Astin Biju Date: Thu, 31 Aug 2023 21:46:33 +0530 Subject: [PATCH] wifi connection added --- src/main.c | 101 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 34 deletions(-) diff --git a/src/main.c b/src/main.c index 81e2f9a..4805cf5 100644 --- a/src/main.c +++ b/src/main.c @@ -1,50 +1,83 @@ -#include -#include -#include -#include +#include //for basic printf commands +#include //for handling strings +#include "freertos/FreeRTOS.h" //for delay,mutexs,semphrs rtos operations +#include "esp_system.h" //esp_init funtions esp_err_t +#include "esp_wifi.h" //esp_wifi_init functions and wifi operations +#include "esp_log.h" //for showing logs +#include "esp_event.h" //for wifi event +#include "nvs_flash.h" //non volatile storage -#define LED_PIN 13 +#include "lwip/sockets.h" +#include "lwip/dns.h" +#include "lwip/netdb.h" -void ledON() { - printf("LED ON"); - gpio_set_level(LED_PIN,0); -} - -void ledOFF() { - printf("LED OFF"); - gpio_set_level(LED_PIN,1); -} - - -void led_blink(void *pvParameters) -{ - gpio_pad_select_gpio(LED_PIN); - gpio_set_direction(LED_PIN,GPIO_MODE_OUTPUT); - while(1){ - ledON(); - vTaskDelay(1000/portTICK_PERIOD_MS); +const char *ssid = "motorola"; +const char *pass = "astinbiju44"; +int retry_num=0; - ledOFF(); - vTaskDelay(1000/portTICK_PERIOD_MS); - - - - } +static void wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id,void *event_data){ +if(event_id == WIFI_EVENT_STA_START) +{ + printf("WIFI CONNECTING....\n"); +} +else if (event_id == WIFI_EVENT_STA_CONNECTED) +{ + printf("WiFi CONNECTED\n"); +} +else if (event_id == WIFI_EVENT_STA_DISCONNECTED) +{ + printf("WiFi lost connection\n"); + if(retry_num<5){esp_wifi_connect();retry_num++;printf("Retrying to Connect...\n");} +} +else if (event_id == IP_EVENT_STA_GOT_IP) +{ + printf("Wifi got IP...\n\n"); +} } - -void app_main() { - - -xTaskCreate(&led_blink,"LED_BLINK",512,NULL,5,NULL); +void wifi_connection() +{ + + esp_netif_init(); + esp_event_loop_create_default(); // event loop + esp_netif_create_default_wifi_sta(); // WiFi station + wifi_init_config_t wifi_initiation = WIFI_INIT_CONFIG_DEFAULT(); + esp_wifi_init(&wifi_initiation); // + 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_configuration = { + .sta = { + .ssid = "", + .password = "", + + } + + }; + strcpy((char*)wifi_configuration.sta.ssid, ssid); + strcpy((char*)wifi_configuration.sta.password, pass); + //esp_log_write(ESP_LOG_INFO, "Kconfig", "SSID=%s, PASS=%s", ssid, pass); + esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_configuration); + // 3 - Wi-Fi Start Phase + esp_wifi_start(); + esp_wifi_set_mode(WIFI_MODE_STA); + // 4- Wi-Fi Connect Phase + esp_wifi_connect(); + printf( "wifi_init_softap finished. SSID:%s password:%s",ssid,pass); + +} +void app_main(void) +{ +nvs_flash_init(); +wifi_connection(); + } \ No newline at end of file