Sunday 25 January 2015

Connecting an ESP8266 to an Access Point

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 "ets_sys.h"
#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(&current_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 );
}

ESP8266 Serial/Wifi/Iot

Part 1 - ESP8266 ESP-01

One of these arrived in the post a couple of days ago.

Connecting a ESP8266-01 to Raspberry Pi


In its basic form it is a serial to wifi converter, with built in network stack and aerial. Connect to the serial port and you can connect out through wifi. This is a simple solution to connect an existing project to a network over a serial connection.

What interested me was being able to write my own firmware, control the GPIO's and send data wirelessly. For simple projects an external processor should not be required, for example, report the temperature every hour and save to a webserver.

Programming with a Raspberry Pi


Connecting a ESP8266-01 to Raspberry Pi

Connect ground of the power supply to ground of the ESP8266 and to the ground of the Raspberry Pi, 3.3v to Vcc, and tx & rx to a 3.3v serial connection. Most serial ports are 12v and that will fry the ESP-01.

I've used a Raspberry Pi B as the serial port is already at 3.3v so no level shifts are required. I've used a separate power supply as the B does not supply enough current to power the board directly,

The board comes with built in "AT" firmware. This is basically an extension of the Hayes modem commands allowing you to control the board over a serial connection.

The current firmware requires a CR+LF so ensure this is enabled on your terminal. You will not get a response otherwise.

Standard commands are AT, this should get back an OK from the device to say its working

AT

OK

Get the current firmware version AT+GMR

AT

00170901

OK



Writing Your Own Firmware


This is where it gets interested! There is plenty of guides how to install the development environment.

I started with the "basic_example"

#include "ets_sys.h"
#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);

//Main code function
static void ICACHE_FLASH_ATTR
loop(os_event_t *events)
{
    os_printf("Hello\n\r");
    os_delay_us(10000);
    system_os_post(user_procTaskPrio, 0, 0 );
}

//Init function
void ICACHE_FLASH_ATTR
user_init()
{
    // Set baud rate of debug port
    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 );
}

The extra line

    uart_div_modify(0,UART_CLK_FREQ / 115200);

sets the baud rate of the debug port, otherwise is defaults to 74880

After flashing I use piccom and this is the result:

Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello

Thanks for using picocom
pi@raspberrypi /opt/Espressif $

Next stage is to use WIFI (2. Connect to an Access Point)