potion_party_leds/main/network.h

69 lines
1.8 KiB
C
Raw Normal View History

2023-09-15 06:21:00 +00:00
#ifndef _server_h
#define _server_h
#include "esp_wifi_types.h"
#include "shared.h"
#include "esp_event.h"
static
void on_station_connects(wifi_event_ap_staconnected_t* event) {
LOGLN("Station %d connected", event->aid);
}
static
void on_station_disconnects(wifi_event_ap_stadisconnected_t* event) {
LOGLN("Station %d disconnected", event->aid);
}
static
void on_wifi_event(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data) {
switch(event_id) {
case WIFI_EVENT_AP_STACONNECTED:
on_station_connects(event_data);
break;
case WIFI_EVENT_AP_STADISCONNECTED:
on_station_disconnects(event_data);
break;
}
}
static inline
void wifi_init() {
LOGLN("Configuring WIFI");
// init tcp/ip stack
tcpip_adapter_init();
// initialize wifi
wifi_init_config_t wifi_startup = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&wifi_startup));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &on_wifi_event, NULL));
LOGLN("WiFi initialized");
}
static inline
void softap_init() {
LOGLN("Starting wireless Access Point");
// set mode to wifi
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
// configure wifi hardware to serve as a wifi access point
wifi_config_t accesspoint_startup_config = {
.ap = {
.ssid = SSID,
.ssid_len = strlen(SSID),
.password = PASSW,
.max_connection=32,
.authmode = WIFI_AUTH_WPA_WPA2_PSK
}
};
// configure with created startup config
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &accesspoint_startup_config));
// start wifi access point
ESP_ERROR_CHECK(esp_wifi_start());
LOGLN("Opened AP SSID: \"%s\" PW: \"%s\"", SSID, PASSW);
}
#endif // !_server_h