Enter Your Electronics & Design Project for a chance to win up to a $200 Shopping Cart of Product! | Project14 Home | |
Monthly Themes | ||
Monthly Theme Poll |
During the life time of any IoT device and especially which is located at remote place, sometime you need to simply restart the device. In conventional MQTT broker there is no such option of remote resetting the device or managed device option. So you have to create everything on your own like create a new topic subscribe to it the then decide about the reset process and many more things. Then you need broker side code to inform you if it restarted or not. For a single device yes you can do it but if you have hundreds of remote devices then this will definitely become a daunting task.
IBM bluemix cloud has everything needed to do this task by just choosing and clicking few options. All the broker side coding is already implemented we only need to write the device side code. The documentation about this process is available HERE. We have already successfully connected our device with IBM IoT platform over secured connection and all the necessary setting are also implemented and tested. (Micro Monster - Succulent Plants Monitoring System (part-2) Establishing A Secure Connection )
Now we will write the code to handle the remote reset (device restart) request received from IBM IoT platform.
WICED Studio Code:
It provide only RTOS based solution and if we want to work with this tool then little information about RTOS is required. (wiced WiFi - video tutorial series) In our code we only need to know about two main threads; one for main loop and one to handle MQTT events.
Code to handle MQTT events is given below,
static wiced_result_t mqtt_connection_event_cb( wiced_mqtt_object_t mqtt_object, wiced_mqtt_event_info_t *event ) { char *mqtt_msg; char *mqtt_topic; switch ( event->type ) { case WICED_MQTT_EVENT_TYPE_CONNECT_REQ_STATUS: case WICED_MQTT_EVENT_TYPE_DISCONNECTED: case WICED_MQTT_EVENT_TYPE_PUBLISHED: case WICED_MQTT_EVENT_TYPE_SUBCRIBED: case WICED_MQTT_EVENT_TYPE_UNSUBSCRIBED: { expected_event = event->type; wiced_rtos_set_semaphore( &semaphore ); } break; case WICED_MQTT_EVENT_TYPE_PUBLISH_MSG_RECEIVED: { wiced_mqtt_topic_msg_t msg = event->data.pub_recvd; WPRINT_APP_INFO(( "[MQTT] Received %.*s for TOPIC : %.*s\n\n", (int) msg.data_len, msg.data, (int) msg.topic_len, msg.topic )); mqtt_msg = (char *) malloc((int) msg.data_len +1); mqtt_topic = (char *) malloc((int) msg.topic_len+1); memset(mqtt_msg, 0, (int) msg.data_len +1); memset(mqtt_topic, 0, (int) msg.topic_len+1); sprintf(mqtt_msg, "%.*s", (int) msg.data_len, msg.data ); sprintf(mqtt_topic, "%.*s", (int) msg.topic_len,msg.topic); mqtt_request_size = msg.data_len; mqtt_cmd_handler(mqtt_topic, mqtt_msg); free(mqtt_msg); free(mqtt_topic); } break; default: break; } return WICED_SUCCESS; }
In this code we call a mqtt_cmd_handler function and set some variable depending upon the received command.
static uint8_t mqtt_cmd_handler(char *mqtt_topic, char * mqtt_data){ uint8_t ret=0; if(mqtt_topic){ if(!strcmp(mqtt_topic, WICED_REBOOT_TOPIC)){ update_reqId(mqtt_data); reboot_request=true; ret=1; } } return ret; }
Now we will not immediately start processing the received command instead we will wait for the completion of ongoing sensor data handling and publishing process. At the end of our main loop we will check if our reboot_request variable is set or not.
if (reboot_request){ reboot_request=false; sys_reboot_handler(REBOOT_INITIATED); if (reboot_request) break; }
If according to IBM IoT platform documentation (HERE) whenever a managed device received a restart it will reply to that request with certain return code and earlier received 40 characters unique id. The sys_reboot_handler function will send a reply to cloud with REBOOT_INITIATED return code along with reqId.
static void sys_reboot_handler(int rc){ char response[100]; char msg[100] ; uint8_t retries = 0; wiced_result_t ret = WICED_SUCCESS; wiced_rtos_delay_milliseconds( WICED_MQTT_DELAY_IN_MILLISECONDS * 5 ); generateMessageForReturnCode(rc,msg); sprintf(response, "{\"rc\":%d,\"message\":\"%s\",\"reqId\":\"%s\"}",rc,msg,currentRequestID); WPRINT_APP_INFO(("[MQTT] Publishing reboot request response...\n %.*s \n\n", strlen(response), (uint8_t*)response)); RUN_COMMAND_PRINT_STATUS_AND_BREAK_ON_ERROR( mqtt_app_publish( mqtt_object, WICED_MQTT_QOS_DELIVER_AT_LEAST_ONCE, WICED_RESPONSE_TOPIC, (uint8_t*)response ,strlen(response) ), NULL, NULL ); if (ret==WICED_SUCCESS){ reboot_request=true; } }
If we will be able to reply back to reboot successfully then will break out of our main loop and perform the software system reboot. We can directly call the reboot function but it is better to first unsubscribe all the topics, disconnect from the MQTT server and also free the RTOS resources and then initiate the system reboot by calling the following code.
if(reboot_request){ printf("Calling wiced_framework_reboot();"); wiced_framework_reboot(); }
Now we are ready to build and program our device to test and verify the functionality of our code.
Testing and Operational Verification:
PART 1: Micro Monster - Succulent Plants Monitoring System
PART 2: Establishing and Testing the Secure Connection
PART 3: Remote Device Restart, Factory Reset and Data storage on External Flash
PART 4: Remote Firmware Download and Update
PART 5: Sensors Interfacing
PART 6: Final Testing and Demo