Skip to content

Commit

Permalink
wifi connection added
Browse files Browse the repository at this point in the history
  • Loading branch information
astinbiju44 committed Aug 31, 2023
1 parent e9997a7 commit 5797d80
Showing 1 changed file with 67 additions and 34 deletions.
101 changes: 67 additions & 34 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,50 +1,83 @@
#include<driver/gpio.h>
#include <rom/gpio.h>
#include<freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <stdio.h> //for basic printf commands
#include <string.h> //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();

}

0 comments on commit 5797d80

Please sign in to comment.