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"
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.
for(;;system_sleep())
:/* Initialize WINC1500 Wi-Fi driver with data and status callbacks. */
param.pfAppWifiCb = wifi_cb;
ret = m2m_wifi_init(¶m);
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);
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;
}
}
main()
:/* Initialize socket. */
socketInit();
registerSocketCallback(http_client_socket_event_handler, http_client_socket_resolve_handler);
/**
* \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);
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;
}
parsing_http_response_data()
function.