Esempio n. 1
0
void application_start( )
{
    /* Initialise the WICED device */
    wiced_init();

    while ( 1 )
    {
        WPRINT_APP_INFO( ( "Analog input voltage measurement:\n" ) );
		
        for(uint8_t i=0; i<8; i++)
        {
            wiced_adc_init( analog_channel[i], 480 );
            // This function only takes sample for the channel that selected by the last time invoking wiced_adc_init().
            // So if you change to take sample for another channel, you need to invoke wiced_adc_init() to select this channel first.
            wiced_adc_take_sample( analog_channel[i], &analog_value[i] );
            WPRINT_APP_INFO( ( "Channel %d input voltage: %d\n", i, analog_value[i] ) );
    	}

        wiced_rtos_delay_milliseconds( 3000 );
        WPRINT_APP_INFO( ( "\n\n" ) );
    }
}
Esempio n. 2
0
wiced_result_t thermistor_take_sample(wiced_adc_t adc, uint16_t* sample_value)
{
    wiced_result_t result = wiced_adc_take_sample(adc, sample_value);

    /* Thermistor is Murata NCP18XH103J03RB  (Digi-key 490-2436-1-ND )
     *
     * Part Number details:
     * NC  : NTC Chip Thermistor
     * P   : Plated termination
     * 18  : Size 0603
     * XH  : Temperature Characteristics : Nominal B-Constant 3350-3399K
     * 103 : Resistance 10k
     * J   : Tolerance   +/- 5%
     * 03  : Individual Specs: Standard
     * RB  : Paper Tape 4mm pitch, 4000pcs
     *
     *
     * It has a 43K feed resistor from 3V3
     *
     * Thermistor Voltage    = V_supply * ADC_value / 4096
     * Thermistor Resistance = R_feed / ( ( V_supply / V_thermistor ) - 1 )
     * Temp in kelvin = 1 / ( ( ln( R_thermistor / R_0 ) / B ) + 1 / T_0 )
     * Where: R_feed = 43k, V_supply = 3V3, R_0 = 10k, B = 3375, T_0 = 298.15°K (25°C)
     */
    if (result == WICED_SUCCESS)
    {
        double thermistor_resistance = 43000.0 / ( ( 4096.0 / (double) *sample_value ) - 1 );
        double logval = log( thermistor_resistance / 10000.0 );
        double temperature = 1.0 / ( logval / 3380.0 + 1.0 / 298.15 ) - 273.15;

        *sample_value = (uint16_t)(temperature*10);
        return WICED_SUCCESS;
    }
    else
    {
        *sample_value = 0;
        return result;
    }
}
/*
 * Main application
 */
void application_start( void )
{
    char            *msg = MSG_OFF;
    wiced_result_t   ret = WICED_SUCCESS;
    int              retries;
    uint16_t         light_value;

    ret = aws_app_init(&app_info);

    /* Initialise Light sensor */
    WPRINT_APP_INFO( ("Initializing Light Sensor\n" ));
    wiced_adc_init( WICED_LIGHT, 5 );

    do
    {
        ret = aws_mqtt_conn_open( app_info.mqtt_object, mqtt_connection_event_cb );
        if ( ret != WICED_SUCCESS )
        {
            WPRINT_APP_INFO(("Failed\n"));
            break;
        }

        while ( 1 )
        {
            /* Read light sensor */
            wiced_adc_take_sample( WICED_LIGHT, &light_value );
            WPRINT_APP_INFO( ("Light value %u\n", light_value) );

            if ( light_value >= LIGHT_THRESHOLD )
            {
                msg = MSG_ON;
            }
            else
            {
                msg = MSG_OFF;
            }

            /* Controlling the LED by publishing to mqtt topic "WICED_BULB" */
            retries = 0;
            do
            {
                ret = aws_mqtt_app_publish( app_info.mqtt_object, WICED_MQTT_QOS_DELIVER_AT_LEAST_ONCE, (uint8_t*) app_info.thing_name, (uint8_t*) msg, strlen( msg ) );
                retries++ ;
            } while ( ( ret != WICED_SUCCESS ) && ( retries < MQTT_PUBLISH_RETRY_COUNT ) );
            if ( ret != WICED_SUCCESS )
            {
                break;
            }

            wiced_rtos_delay_milliseconds( 5000 );
        }

        aws_mqtt_conn_close( app_info.mqtt_object );

        wiced_rtos_delay_milliseconds( MQTT_DELAY_IN_MILLISECONDS * 2 );
    } while ( 1 );

    aws_mqtt_conn_close( app_info.mqtt_object );

    wiced_rtos_deinit_semaphore( &app_info.msg_semaphore );
    WPRINT_APP_INFO(("[MQTT] Deinit connection...\n"));
    ret = wiced_mqtt_deinit( app_info.mqtt_object );
    free( app_info.mqtt_object );
    app_info.mqtt_object = NULL;

    return;
}