Further Development of the Application

The following section will describe how the application can be further extended to also send temperature data to the Android application. The Android application is already set up for this, and it can be enabled from the app. This is done by pressing the three dots in the top right corner of the main screen, selecting settings and checking the "Enable temperature reading" checkbox as shown in the figure below.

Figure 1. Enable Temperature Reading in Android Application

This will add another button to each item in the list, which can be clicked to request temperature data from the Xplained Mini board. When this button is pressed, the Android application will send a TCP packet to the ATWINC1500 with the text "temp". The handling of this message needs to be implemented in the WINC_mega.c file in the Atmel Studio project. The Atmel Studio project, "WINC_mega6_2.atsln", can be found in the "Studio Project\WINC_mega" folder in the .zip file that comes with the application note.

The first thing that should be done is to add two functions to WINC_mega.c; one for initializing the ADC and one for doing an ADC conversion to read the internal temperature sensor of the megaAVR.

static void init_temp(void)
{
    sysclk_enable_module(POWER_RED_REG0, PRADC_bm);	
    /* Init ADC to read from internal temperature sensor */
    ADMUX =  (1 << MUX3) | (1 << REFS0) | (1 << REFS1);
    /* Enable ADC */
    ADCSRA = 1 << ADEN | 1 << ADPS0 | 1 << ADPS1 | 1 << ADPS2;
}
static void read_temp(uint8_t *buffer)
{
    /* Start conversion */
    ADCSRA |= 1 << ADSC;

    /* Wait for conversion to be done */
    while(!(ADCSRA & (1 << ADIF)));
    /* Write conversion result to buffer */
    ((uint16_t*)buffer)[0] = ADCW;
}
We will also need a variable to signal to the main loop whether a request to send the temperature has been received from the Android application. This variable should be global and placed outside the main function:
/** Variable for main loop to see if a temperature read request is received */
volatile bool temperature = false;

At the start of the main function, a buffer should be declared. This will be used to store the temperature that should be sent to the Android application when requested.

/** Buffer to store temperature */
uint8_t temp_buf[2];

In the socket callback function m2m_tcp_socket_handler(), we will add the logic to handle the new message from the Android application. As previously stated, the message sent is "temp", so we will need to check if this string is present in the received message. If it is, we will set the global variable to true, so that it can be handled in the main loop. Find the SOCKET_MSG_RECV case in the switch case and replace it with the following code:

/* Message receive */
case SOCKET_MSG_RECV:
    /* Check command */
    if (strstr(gau8SocketTestBuffer, "toggle")) {
        toggle = true;
    } else if (strstr(gau8SocketTestBuffer, "temp")) {
        temperature = true;
    }
    break;

After doing this, we will need to call the ADC initialization function from the main function, this must be done before the beginning of the while(1) loop.

init_temp();

The last thing to do is to add the temperature reading and sending it back to the Android application when it has been requested. The following code should be placed inside the while(1) loop, inside the if (wifi_connected == M2M_WIFI_CONNECTED) statement, right after the similar code for the LED toggling:

/* Temperature command received  */			
if (temperature) {
    temperature = false;
    read_temp(temp_buf);
    /* Sending temperature reading back */
    send(tcp_client_socket, (void*)temp_buf, sizeof(temp_buf), 0);
}

The value sent from the AVR to the Android application will be the raw value from the ADC sampling the temperature sensor. This will thus not be temperature in degrees, but when heating or cooling the AVR, there will be a difference in the readings. For more information about the temperature sensor, refer to the device datasheet.

A WINC_mega.c file with all these implementations added can be found in the !Studio Project\temperature" folder in the .zip file, and can be used as a reference.