kopia lustrzana https://github.com/pjalocha/esp32-ogn-tracker
Split the WiFi code, thus now common between Stratux and APRS connection
rodzic
5bccc189b5
commit
07d3ac7e92
152
main/aprs.cpp
152
main/aprs.cpp
|
@ -13,142 +13,13 @@
|
|||
|
||||
#ifdef WITH_APRS
|
||||
|
||||
#include "wifi.h"
|
||||
#include "aprs.h"
|
||||
|
||||
#define DEBUG_PRINT
|
||||
|
||||
// tutorial on WiFi and scan: http://www.lucadentella.it/en/2017/01/09/esp32-5-wifi-scanner/
|
||||
// example on TCP/IP: https://github.com/espressif/esp-idf/blob/master/examples/protocols/http_request/main/http_request_example_main.c
|
||||
// https://github.com/lucadentella/esp32-tutorial/blob/master/04_httpclient/main/main.c
|
||||
|
||||
// ======================================================================================================
|
||||
|
||||
wifi_config_t WIFI_Config; // WIFI config: ESSID, etc.
|
||||
uint32_t WIFI_LocalIP = 0; // WIFI local IP address
|
||||
bool WIFI_isConnected(void) { return WIFI_LocalIP!=0; }
|
||||
|
||||
static esp_err_t WIFI_event_handler(void *ctx, system_event_t *event)
|
||||
{
|
||||
#ifdef DEBUG_PRINT
|
||||
xSemaphoreTake(CONS_Mutex, portMAX_DELAY);
|
||||
Format_String(CONS_UART_Write, "WIFI_event_handler => ");
|
||||
Format_SignDec(CONS_UART_Write, event->event_id);
|
||||
Format_String(CONS_UART_Write, "\n");
|
||||
xSemaphoreGive(CONS_Mutex);
|
||||
#endif
|
||||
|
||||
switch (event->event_id)
|
||||
{ case SYSTEM_EVENT_STA_START: // #2
|
||||
// esp_wifi_connect();
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_STOP: // #3
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_CONNECTED: // #4
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED: // #5
|
||||
// esp_wifi_connect();
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_GOT_IP: // #7
|
||||
// ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_LOST_IP: // #8
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return ESP_OK; }
|
||||
|
||||
static esp_err_t WIFI_Init(void)
|
||||
{ esp_err_t Err;
|
||||
tcpip_adapter_init();
|
||||
Err = esp_event_loop_init(WIFI_event_handler, NULL); if(Err!=ESP_OK) return Err;
|
||||
wifi_init_config_t Config = WIFI_INIT_CONFIG_DEFAULT();
|
||||
Err = esp_wifi_init(&Config); if(Err!=ESP_OK) return Err;
|
||||
Err = esp_wifi_set_storage(WIFI_STORAGE_RAM);
|
||||
return Err; }
|
||||
|
||||
static esp_err_t WIFI_Start(void)
|
||||
{ esp_err_t Err;
|
||||
Err = esp_wifi_set_mode(WIFI_MODE_STA); if(Err!=ESP_OK) return Err;
|
||||
Err = esp_wifi_start(); return Err; }
|
||||
|
||||
static esp_err_t WIFI_Stop(void)
|
||||
{ return esp_wifi_stop(); }
|
||||
|
||||
static esp_err_t WIFI_ActiveScan(wifi_ap_record_t *AP, uint16_t &APs)
|
||||
{ esp_err_t Err;
|
||||
wifi_scan_config_t Config = { ssid:0, bssid:0, channel:0, show_hidden:0,
|
||||
scan_type:WIFI_SCAN_TYPE_ACTIVE,
|
||||
scan_time: { active: { min:100, max:2000 } } };
|
||||
Err = esp_wifi_scan_start(&Config, 1); if(Err!=ESP_OK) return Err;
|
||||
Err = esp_wifi_scan_get_ap_records(&APs, AP); return Err; }
|
||||
|
||||
static esp_err_t WIFI_PassiveScan(wifi_ap_record_t *AP, uint16_t &APs) //
|
||||
{ esp_err_t Err;
|
||||
wifi_scan_config_t Config = { ssid:0, bssid:0, channel:0, show_hidden:0,
|
||||
scan_type:WIFI_SCAN_TYPE_PASSIVE,
|
||||
scan_time: { passive: 100 } };
|
||||
Err = esp_wifi_scan_start(&Config, 1); if(Err!=ESP_OK) return Err;
|
||||
Err = esp_wifi_scan_get_ap_records(&APs, AP); return Err; }
|
||||
|
||||
static esp_err_t WIFI_Connect(wifi_ap_record_t *AP, const char *Pass) // connect to given Access Point with gicen password
|
||||
{ esp_err_t Err;
|
||||
memcpy(WIFI_Config.sta.ssid, AP->ssid, 32);
|
||||
if(Pass) strncpy((char *)WIFI_Config.sta.password, Pass, 64);
|
||||
else WIFI_Config.sta.password[0]=0;
|
||||
memcpy(WIFI_Config.sta.bssid, AP->bssid, 6);
|
||||
WIFI_Config.sta.bssid_set = 1;
|
||||
WIFI_Config.sta.channel = AP->primary;
|
||||
WIFI_Config.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
|
||||
WIFI_Config.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL;
|
||||
WIFI_Config.sta.threshold.rssi = -127;
|
||||
Err = esp_wifi_set_config(ESP_IF_WIFI_STA, &WIFI_Config); if(Err!=ESP_OK) return Err;
|
||||
Err = esp_wifi_connect(); if(Err!=ESP_OK) return Err;
|
||||
return Err; }
|
||||
|
||||
static esp_err_t WIFI_Disconnect(void) // disconnect from WiFi AP
|
||||
{ esp_err_t Err=esp_wifi_disconnect();
|
||||
return Err; }
|
||||
|
||||
static uint32_t WIFI_getLocalIP(void) // get local IP, once DHCP phase is done
|
||||
{ esp_err_t Err;
|
||||
tcpip_adapter_ip_info_t Info;
|
||||
Err=tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &Info); if(Err!=ESP_OK) return 0;
|
||||
return Info.ip.addr; }
|
||||
|
||||
uint8_t IP_Print(char *Out, uint32_t IP)
|
||||
{ uint8_t Len=0;
|
||||
for(uint8_t Idx=0; Idx<4; Idx++)
|
||||
{ if(Idx) Out[Len++]='.';
|
||||
Len+=Format_UnsDec(Out+Len, IP&0xFF);
|
||||
IP>>=8; }
|
||||
Out[Len]=0; return Len; }
|
||||
|
||||
void IP_Print(void (*Output)(char), uint32_t IP)
|
||||
{ for(uint8_t Idx=0; Idx<4; Idx++)
|
||||
{ if(Idx) Output('.');
|
||||
Format_UnsDec(Output, IP&0xFF);
|
||||
IP>>=8; }
|
||||
}
|
||||
|
||||
uint8_t AP_Print(char *Out, wifi_ap_record_t *AP) // print numbers for a found Wi-Fi Access Point
|
||||
{ uint8_t Len=0;
|
||||
for(uint8_t Idx=0; Idx<6; Idx++)
|
||||
{ Len+=Format_Hex(Out+Len, AP->bssid[Idx]); Out[Len++]=':'; }
|
||||
Len+=Format_String(Out+Len, (const char *)(AP->ssid), 32, 32);
|
||||
Out[Len++]=' ';
|
||||
Out[Len++]='0'+AP->authmode;
|
||||
Out[Len++]=' ';
|
||||
Len+=Format_UnsDec(Out+Len, AP->primary, 2);
|
||||
Len+=Format_String(Out+Len, "ch/");
|
||||
Len+=Format_SignDec(Out+Len, AP->rssi);
|
||||
Len+=Format_String(Out+Len, "dBm ");
|
||||
Out[Len++]=AP->phy_11b ? 'B':'_';
|
||||
Out[Len++]=AP->phy_11g ? 'G':'_';
|
||||
Out[Len++]=AP->phy_11n ? 'N':'_';
|
||||
Out[Len++]=AP->phy_lr ? 'L':'_';
|
||||
Out[Len++]=AP->wps ? 'W':'_';
|
||||
Out[Len++]='\n'; Out[Len]=0;
|
||||
return Len; }
|
||||
|
||||
static wifi_ap_record_t AP[8];
|
||||
static uint16_t APs=0;
|
||||
|
||||
|
@ -282,21 +153,22 @@ void vTaskAPRS(void* pvParameters)
|
|||
#endif
|
||||
if(Err) continue; // if connection failed then give up
|
||||
|
||||
WIFI_LocalIP=0;
|
||||
WIFI_IP.ip.addr = 0;
|
||||
for(uint8_t Idx=0; Idx<10; Idx++) // wait to obtain local IP from DHCP
|
||||
{ vTaskDelay(1000);
|
||||
WIFI_LocalIP = WIFI_getLocalIP();
|
||||
if(WIFI_LocalIP) break; }
|
||||
if(WIFI_State.isConnected==1) break;
|
||||
if(WIFI_getLocalIP()) break; }
|
||||
|
||||
#ifdef DEBUG_PRINT
|
||||
xSemaphoreTake(CONS_Mutex, portMAX_DELAY);
|
||||
Format_String(CONS_UART_Write, "LocalIP: ");
|
||||
IP_Print(CONS_UART_Write, WIFI_LocalIP);
|
||||
// Format_Hex(CONS_UART_Write, WIFI_LocalIP);
|
||||
Format_String(CONS_UART_Write, "Local IP: ");
|
||||
IP_Print(CONS_UART_Write, WIFI_IP.ip.addr);
|
||||
Format_String(CONS_UART_Write, " GW: ");
|
||||
IP_Print(CONS_UART_Write, WIFI_IP.gw.addr);
|
||||
Format_String(CONS_UART_Write, "\n");
|
||||
xSemaphoreGive(CONS_Mutex);
|
||||
#endif
|
||||
if(WIFI_LocalIP==0) { WIFI_Disconnect(); continue; } // if getting local IP failed then give up
|
||||
if(WIFI_IP.ip.addr==0) { WIFI_Disconnect(); continue; } // if getting local IP failed then give up
|
||||
|
||||
int ConnErr=APRS_Socket.Connect(APRS_Host, APRS_Port); // connect to the APRS server
|
||||
if(ConnErr>=0) // if connection succesfull
|
||||
|
@ -351,7 +223,7 @@ void vTaskAPRS(void* pvParameters)
|
|||
#endif
|
||||
APRS_Socket.Disconnect();
|
||||
vTaskDelay(5000);
|
||||
WIFI_Disconnect(); WIFI_LocalIP=0;
|
||||
WIFI_Disconnect(); WIFI_IP.ip.addr=0;
|
||||
vTaskDelay(2000);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -579,11 +579,11 @@ void OLED_DrawStatusBar(u8g2_t *OLED, GPS_Position *GPS) // status bar on top
|
|||
{ u8g2_SetFont(OLED, u8g2_font_open_iconic_all_1x_t);
|
||||
u8g2_DrawGlyph(OLED, 36, 11, 0x4A); }
|
||||
#endif
|
||||
#ifdef WITH_STRATUX
|
||||
if(Stratux_isConnected())
|
||||
{ u8g2_SetFont(OLED, u8g2_font_open_iconic_all_1x_t);
|
||||
u8g2_DrawGlyph(OLED, 43, 11, 0x50); }
|
||||
#endif
|
||||
// #ifdef WITH_STRATUX
|
||||
// if(Stratux_isConnected())
|
||||
// { u8g2_SetFont(OLED, u8g2_font_open_iconic_all_1x_t);
|
||||
// u8g2_DrawGlyph(OLED, 43, 11, 0x50); }
|
||||
// #endif
|
||||
#ifdef WITH_WIFI
|
||||
if(WIFI_isConnected())
|
||||
{ u8g2_SetFont(OLED, u8g2_font_open_iconic_all_1x_t);
|
||||
|
|
129
main/stratux.cpp
129
main/stratux.cpp
|
@ -16,134 +16,7 @@
|
|||
|
||||
#ifdef WITH_STRATUX
|
||||
|
||||
// ==============================================================================================
|
||||
|
||||
wifi_config_t WIFI_Config; // WIFI config: ESSID, etc.
|
||||
tcpip_adapter_ip_info_t WIFI_IP = { 0, 0, 0 }; // WIFI local IP address, mask and gateway
|
||||
|
||||
static union
|
||||
{ uint32_t Flags;
|
||||
struct
|
||||
{ uint8_t isON : 2;
|
||||
uint8_t isConnected: 2;
|
||||
uint8_t hasIP : 2;
|
||||
} ;
|
||||
} WIFI_State;
|
||||
|
||||
bool WIFI_isConnected(void) { return WIFI_IP.ip.addr!=0; } // return "connected" status when IP from DHCP is there
|
||||
|
||||
static esp_err_t WIFI_event_handler(void *ctx, system_event_t *event)
|
||||
{
|
||||
#ifdef DEBUG_PRINT
|
||||
const char *EventName[9] = { "WIFI_READY", "SCAN_DONE", "STA_START", "STA_STOP", "STA_CONN", "STA_DISCONN", "AUTHMODE", "GOT_IP", "LOST_IP" } ;
|
||||
xSemaphoreTake(CONS_Mutex, portMAX_DELAY);
|
||||
Format_String(CONS_UART_Write, "WIFI_event_handler => ");
|
||||
if(event->event_id>=0 && event->event_id<9 && EventName[event->event_id])
|
||||
Format_String(CONS_UART_Write, EventName[event->event_id]);
|
||||
else
|
||||
Format_SignDec(CONS_UART_Write, event->event_id);
|
||||
Format_String(CONS_UART_Write, "\n");
|
||||
xSemaphoreGive(CONS_Mutex);
|
||||
#endif
|
||||
switch (event->event_id)
|
||||
{ case SYSTEM_EVENT_STA_START: // #2 after WIFI_Start()
|
||||
WIFI_State.isON=3;
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_STOP: // #3 after WIFI_Stop()
|
||||
WIFI_State.isON=1;
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_CONNECTED: // #4 after WIFI_Connect();
|
||||
WIFI_State.isConnected=3;
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED: // #5 after WIFI_Connect();
|
||||
WIFI_State.isConnected=1;
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_GOT_IP: // #7
|
||||
// ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)
|
||||
WIFI_State.hasIP=3;
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_LOST_IP: // #8
|
||||
WIFI_State.hasIP=1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return ESP_OK; }
|
||||
|
||||
static esp_err_t WIFI_Init(void)
|
||||
{ esp_err_t Err;
|
||||
tcpip_adapter_init();
|
||||
Err = esp_event_loop_init(WIFI_event_handler, NULL); if(Err!=ESP_OK) return Err;
|
||||
wifi_init_config_t Config = WIFI_INIT_CONFIG_DEFAULT();
|
||||
Err = esp_wifi_init(&Config); if(Err!=ESP_OK) return Err;
|
||||
Err = esp_wifi_set_storage(WIFI_STORAGE_RAM);
|
||||
return Err; }
|
||||
|
||||
static esp_err_t WIFI_setPowerSave(bool ON)
|
||||
{ return esp_wifi_set_ps(ON?WIFI_PS_MAX_MODEM:WIFI_PS_MIN_MODEM); }
|
||||
|
||||
static esp_err_t WIFI_Start(void)
|
||||
{ esp_err_t Err;
|
||||
Err = esp_wifi_set_mode(WIFI_MODE_STA); if(Err!=ESP_OK) return Err;
|
||||
Err = esp_wifi_start();
|
||||
return Err; }
|
||||
|
||||
static esp_err_t WIFI_Stop(void)
|
||||
{ return esp_wifi_stop(); }
|
||||
|
||||
static esp_err_t WIFI_setTxPower(int8_t TxPwr=40) // [0.25dBm] 80:L0=20dBm, 76:L1, 74:L2, 70:L3, 64:L4, 56:L5, 50:L5-2dBm, 0:L5-14dBm
|
||||
{ return esp_wifi_set_max_tx_power(TxPwr); }
|
||||
|
||||
static esp_err_t WIFI_Connect(wifi_ap_record_t *AP, const char *Pass, int8_t MinSig=(-90)) // connect to given Access Point with gicen password
|
||||
{ esp_err_t Err;
|
||||
memcpy(WIFI_Config.sta.ssid, AP->ssid, 32);
|
||||
if(Pass) strncpy((char *)WIFI_Config.sta.password, Pass, 64);
|
||||
else WIFI_Config.sta.password[0]=0;
|
||||
memcpy(WIFI_Config.sta.bssid, AP->bssid, 6);
|
||||
WIFI_Config.sta.bssid_set = 1;
|
||||
WIFI_Config.sta.channel = AP->primary;
|
||||
WIFI_Config.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
|
||||
WIFI_Config.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL;
|
||||
WIFI_Config.sta.threshold.rssi = MinSig;
|
||||
Err = esp_wifi_set_config(ESP_IF_WIFI_STA, &WIFI_Config); if(Err!=ESP_OK) return Err;
|
||||
Err = esp_wifi_connect(); if(Err!=ESP_OK) return Err;
|
||||
return Err; }
|
||||
|
||||
static esp_err_t WIFI_Connect(const char *SSID, const char *Pass, int8_t MinSig=(-90))
|
||||
{ esp_err_t Err;
|
||||
strncpy((char *)WIFI_Config.sta.ssid, SSID, 32);
|
||||
if(Pass && Pass[0]) strncpy((char *)WIFI_Config.sta.password, Pass, 64);
|
||||
else WIFI_Config.sta.password[0]=0;
|
||||
WIFI_Config.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
|
||||
WIFI_Config.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL;
|
||||
WIFI_Config.sta.threshold.rssi = MinSig;
|
||||
Err = esp_wifi_set_config(ESP_IF_WIFI_STA, &WIFI_Config); if(Err!=ESP_OK) return Err;
|
||||
Err = esp_wifi_connect(); if(Err!=ESP_OK) return Err;
|
||||
return Err; }
|
||||
|
||||
static esp_err_t WIFI_Disconnect(void) // disconnect from WiFi AP
|
||||
{ esp_err_t Err=esp_wifi_disconnect();
|
||||
return Err; }
|
||||
|
||||
static uint32_t WIFI_getLocalIP(void) // get local IP, once DHCP phase is done
|
||||
{ esp_err_t Err;
|
||||
Err=tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &WIFI_IP); if(Err!=ESP_OK) return 0;
|
||||
return WIFI_IP.ip.addr; }
|
||||
|
||||
uint8_t IP_Print(char *Out, uint32_t IP)
|
||||
{ uint8_t Len=0;
|
||||
for(uint8_t Idx=0; Idx<4; Idx++)
|
||||
{ if(Idx) Out[Len++]='.';
|
||||
Len+=Format_UnsDec(Out+Len, IP&0xFF);
|
||||
IP>>=8; }
|
||||
Out[Len]=0; return Len; }
|
||||
|
||||
void IP_Print(void (*Output)(char), uint32_t IP)
|
||||
{ for(uint8_t Idx=0; Idx<4; Idx++)
|
||||
{ if(Idx) Output('.');
|
||||
Format_UnsDec(Output, IP&0xFF);
|
||||
IP>>=8; }
|
||||
}
|
||||
#include "wifi.h"
|
||||
|
||||
// ==============================================================================================
|
||||
|
||||
|
|
|
@ -0,0 +1,161 @@
|
|||
#include <string.h>
|
||||
|
||||
#include "wifi.h"
|
||||
#include "format.h"
|
||||
|
||||
wifi_config_t WIFI_Config; // WIFI config: ESSID, etc.
|
||||
tcpip_adapter_ip_info_t WIFI_IP = { 0, 0, 0 }; // WIFI local IP address, mask and gateway
|
||||
WIFI_State_t WIFI_State;
|
||||
|
||||
bool WIFI_isConnected(void) { return WIFI_IP.ip.addr!=0; } // return "connected" status when IP from DHCP is there
|
||||
|
||||
static esp_err_t WIFI_event_handler(void *ctx, system_event_t *event)
|
||||
{
|
||||
#ifdef DEBUG_PRINT
|
||||
const char *EventName[9] = { "WIFI_READY", "SCAN_DONE", "STA_START", "STA_STOP", "STA_CONN", "STA_DISCONN", "AUTHMODE", "GOT_IP", "LOST_IP" } ;
|
||||
xSemaphoreTake(CONS_Mutex, portMAX_DELAY);
|
||||
Format_String(CONS_UART_Write, "WIFI_event_handler => ");
|
||||
if(event->event_id>=0 && event->event_id<9 && EventName[event->event_id])
|
||||
Format_String(CONS_UART_Write, EventName[event->event_id]);
|
||||
else
|
||||
Format_SignDec(CONS_UART_Write, event->event_id);
|
||||
Format_String(CONS_UART_Write, "\n");
|
||||
xSemaphoreGive(CONS_Mutex);
|
||||
#endif
|
||||
switch (event->event_id)
|
||||
{ case SYSTEM_EVENT_STA_START: // #2 after WIFI_Start()
|
||||
WIFI_State.isON=3;
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_STOP: // #3 after WIFI_Stop()
|
||||
WIFI_State.isON=1;
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_CONNECTED: // #4 after WIFI_Connect();
|
||||
WIFI_State.isConnected=3;
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED: // #5 after WIFI_Connect();
|
||||
WIFI_State.isConnected=1;
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_GOT_IP: // #7
|
||||
// ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)
|
||||
WIFI_State.hasIP=3;
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_LOST_IP: // #8
|
||||
WIFI_State.hasIP=1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return ESP_OK; }
|
||||
|
||||
esp_err_t WIFI_Init(void)
|
||||
{ esp_err_t Err;
|
||||
tcpip_adapter_init();
|
||||
Err = esp_event_loop_init(WIFI_event_handler, NULL); if(Err!=ESP_OK) return Err;
|
||||
wifi_init_config_t Config = WIFI_INIT_CONFIG_DEFAULT();
|
||||
Err = esp_wifi_init(&Config); if(Err!=ESP_OK) return Err;
|
||||
Err = esp_wifi_set_storage(WIFI_STORAGE_RAM);
|
||||
return Err; }
|
||||
|
||||
esp_err_t WIFI_setPowerSave(bool ON)
|
||||
{ return esp_wifi_set_ps(ON?WIFI_PS_MAX_MODEM:WIFI_PS_MIN_MODEM); }
|
||||
|
||||
esp_err_t WIFI_Start(void)
|
||||
{ esp_err_t Err;
|
||||
Err = esp_wifi_set_mode(WIFI_MODE_STA); if(Err!=ESP_OK) return Err;
|
||||
Err = esp_wifi_start();
|
||||
return Err; }
|
||||
|
||||
esp_err_t WIFI_Stop(void)
|
||||
{ return esp_wifi_stop(); }
|
||||
|
||||
esp_err_t WIFI_setTxPower(int8_t TxPwr) // [0.25dBm] 80:L0=20dBm, 76:L1, 74:L2, 70:L3, 64:L4, 56:L5, 50:L5-2dBm, 0:L5-14dBm
|
||||
{ return esp_wifi_set_max_tx_power(TxPwr); }
|
||||
|
||||
esp_err_t WIFI_ActiveScan(wifi_ap_record_t *AP, uint16_t &APs)
|
||||
{ esp_err_t Err;
|
||||
wifi_scan_config_t Config = { ssid:0, bssid:0, channel:0, show_hidden:0,
|
||||
scan_type:WIFI_SCAN_TYPE_ACTIVE,
|
||||
scan_time: { active: { min:100, max:2000 } } };
|
||||
Err = esp_wifi_scan_start(&Config, 1); if(Err!=ESP_OK) return Err;
|
||||
Err = esp_wifi_scan_get_ap_records(&APs, AP); return Err; }
|
||||
|
||||
esp_err_t WIFI_PassiveScan(wifi_ap_record_t *AP, uint16_t &APs) //
|
||||
{ esp_err_t Err;
|
||||
wifi_scan_config_t Config = { ssid:0, bssid:0, channel:0, show_hidden:0,
|
||||
scan_type:WIFI_SCAN_TYPE_PASSIVE,
|
||||
scan_time: { passive: 100 } };
|
||||
Err = esp_wifi_scan_start(&Config, 1); if(Err!=ESP_OK) return Err;
|
||||
Err = esp_wifi_scan_get_ap_records(&APs, AP); return Err; }
|
||||
|
||||
esp_err_t WIFI_Connect(wifi_ap_record_t *AP, const char *Pass, int8_t MinSig) // connect to given Access Point with gicen password
|
||||
{ esp_err_t Err;
|
||||
memcpy(WIFI_Config.sta.ssid, AP->ssid, 32);
|
||||
if(Pass) strncpy((char *)WIFI_Config.sta.password, Pass, 64);
|
||||
else WIFI_Config.sta.password[0]=0;
|
||||
memcpy(WIFI_Config.sta.bssid, AP->bssid, 6);
|
||||
WIFI_Config.sta.bssid_set = 1;
|
||||
WIFI_Config.sta.channel = AP->primary;
|
||||
WIFI_Config.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
|
||||
WIFI_Config.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL;
|
||||
WIFI_Config.sta.threshold.rssi = MinSig;
|
||||
Err = esp_wifi_set_config(ESP_IF_WIFI_STA, &WIFI_Config); if(Err!=ESP_OK) return Err;
|
||||
Err = esp_wifi_connect(); if(Err!=ESP_OK) return Err;
|
||||
return Err; }
|
||||
|
||||
esp_err_t WIFI_Connect(const char *SSID, const char *Pass, int8_t MinSig)
|
||||
{ esp_err_t Err;
|
||||
strncpy((char *)WIFI_Config.sta.ssid, SSID, 32);
|
||||
if(Pass && Pass[0]) strncpy((char *)WIFI_Config.sta.password, Pass, 64);
|
||||
else WIFI_Config.sta.password[0]=0;
|
||||
WIFI_Config.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
|
||||
WIFI_Config.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL;
|
||||
WIFI_Config.sta.threshold.rssi = MinSig;
|
||||
Err = esp_wifi_set_config(ESP_IF_WIFI_STA, &WIFI_Config); if(Err!=ESP_OK) return Err;
|
||||
Err = esp_wifi_connect(); if(Err!=ESP_OK) return Err;
|
||||
return Err; }
|
||||
|
||||
esp_err_t WIFI_Disconnect(void) // disconnect from WiFi AP
|
||||
{ esp_err_t Err=esp_wifi_disconnect();
|
||||
return Err; }
|
||||
|
||||
uint32_t WIFI_getLocalIP(void) // get local IP, once DHCP phase is done
|
||||
{ esp_err_t Err;
|
||||
Err=tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &WIFI_IP); if(Err!=ESP_OK) return 0;
|
||||
return WIFI_IP.ip.addr; }
|
||||
|
||||
uint8_t IP_Print(char *Out, uint32_t IP)
|
||||
{ uint8_t Len=0;
|
||||
for(uint8_t Idx=0; Idx<4; Idx++)
|
||||
{ if(Idx) Out[Len++]='.';
|
||||
Len+=Format_UnsDec(Out+Len, IP&0xFF);
|
||||
IP>>=8; }
|
||||
Out[Len]=0; return Len; }
|
||||
|
||||
void IP_Print(void (*Output)(char), uint32_t IP)
|
||||
{ for(uint8_t Idx=0; Idx<4; Idx++)
|
||||
{ if(Idx) Output('.');
|
||||
Format_UnsDec(Output, IP&0xFF);
|
||||
IP>>=8; }
|
||||
}
|
||||
|
||||
uint8_t AP_Print(char *Out, wifi_ap_record_t *AP) // print numbers for a found Wi-Fi Access Point
|
||||
{ uint8_t Len=0;
|
||||
for(uint8_t Idx=0; Idx<6; Idx++)
|
||||
{ Len+=Format_Hex(Out+Len, AP->bssid[Idx]); Out[Len++]=':'; }
|
||||
Len+=Format_String(Out+Len, (const char *)(AP->ssid), 32, 32);
|
||||
Out[Len++]=' ';
|
||||
Out[Len++]='0'+AP->authmode;
|
||||
Out[Len++]=' ';
|
||||
Len+=Format_UnsDec(Out+Len, AP->primary, 2);
|
||||
Len+=Format_String(Out+Len, "ch/");
|
||||
Len+=Format_SignDec(Out+Len, AP->rssi);
|
||||
Len+=Format_String(Out+Len, "dBm ");
|
||||
Out[Len++]=AP->phy_11b ? 'B':'_';
|
||||
Out[Len++]=AP->phy_11g ? 'G':'_';
|
||||
Out[Len++]=AP->phy_11n ? 'N':'_';
|
||||
Out[Len++]=AP->phy_lr ? 'L':'_';
|
||||
Out[Len++]=AP->wps ? 'W':'_';
|
||||
Out[Len++]='\n'; Out[Len]=0;
|
||||
return Len; }
|
||||
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
#ifndef __WIFI_H__
|
||||
#define __WIFI_H__
|
||||
|
||||
#include "tcpip_adapter.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event_loop.h"
|
||||
|
||||
extern tcpip_adapter_ip_info_t WIFI_IP; // WIFI local IP address, mask and gateway
|
||||
|
||||
typedef union
|
||||
{ uint32_t Flags;
|
||||
struct
|
||||
{ uint8_t isON : 2;
|
||||
uint8_t isConnected: 2;
|
||||
uint8_t hasIP : 2;
|
||||
} ;
|
||||
} WIFI_State_t;
|
||||
|
||||
extern WIFI_State_t WIFI_State;
|
||||
|
||||
bool WIFI_isConnected(void);
|
||||
esp_err_t WIFI_Init(void);
|
||||
esp_err_t WIFI_setPowerSave(bool ON);
|
||||
esp_err_t WIFI_Start(void);
|
||||
esp_err_t WIFI_Stop(void);
|
||||
esp_err_t WIFI_setTxPower(int8_t TxPwr=40);
|
||||
esp_err_t WIFI_Connect(wifi_ap_record_t *AP, const char *Pass, int8_t MinSig=(-90));
|
||||
esp_err_t WIFI_Connect(const char *SSID, const char *Pass, int8_t MinSig=(-90));
|
||||
esp_err_t WIFI_Disconnect(void);
|
||||
uint32_t WIFI_getLocalIP(void);
|
||||
esp_err_t WIFI_ActiveScan(wifi_ap_record_t *AP, uint16_t &APs);
|
||||
esp_err_t WIFI_PassiveScan(wifi_ap_record_t *AP, uint16_t &APs);
|
||||
uint8_t AP_Print(char *Out, wifi_ap_record_t *AP);
|
||||
uint8_t IP_Print(char *Out, uint32_t IP);
|
||||
void IP_Print(void (*Output)(char), uint32_t IP);
|
||||
|
||||
#endif // __WIFI_H__
|
Ładowanie…
Reference in New Issue