5.1.1 Execution

main.c – Initialize the board, connect to an Exosite Cloud and upload the temperature details to the cloud.

The SAM W25 module should now be in AP mode and be listed as a Wi-Fi network with the same name as shown in the image below (Atmel_SAMW25_XX:XX). Notice that the last two bytes of the MAC address are appended to the SSID. Simply connect your PC/smartphone to the module in AP mode.

#define MAIN_M2M_DEVICE_NAME    "Atmel_SAMW25_00:00"
Note: Once connected, open your favorite web browser at the following address http://atmelconfig.com and provide the required Network Name (SSID) and Passphrase (device name can be blank) fields of the Wi-Fi AP the SAM W25 is supposed to connect to.

Once the SAM W25 is connected to the AP with internet connectivity, it will immediately connect to the Exosite messaging service and will start sending temperature and light data.

For this application, you can start with the main.c file, which has all the code needed to perform the system initialization by configuring the necessary components such as UART, buttons, LEDs, BSP, temperature sensor, Wi-Fi driver and socket.

The WiFi initialization and any other configurations required should be done before the super loop function for(;;system_sleep()):
/* Initialize WINC1500 Wi-Fi driver with data and status callbacks. */
    param.pfAppWifiCb = wifi_cb;
    ret = m2m_wifi_init(&param);
    if (M2M_SUCCESS != ret) {
        DEBUG(DEBUG_CONF_WIFI "m2m_wifi_init call error!(%d)" DEBUG_EOL, ret);
        while (1) {
	}
    }	
    m2m_wifi_set_sleep_mode(M2M_PS_AUTOMATIC, 1);
	
    /* Initialize socket. */
    socketInit();
    registerSocketCallback(http_client_socket_event_handler, http_client_socket_resolve_handler);	
    /* Connect using stored SSID and Password. */
    m2m_wifi_default_connect();	
     ap_exosite_connection_state = MAIN_CHECKING_AP_INFORMATION;
    for(;;system_sleep()) 
    {		
        /* Handle pending events from network controller. */
        ret = m2m_wifi_handle_events(NULL);
The first step is to initialize the Exosite client module by calling function Exosite_example_init(). This function receives one parameter, which is a function pointer to the callback of the http module. The role of this function is to initialize the http_module and also initialize a timer_module for it. Add the following code just before the for(;;system_sleep()) inside the main() function.
/* Initialize Exosite. */
exosite_example_init(main_http_client_callback);
main_http_client_callback() will be registered by Exosite_example_init() and will receive and process all socket events. This function can be located in the main.c file or in a separate file as shown below:
/** brief Callback to get the Data from socket.*/
    static void main_http_client_callback(struct http_client_module *module_list, int type,
                                                union http_client_data *data)
    {
        Switch(type)    
        {
            case HTTP_CLIENT_CALLBACK_SOCK_CONNECTED: break;
            case HTTP_CLIENT_CALLBACK_REQUESTED: break;
            case HTTP_CLIENT_CALLBACK_RECV_RESPONSE: break;
            case HTTP_CLIENT_CALLBACK_DISCONNECTED: break;
        }
    }
Register the callback function for the sockets by calling the following function in main():
/* Initialize socket. */
socketInit();
registerSocketCallback(http_client_socket_event_handler, http_client_socket_resolve_handler);
The above function receives two parameters. The first one is a function pointer to deliver socket messages from the socket. The other one is a function pointer that is used for resolving DNS. The two parameters are located in http_client.h.
/**
 * \brief Event handler of socket event.
 *
 * \param[in]  sock            Socket descriptor.
 * \param[in]  msg_type        Event type.
 * \param[in]  msg_data        Structure of socket event.
 */
void http_client_socket_event_handler(SOCKET sock, uint8_t msg_type, void *msg_data);

/**
 * \brief Event handler of gethostbyname.
 *
 * \param[in]  domain_name     Domain name.
 * \param[in]  server_ip       Server IP.
 */
void http_client_socket_resolve_handler(uint8_t *domain_name, uint32_t server_ip);
In order to send the data from the temperature and light sensors on the IO1 to the Exosite cloud, the function exosite_example_read_and_write() must be used.
/* publish the temp measurements every interval */ 
if (tick_counter_check_timer())
{
    Char send_buf[100]; int dTemp = 0;
    int dLight = 0;

    /* prepare to sensor data in the I/o1 Board */ io1_board_prepare_to_get_info();
    dTemp = io1_board_get_temperature(); dLight = io1_board_get_lightvalue();
    sprintſsend_buf,”degree=%d&voltage=%d”, ſintƀdTemp, ſintƀdLightƀ;
    if( exosite_example_read_and_write(send_buf, (char*)p_board_info->cik))
    ...
}

The function sends the data to the cloud continuously at regular intervals.

In order to receive the response messages from Exosite, the main_http_client_callback() function must be used.

/** brief Callback to get the Data from socket. */
static void main_http_client_callback(struct http_client_module *module_list,
                                        int type, union http_client_data *data)
{
    case HTTP_CLIENT_CALLBACK_RECV_RESPONSE:
    parsing_http_response_data( data->recv_response.response_code,
    data->recv_response.content,
    data->recv_response.content_length);
    break;
}
Then you can add the relevant code to process the received messages without the parsing_http_response_data() function.
Note: This function is required to receive the CIK from Exosite.
Figure 5-1. Exosite Application Flow Chart