Intermediate
20 min

Reimagine the way you connect to the world using DA16200MOD-AA and ATmega1284

Connect anywhere, anytime with ultra-low-power Wi-Fi innovation!

LP WiFi Click with EasyAVR v7

Published Nov 09, 2023

Click board™

LP WiFi Click

Dev. board

EasyAVR v7

Compiler

NECTO Studio

MCU

ATmega1284

Experience a new era of seamless connectivity as we introduce our innovative ultra-low-power Wi-Fi solution, redefining the way you stay connected on the go.

A

A

Hardware Overview

How does it work?

LP WiFi Click is based on the DA16200MOD-AA, a highly integrated ultra-low power WiFi module with the best RF performance and a comfortable development environment from Renesas. It consists of the DA16200 SoC, 4MB flash memory, RF components, crystal oscillator, RF lumped filter, and an onboard 2.4GHz chip antenna. Such low-power operation can extend the battery life for a year or more, depending on the application, even when the board is continuously connected to the WiFi network. This module also features strong IoT security, including WPA3 and TLS for authentication and encryption at WiFi and higher stack layers. The DA16200MOD has an integrated RTC block (36-bit real-time counter) with a 32.768kHz clock source necessary for the free-running counter in the RTC block of the SoC, which provides power management and function control for low-power operation. The RTC block

is always powered ON when an onboard switch labeled POWER is set to the appropriate ON position during normal operation. This optimizes power consumption and is used for power ON/OFF purposes. Also, the upper left header of the board, labeled as RTC Block Pads, is connected to the internal RTC block to connect and receive external event signals from an external device like a sensor. LP WiFi Click communicates with MCU using the UART interface as its default communication protocol. Users can use other interfaces, such as SPI and I2C, to improve and communicate with peripherals. It should be noted that the DA16200 module comes with firmware that only supports UART communication with the host microcontroller and I2C with peripherals such as sensors (SPI interface is not supported by default and can be enabled by firmware update). Additional options that this board has are certain

buttons and headers. The onboard pushbuttons are labeled RESET, and WPS represents a Factory reset button and WiFi Protected Setup (WPS) button. As for the headers, the first one marked with TX and RX signals is suitable for debugging purposes using the UART interface, while the second lets you add a small but bright and crisp OLED display to your design by connecting it to the upper-right header with I2C signals suitable for controlling the OLED. This Click board™ can be operated only with a 3.3V logic voltage level. The board must perform appropriate logic voltage level conversion before using MCUs with different logic levels. Also, it comes equipped with a library containing functions and an example code that can be used as a reference for further development.

LP WiFi Click hardware overview image

Features overview

Development board

EasyAVR v7 is the seventh generation of AVR development boards specially designed for the needs of rapid development of embedded applications. It supports a wide range of 16-bit AVR microcontrollers from Microchip and has a broad set of unique functions, such as a powerful onboard mikroProg programmer and In-Circuit debugger over USB. The development board is well organized and designed so that the end-user has all the necessary elements in one place, such as switches, buttons, indicators, connectors, and others. With four different connectors for each port, EasyAVR v7 allows you to connect accessory boards, sensors, and custom electronics more

efficiently than ever. Each part of the EasyAVR v7 development board contains the components necessary for the most efficient operation of the same board. An integrated mikroProg, a fast USB 2.0 programmer with mikroICD hardware In-Circuit Debugger, offers many valuable programming/debugging options and seamless integration with the Mikroe software environment. Besides it also includes a clean and regulated power supply block for the development board. It can use a wide range of external power sources, including an external 12V power supply, 7-12V AC or 9-15V DC via DC connector/screw terminals, and a power source via the USB Type-B (USB-B)

connector. Communication options such as USB-UART and RS-232 are also included, alongside the well-established mikroBUS™ standard, three display options (7-segment, graphical, and character-based LCD), and several different DIP sockets which cover a wide range of 16-bit AVR MCUs. EasyAVR v7 is an integral part of the Mikroe ecosystem for rapid development. Natively supported by Mikroe software tools, it covers many aspects of prototyping and development thanks to a considerable number of different Click boards™ (over a thousand boards), the number of which is growing every day.

EasyAVR v7 horizontal image

Microcontroller Overview

MCU Card / MCU

ATmega1284

Architecture

AVR

MCU Memory (KB)

128

Silicon Vendor

Microchip

Pin count

40

RAM (Bytes)

16384

Used MCU Pins

mikroBUS™ mapper

NC
NC
AN
Wake-Up
PA6
RST
SPI Chip Select
PA5
CS
SPI Clock
PB7
SCK
SPI Data OUT
PB6
MISO
SPI Data IN
PB5
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
PWM Signal
PD4
PWM
Interrupt
PD2
INT
UART TX
PD1
TX
UART RX
PD0
RX
I2C Clock
PC0
SCL
I2C Data
PC1
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Click board™ Schematic

LP WiFi Click Schematic schematic

Step by step

Project assembly

EasyAVR v7 front image hardware assembly

Start by selecting your development board and Click board™. Begin with the EasyAVR v7 as your development board.

EasyAVR v7 front image hardware assembly
GNSS2 Click front image hardware assembly
EasyAVR v7 Access DIP MB 1 - upright/background hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
NECTO Compiler Selection Step Image hardware assembly
NECTO Output Selection Step Image hardware assembly
Necto image step 6 hardware assembly
Necto DIP image step 7 hardware assembly
EasyPIC PRO v7a Display Selection Necto Step hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Necto PreFlash Image hardware assembly

Track your results in real time

Application Output

1. Application Output - In Debug mode, the 'Application Output' window enables real-time data monitoring, offering direct insight into execution results. Ensure proper data display by configuring the environment correctly using the provided tutorial.

2. UART Terminal - Use the UART Terminal to monitor data transmission via a USB to UART converter, allowing direct communication between the Click board™ and your development system. Configure the baud rate and other serial settings according to your project's requirements to ensure proper functionality. For step-by-step setup instructions, refer to the provided tutorial.

3. Plot Output - The Plot feature offers a powerful way to visualize real-time sensor data, enabling trend analysis, debugging, and comparison of multiple data points. To set it up correctly, follow the provided tutorial, which includes a step-by-step example of using the Plot feature to display Click board™ readings. To use the Plot feature in your code, use the function: plot(*insert_graph_name*, variable_name);. This is a general format, and it is up to the user to replace 'insert_graph_name' with the actual graph name and 'variable_name' with the parameter to be displayed.

Software Support

Library Description

This library contains API for LP WiFi Click driver.

Key functions:

  • lpwifi_send_cmd - Send command function.

  • lpwifi_connect_to_ap - Connect to AP function.

  • lpwifi_factory_reset_device - Device factory reset function.

Open Source

Code example

The complete application code and a ready-to-use project are available through the NECTO Studio Package Manager for direct installation in the NECTO Studio. The application code can also be found on the MIKROE GitHub account.

/*!
 * @file main.c
 * @brief LP WiFi Click Example.
 *
 * # Description
 * This example reads and processes data from LP WiFi clicks.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver and powers up the module, then connects to the desired AP
 * and creates TCP and UDP servers on the desired local port.
 *
 * ## Application Task
 * Logs all the data received from TCP/UDP clients on the USB UART.
 *
 * ## Additional Function
 * - static void lpwifi_clear_app_buf ( void )
 * - static err_t lpwifi_process ( void )
 * - static void lpwifi_error_check( err_t error_flag )
 * - static void lpwifi_log_app_buf ( void )
 * - static err_t lpwifi_rsp_check ( void )
 * - static void lpwifi_check_connection( void )
 *
 * @note
 * In order for the example to work, user needs to set the AP SSID, password, and Local port
 * on which the TCP server and UDP socket will be created.
 * Enter valid data for the following macros: AP_SSID, AP_PASSWORD and LOCAL_PORT.
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "lpwifi.h"
#include "string.h"
#include "generic_pointer.h"

// Set AP SSID
#define AP_SSID                     ""

// Set AP password - if the AP is OPEN remain this NULL
#define AP_PASSWORD                 ""

// Set Local port on which the TCP server and UDP socket will be created.
#define LOCAL_PORT                  1

#define APP_OK                      0
#define APP_ERROR_DRIVER            -1
#define APP_ERROR_OVERFLOW          -2
#define APP_ERROR_TIMEOUT           -3

#define RSP_OK                      "OK"
#define RSP_ERROR                   "ERROR"

#define PROCESS_BUFFER_SIZE         400

#define WAIT_FOR_CONNECTION         0
#define CONNECTED_TO_AP             1
#define NOT_CONNECTED_TO_AP         2

static lpwifi_t lpwifi;
static log_t logger;

static char app_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
static int32_t app_buf_len                 = 0;
static int32_t app_buf_cnt                 = 0;

static char assigned_ip_address[ 25 ]      = { 0 };
static uint8_t app_connection_status       = WAIT_FOR_CONNECTION;

static err_t app_error_flag;

/**
 * @brief LP WiFi clearing application buffer.
 * @details This function clears memory of application buffer and reset it's length and counter.
 * @note None.
 */
static void lpwifi_clear_app_buf ( void );

/**
 * @brief LP WiFi data reading function.
 * @details This function reads data from device and concatenates data to application buffer.
 *
 * @return @li @c  0 - Read some data.
 *         @li @c -1 - Nothing is read.
 *         @li @c -2 - Application buffer overflow.
 *
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lpwifi_process ( void );

/**
 * @brief LP WiFi check for errors.
 * @details This function checks for different types of errors and logs them on UART.
 * @note None.
 */
static void lpwifi_error_check( err_t error_flag );

/**
 * @brief LP WiFi logs application buffer.
 * @details This function logs data from application buffer.
 * @note None.
 */
static void lpwifi_log_app_buf ( void );

/**
 * @brief LP WiFi response check.
 * @details This function checks for response and returns the status of response.
 *
 * @return application status.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lpwifi_rsp_check ( void );

/**
 * @brief LP WiFi check connection.
 * @details This function checks connection to the AP, and fills the assigned_ip_address buffer and
 *          logs the response on the USB UART if it is connected successfully.
 *
 * @note None.
 */
static void lpwifi_check_connection( void );

/**
 * @brief LP WiFi str cut chr function.
 * @details This function removes all selected characters from string str,
 * and returns it to the same str without those characters.
 * @param str Address of string.
 * @param chr Character to cut.
 */
static void lpwifi_str_cut_chr ( uint8_t *str, uint8_t chr );

void application_init ( void )
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    lpwifi_cfg_t lpwifi_cfg;  /**< Click config object. */

    /**
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX
     * are defined as HAL_PIN_NC, you will
     * need to define them manually for log to work.
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info( &logger, " Application Init " );
    Delay_ms ( 100 );

    // Click initialization.

    lpwifi_cfg_setup( &lpwifi_cfg );
    LPWIFI_MAP_MIKROBUS( lpwifi_cfg, MIKROBUS_1 );
    err_t init_flag = lpwifi_init( &lpwifi, &lpwifi_cfg );
    if ( UART_ERROR == init_flag )
    {
        log_error( &logger, " Application Init Error. " );
        log_info( &logger, " Please, run program again... " );

        for ( ; ; );
    }

    lpwifi_default_cfg( &lpwifi );
    Delay_ms ( 500 );

    // Initiate the communication
    lpwifi_send_cmd( &lpwifi, LPWIFI_CMD_AT );
    Delay_ms ( 1000 );

    // Dummy read
    lpwifi_process( );
    lpwifi_clear_app_buf( );

    log_printf( &logger, "\r\n --- Factory reset --- \r\n" );
    lpwifi_factory_reset_device ( &lpwifi );
    Delay_ms ( 500 );
    // Enable Echo
    lpwifi_send_cmd( &lpwifi, LPWIFI_CMD_ATE );
    app_error_flag = lpwifi_rsp_check( );
    lpwifi_error_check( app_error_flag );
    Delay_ms ( 500 );
    log_printf( &logger, " ----------------------------------------------- \r\n" );

    log_printf( &logger, "\r\n --- Connecting to the access point --- \r\n" );
    // Connect to AP
    lpwifi_connect_to_ap( &lpwifi, AP_SSID, AP_PASSWORD );
    app_error_flag = lpwifi_rsp_check( );
    lpwifi_error_check( app_error_flag );
    lpwifi_check_connection();
    while ( CONNECTED_TO_AP != app_connection_status )
    {
        lpwifi_check_connection();
        if ( NOT_CONNECTED_TO_AP == app_connection_status )
        {
            Delay_ms ( 500 );
            app_connection_status = WAIT_FOR_CONNECTION;

            // Connect to AP
            lpwifi_connect_to_ap( &lpwifi, AP_SSID, AP_PASSWORD );
            app_error_flag = lpwifi_rsp_check( );
            lpwifi_error_check( app_error_flag );
        }
    }
    log_printf( &logger, " ----------------------------------------------- \r\n" );
    log_printf( &logger, "\r\n --- Creating a TCP server --- \r\n" );
    // Create TCP Server
    lpwifi_create_tcp_server( &lpwifi, LOCAL_PORT );
    app_error_flag = lpwifi_rsp_check( );
    lpwifi_error_check( app_error_flag );
    Delay_ms ( 500 );

    log_printf( &logger, " ----------------------------------------------- \r\n" );
    log_printf( &logger, "\r\n --- Creating a UDP socket --- \r\n" );
    // Create UDP socket
    lpwifi_create_udp_socket( &lpwifi, LOCAL_PORT );
    app_error_flag = lpwifi_rsp_check( );
    lpwifi_error_check( app_error_flag );
    Delay_ms ( 500 );

    log_printf( &logger, " ----------------------------------------------- \r\n" );
    log_printf( &logger, " TCP server and UDP socket are available at: \r\n" );
    log_printf( &logger, "    SSID: \"%s\"\r\n    IP: %s\r\n    Port: %u", ( char * ) AP_SSID,
                                                                           ( char * ) assigned_ip_address,
                                                                         ( uint16_t ) LOCAL_PORT );
    log_printf( &logger, "\r\n ----------------------------------------------- \r\n" );
    log_printf( &logger, " You can connect to it via a TCP/UDP client." );
    log_printf( &logger, "\r\n ----------------------------------------------- \r\n" );
}

void application_task ( void )
{
    lpwifi_process( );
    lpwifi_log_app_buf( );
}

int main ( void ) 
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif
    
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}

static void lpwifi_clear_app_buf ( void )
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
    app_buf_cnt = 0;
}

static err_t lpwifi_process ( void )
{
    int32_t rx_size;
    char rx_buff[ PROCESS_BUFFER_SIZE ] = { 0 };

    rx_size = lpwifi_generic_read( &lpwifi, rx_buff, PROCESS_BUFFER_SIZE );

    if ( rx_size > 0 )
    {
        int32_t buf_cnt = 0;

        if ( app_buf_len + rx_size >= PROCESS_BUFFER_SIZE )
        {
            lpwifi_clear_app_buf(  );
            return LPWIFI_ERROR;
        }
        else
        {
            buf_cnt = app_buf_len;
            app_buf_len += rx_size;
        }

        for ( int32_t rx_cnt = 0; rx_cnt < rx_size; rx_cnt++ )
        {
            if ( rx_buff[ rx_cnt ] != 0 )
            {
                app_buf[ ( buf_cnt + rx_cnt ) ] = rx_buff[ rx_cnt ];
            }
            else
            {
                app_buf_len--;
                buf_cnt--;
            }

        }
        return LPWIFI_OK;
    }
    return LPWIFI_ERROR;
}

static err_t lpwifi_rsp_check ( void )
{
    uint16_t timeout_cnt = 0;
    uint16_t timeout = 10000;

    err_t error_flag = lpwifi_process(  );

    if ( ( error_flag != 0 ) && ( error_flag != -1 ) )
    {
        return error_flag;
    }

    while ( ( strstr( app_buf, RSP_OK ) == 0 ) && ( strstr( app_buf, RSP_ERROR ) == 0 ) )
    {
        error_flag = lpwifi_process(  );
        if ( ( error_flag != 0 ) && ( error_flag != -1 ) )
        {
            return error_flag;
        }

        timeout_cnt++;
        if ( timeout_cnt > timeout )
        {
            // Initialize AT command
            while ( ( 0 == strstr( app_buf, RSP_OK ) ) && ( 0 == strstr( app_buf, RSP_ERROR ) ) )
            {
                lpwifi_send_cmd( &lpwifi, LPWIFI_CMD_ATZ );
                lpwifi_process(  );
                Delay_ms ( 100 );
            }
            lpwifi_clear_app_buf(  );
            // Enable Echo
            while ( ( 0 == strstr( app_buf, RSP_OK ) ) && ( 0 == strstr( app_buf, RSP_ERROR ) ) )
            {
                lpwifi_send_cmd( &lpwifi, LPWIFI_CMD_ATE );
                lpwifi_process(  );
                Delay_ms ( 100 );
            }
            lpwifi_clear_app_buf(  );
            return APP_ERROR_TIMEOUT;
        }

        Delay_ms ( 1 );
    }

    lpwifi_log_app_buf();

    return APP_OK;
}

static void lpwifi_error_check( err_t error_flag )
{
    if ( ( error_flag != APP_OK ) && ( error_flag != APP_ERROR_DRIVER ) )
    {
        switch ( error_flag )
        {
            case APP_ERROR_OVERFLOW:
                log_error( &logger, " Overflow!" );
                break;
            case APP_ERROR_TIMEOUT:
                log_error( &logger, " Timeout!" );
                break;
            default:
                break;
        }
    }
}

static void lpwifi_log_app_buf ( void )
{
    for ( int32_t buf_cnt = 0; buf_cnt < app_buf_len; buf_cnt++ )
    {
        log_printf( &logger, "%c", app_buf[ buf_cnt ] );
    }

    lpwifi_clear_app_buf(  );
}

static void lpwifi_check_connection( void )
{
    #define CONNECTED     "+WFJAP:1"
    #define NOT_CONNECTED "+WFJAP:0"

    lpwifi_process( );
    if ( 0 != strstr( app_buf, CONNECTED ) )
    {
        #define IP_DELIMITER "',"
        char * __generic_ptr app_buf_ptr;
        Delay_ms ( 200 );
        lpwifi_process( );
        app_buf_ptr = strstr( app_buf, IP_DELIMITER );
        strcpy( assigned_ip_address, app_buf_ptr );
        lpwifi_str_cut_chr( assigned_ip_address, '\'' );
        lpwifi_str_cut_chr( assigned_ip_address, ',' );
        lpwifi_str_cut_chr( assigned_ip_address, '\r' );
        lpwifi_str_cut_chr( assigned_ip_address, '\n' );
        lpwifi_log_app_buf( );
        app_connection_status = CONNECTED_TO_AP;
    }
    else if ( 0 != strstr( app_buf, NOT_CONNECTED ) )
    {
        app_connection_status = NOT_CONNECTED_TO_AP;
    }
}

static void lpwifi_str_cut_chr ( uint8_t *str, uint8_t chr )
{
    uint16_t cnt_0, cnt_1;
    for ( cnt_0 = 0; cnt_0 < strlen( str ); cnt_0++ )
    {
        if ( str[ cnt_0 ] == chr )
        {
            for ( cnt_1 = cnt_0; cnt_1 < strlen( str ); cnt_1++ )
            {
                str[ cnt_1 ] = str[ cnt_1 + 1 ];
            }
        }
    }
}

// ------------------------------------------------------------------------ END

Additional Support

Resources

Love this project?

'Buy This Kit' button takes you directly to the shopping cart where you can easily add or remove products.