Part 2 - Connecting an ESP8266 to an Access Point
The ESP8266 has built in WIFI. Next we will connect to an access point, get the MAC address and IP configuration.
Get MAC address
The command to get the MAC
bool wifi_get_macaddr(uint8 if_index , uint8 *macaddr)
For station Mode, set if_index to 0x00, for the mac address of the soft access point set if_index to 0x01.
Get Connection Status
uint8 wifi_station_get_connect_status(void)
The return value
STATION_IDLE = 0
STATION_CONNECTING = 1
STATION_WRONG_PASSWORD = 2
STATION_NO_AP_FOUND = 3
STATION_CONNECT_FAIL = 4
STATION_GOT_IP = 5
Get IP Configuration
bool wifi_get_ip_info(uint8 if_index, struct ip_info *info)
For station Mode, set if_index to 0x00, for the mac address of the soft access point set if_index to 0x01.
struct ip_info
{
struct ip_addr ip;
struct ip_addr netmask;
struct ip_addr gw;
}
struct ip_addr
{
uint32 addr;
}
Set WIFI Mode
bool wifi_set_opmode (uint8 opmode)
Set opmode to control to mode
0x01 STATION_MODE
0x02 SOFTAP_MODE
0x03 STATIONAP_MODE.
Full Code
#include "osapi.h"
#include "gpio.h"
#include "os_type.h"
#include "user_config.h"
#include "user_interface.h"
#define user_procTaskPrio 0
#define user_procTaskQueueLen 1
os_event_t user_procTaskQueue[user_procTaskQueueLen];
static void loop(os_event_t *events);
// Print Ip Address
void print_ip(char *desc, uint32 addr)
{
os_printf("%s", desc);
if(addr>0)
{
os_printf("%d.%d.%d.%d", IP2STR(&addr));
}
else
os_printf("Not set");
os_printf("\n\r");
}
void show_connection()
{
os_printf("\n\rCurrent Details\n\r");
// Show MAC
char mac_address[32];
wifi_get_macaddr(0, &mac_address[0]);
os_printf("MAC Address %02x-%02x-%02x-%02x-%02x-%02x\n\r",
(unsigned)mac_address[0], (unsigned)mac_address[1],
(unsigned)mac_address[2], (unsigned)mac_address[3],
(unsigned)mac_address[4], (unsigned)mac_address[5]);
// Get Status
os_printf("Connect status - %d\n\r",
wifi_station_get_connect_status());
// Get Config
struct station_config current_config;
wifi_station_get_config(¤t_config);
os_printf("Curent Config (Station - %s, Paswword - %s)\n\r",
current_config.ssid, current_config.password);
// Get IP Info
struct ip_info ipinfo;
ipinfo.ip.addr=0;
ipinfo.netmask.addr=255;
ipinfo.gw.addr=0;
wifi_get_ip_info(0, &ipinfo);
print_ip("IP Address: ", ipinfo.ip.addr);
print_ip("Netmask : ", ipinfo.netmask.addr);
print_ip("Gateway : ", ipinfo.gw.addr);
}
//Main code function
static void ICACHE_FLASH_ATTR
loop(os_event_t *events)
{
show_connection();
os_delay_us(300000);
system_os_post(user_procTaskPrio, 0, 0 );
}
//Init function
void ICACHE_FLASH_ATTR
user_init()
{
char ssid[32] = "my_accesspoint";
char password[64] = "my_password";
struct station_config stationConf;
//Set station mode
wifi_set_opmode( 0x1 );
//Set ap settings
os_memcpy(&stationConf.ssid, ssid, 32);
os_memcpy(&stationConf.password, password, 64);
wifi_station_set_config(&stationConf);
uart_div_modify(0,UART_CLK_FREQ / 115200);
//Start os task
system_os_task(loop, user_procTaskPrio,user_procTaskQueue,
user_procTaskQueueLen);
system_os_post(user_procTaskPrio, 0, 0 );
}