void application_start( ) { uint8_t led_state = 0; wiced_bool_t button_pressed; /* Initialise the WICED device */ wiced_init(); // The RGB and setup button are initialized in platform.c WPRINT_APP_INFO( ( "The RGB are flashing R-G-B alternately. Holding the setup button will force it flashing white.\n" ) ); while ( 1 ) { /* Read the state of setup button */ button_pressed = wiced_gpio_input_get( SETUP_BUTTON ) ? WICED_FALSE : WICED_TRUE; /* The button has inverse logic */ if ( button_pressed == WICED_TRUE ) { /* Flashing white */ if( (led_state%2) == 0 ) { wiced_gpio_output_low( RGB_R ); wiced_gpio_output_low( RGB_G ); wiced_gpio_output_low( RGB_B ); } else { wiced_gpio_output_high( RGB_R ); wiced_gpio_output_high( RGB_G ); wiced_gpio_output_high( RGB_B ); } } else { /* Flashing R-G-B alternately */ if ( (led_state%3) == 0 ) { wiced_gpio_output_low( RGB_R ); wiced_gpio_output_high( RGB_G ); wiced_gpio_output_high( RGB_B ); } else if ( (led_state%3) == 1 ) { wiced_gpio_output_high( RGB_R ); wiced_gpio_output_low( RGB_G ); wiced_gpio_output_high( RGB_B ); } else { wiced_gpio_output_high( RGB_R ); wiced_gpio_output_high( RGB_G ); wiced_gpio_output_low( RGB_B ); } } wiced_rtos_delay_milliseconds( 300 ); led_state++; } }
// Main application thread which is started by the RTOS after boot void application_start(void) { wiced_init( ); dbStart(); WPRINT_APP_INFO(("Starting WWEP Server\n")); while(wiced_network_up( INTERFACE, DHCP_MODE, &ip_settings ) != WICED_SUCCESS); // Keep trying until you get hooked up // I created all of the server code in a separate thread to make it easier to put the server // and client together in one application. wiced_rtos_create_thread(&tcp_thread, TCP_SERVER_NONSECURE_THREAD_PRIORITY, "Server TCP Server", tcp_server_nonsecure_thread_main, TCP_SERVER_NONSECURE_STACK_SIZE, 0); wiced_rtos_create_thread(&ping_thread, PING_THREAD_PRIORITY, "Ping", pingAP, 1024, 0); // Setup Display WPRINT_APP_INFO(("#\t IP\t\tPort\tMessage\n")); WPRINT_APP_INFO(("----------------------------------------------------------------------\n")); // just blink the led while the whole thing is running while(1) { wiced_gpio_output_low( WICED_LED1 ); wiced_rtos_delay_milliseconds( 250 ); wiced_gpio_output_high( WICED_LED1 ); wiced_rtos_delay_milliseconds( 250 ); } }
/****************************************************** * Function Definitions ******************************************************/ wiced_result_t aws_app_init( aws_app_info_t *app_info ) { wiced_result_t ret = WICED_SUCCESS; aws_config_dct_t *aws_app_dct = NULL; aws_app_info = app_info; wiced_init( ); /* Disable roaming to other access points */ wiced_wifi_set_roam_trigger( -99 ); /* -99dBm ie. extremely low signal level */ WPRINT_APP_INFO((" Please wait, connecting to network...\n")); WPRINT_APP_INFO(("(To return to SSID console screen, hold USER switch for 5 seconds during RESET to clear DCT configuration)\n")); wiced_rtos_delay_milliseconds( 1000 ); for ( int i = 0; i < 25; i++ ) { wiced_gpio_output_high( WICED_LED2 ); wiced_rtos_delay_milliseconds( 100 ); wiced_gpio_output_low( WICED_LED2 ); wiced_rtos_delay_milliseconds( 100 ); if ( !wiced_gpio_input_get( WICED_BUTTON1 ) ) { wiced_rtos_delay_milliseconds( 5000 ); if ( !wiced_gpio_input_get( WICED_BUTTON1 ) ) { aws_config_dct_t aws_dct = { .is_configured = WICED_FALSE, .thing_name = AWS_DEFAULT_THING_NAME }; wiced_gpio_output_high( WICED_LED1 ); WPRINT_APP_INFO(( "DCT clearing start\n" )); wiced_dct_write( &aws_dct, DCT_APP_SECTION, 0, sizeof( aws_config_dct_t ) ); wiced_rtos_delay_milliseconds( 1000 ); wiced_gpio_output_low( WICED_LED1 ); WPRINT_APP_INFO(( "DCT clearing end\n" )); break; } }
/* Define the thread function that will blink the LED on/off every 500ms */ void ledThread(wiced_thread_arg_t arg) { while(1) { /* LED OFF for the shield (LED ON if using the baseboard by itself) */ wiced_gpio_output_low( WICED_LED1 ); wiced_rtos_delay_milliseconds( 250 ); /* LED ON for the shield (LED OFF if using the baseboard by itself) */ wiced_gpio_output_high( WICED_LED1 ); wiced_rtos_delay_milliseconds( 250 ); } }
static wiced_result_t led2_write_state( spi_slave_t* device, uint8_t* data_start ) { wiced_bool_t new_state = (wiced_bool_t)*data_start; led2_state = new_state; if ( new_state == WICED_TRUE ) { wiced_gpio_output_high( WICED_LED2 ); } else { wiced_gpio_output_low( WICED_LED2 ); } return WICED_SUCCESS; }
void messageArrived( MessageData* md ) { MQTTMessage* message = md->message; WPRINT_APP_INFO ( ("Message Subscribed %d \r\n", message->payloadlen));WPRINT_APP_INFO ( ("Message dup %x \r\n", message->dup));WPRINT_APP_INFO ( ("Message id %x \r\n", message->id));WPRINT_APP_INFO ( ("Message qos %x \r\n", message->qos));WPRINT_APP_INFO ( ("Message retained %x \r\n", message->retained)); WPRINT_APP_INFO( ( "Topic Name ==> %.*s\t", md->topicName->lenstring.len, md->topicName->lenstring.data ) );WPRINT_APP_INFO( ( "Message ==> %.*s\n", (int)message->payloadlen, (char*)message->payload ) ); if ( strstr( (char*) message->payload, "ON" ) ) { printf( "Turn ON LED\n" ); wiced_gpio_output_high( WICED_LED1 ); } else { if ( strstr( (char*) message->payload, "OFF" ) ) { printf( "Turn OFF LED\n" ); wiced_gpio_output_low( WICED_LED1 ); } } }
/* Main application */ void application_start( ) { char receiveChar; uint32_t expected_data_size = 1; wiced_init(); /* Initialize the WICED device */ /* Configure and start the UART. */ /* Note that WICED_DISABLE_STDIO must be defined in the make file for this to work */ #define RX_BUFFER_SIZE (5) wiced_ring_buffer_t rx_buffer; uint8_t rx_data[RX_BUFFER_SIZE]; ring_buffer_init(&rx_buffer, rx_data, RX_BUFFER_SIZE ); /* Initialize ring buffer to hold receive data */ wiced_uart_config_t uart_config = { .baud_rate = 9600, .data_width = DATA_WIDTH_8BIT, .parity = NO_PARITY, .stop_bits = STOP_BITS_1, .flow_control = FLOW_CONTROL_DISABLED, }; wiced_uart_init( STDIO_UART, &uart_config, &rx_buffer); /* Setup UART */ while ( 1 ) { if ( wiced_uart_receive_bytes( STDIO_UART, &receiveChar, &expected_data_size, WICED_NEVER_TIMEOUT ) == WICED_SUCCESS ) { /* If we get here then a character has been received */ if(receiveChar == '0') /* LED OFF for the shield (LED ON if using the baseboard by itself) */ { wiced_gpio_output_low( WICED_LED1 ); } if(receiveChar == '1') /* LED ON for the shield (LED OFF if using the baseboard by itself) */ { wiced_gpio_output_high( WICED_LED1 ); } } } }
/****************************************************** * Function Definitions ******************************************************/ void application_start( void ) { wiced_result_t ret = WICED_SUCCESS; uint32_t connection_retries = 0; uint32_t retries = 0; ret = aws_app_init( &app_info ); wiced_gpio_input_irq_enable( WICED_BUTTON1, IRQ_TRIGGER_RISING_EDGE, publish_callback, NULL ); do { ret = aws_mqtt_conn_open( app_info.mqtt_object, mqtt_connection_event_cb ); connection_retries++ ; } while ( ( ret != WICED_SUCCESS ) && ( connection_retries < WICED_MQTT_CONNECTION_NUMBER_OF_RETRIES ) ); aws_mqtt_app_publish( app_info.mqtt_object, WICED_MQTT_QOS_DELIVER_AT_LEAST_ONCE, (uint8_t*) app_info.shadow_state_topic, (uint8_t*) SHADOW_PUBLISH_MESSAGE_STR_OFF_DESIRED_AND_REPORTED, sizeof( SHADOW_PUBLISH_MESSAGE_STR_OFF_DESIRED_AND_REPORTED ) ); wiced_rtos_delay_milliseconds( MQTT_DELAY_IN_MILLISECONDS * 2 ); aws_mqtt_app_subscribe( app_info.mqtt_object, app_info.shadow_delta_topic, WICED_MQTT_QOS_DELIVER_AT_LEAST_ONCE ); do { ret = aws_mqtt_app_subscribe( app_info.mqtt_object, app_info.thing_name, WICED_MQTT_QOS_DELIVER_AT_MOST_ONCE ); retries++ ; } while ( ( ret != WICED_SUCCESS ) && ( retries < WICED_MQTT_SUBSCRIBE_RETRY_COUNT ) ); if ( ret != WICED_SUCCESS ) { return; } while ( 1 ) { /* Wait forever on wake semaphore until the LED status is changed */ wiced_rtos_get_semaphore( &app_info.wake_semaphore, WICED_NEVER_TIMEOUT ); /* Toggle the LED */ if ( ( strncasecmp( led_status, "OFF", 3 ) == 0 ) && smart_control == 1 ) { wiced_gpio_output_low( WICED_LED1 ); led_status = "OFF"; WPRINT_APP_INFO(("[MQTT] Publishing to Thing state topic\n")); aws_mqtt_app_publish( app_info.mqtt_object, WICED_MQTT_QOS_DELIVER_AT_LEAST_ONCE, (uint8_t*) app_info.shadow_state_topic, (uint8_t*) SHADOW_PUBLISH_MESSAGE_STR_OFF_DESIRED_AND_REPORTED_STATUS_ON, sizeof( SHADOW_PUBLISH_MESSAGE_STR_OFF_DESIRED_AND_REPORTED_STATUS_ON ) ); } else if ( ( strncasecmp( led_status, "ON", 2 ) == 0 ) && smart_control == 0 ) { wiced_gpio_output_high( WICED_LED1 ); led_status = "ON"; WPRINT_APP_INFO(("[MQTT] Publishing to Thing state topic\n")); aws_mqtt_app_publish( app_info.mqtt_object, WICED_MQTT_QOS_DELIVER_AT_LEAST_ONCE, (uint8_t*) app_info.shadow_state_topic, (uint8_t*) SHADOW_PUBLISH_MESSAGE_STR_ON_DESIRED_AND_REPORTED_STATUS_OFF, sizeof( SHADOW_PUBLISH_MESSAGE_STR_ON_DESIRED_AND_REPORTED_STATUS_OFF ) ); } else if ( ( strncasecmp( led_status, "ON", 2 ) == 0 ) && smart_control == 1 ) { wiced_gpio_output_high( WICED_LED1 ); led_status = "ON"; WPRINT_APP_INFO(("[MQTT] Publishing to Thing state topic\n")); aws_mqtt_app_publish( app_info.mqtt_object, WICED_MQTT_QOS_DELIVER_AT_LEAST_ONCE, (uint8_t*) app_info.shadow_state_topic, (uint8_t*) SHADOW_PUBLISH_MESSAGE_STR_ON_DESIRED_AND_REPORTED, sizeof( SHADOW_PUBLISH_MESSAGE_STR_ON_DESIRED_AND_REPORTED ) ); } else { wiced_gpio_output_low( WICED_LED1 ); led_status = "OFF"; WPRINT_APP_INFO(("[MQTT] Publishing to Thing state topic\n")); aws_mqtt_app_publish( app_info.mqtt_object, WICED_MQTT_QOS_DELIVER_AT_LEAST_ONCE, (uint8_t*) app_info.shadow_state_topic, (uint8_t*) SHADOW_PUBLISH_MESSAGE_STR_OFF_DESIRED_AND_REPORTED, sizeof( SHADOW_PUBLISH_MESSAGE_STR_OFF_DESIRED_AND_REPORTED ) ); } } aws_mqtt_conn_close( app_info.mqtt_object ); wiced_rtos_deinit_semaphore( &app_info.msg_semaphore ); ret = wiced_mqtt_deinit( app_info.mqtt_object ); wiced_rtos_deinit_semaphore( &app_info.wake_semaphore ); free( app_info.mqtt_object ); app_info.mqtt_object = NULL; return; }