element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Remote Monitoring & Control
  • Challenges & Projects
  • Project14
  • Remote Monitoring & Control
  • More
  • Cancel
Remote Monitoring & Control
Blog PSoC®︎ 6 WiFi-BT Pioneer Kit (CY8CKIT-062-WiFi-BT) - Remote Reset Over Secured MQTT Connection
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Remote Monitoring & Control to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: mahmood.hassan
  • Date Created: 14 Aug 2019 10:07 AM Date Created
  • Views 663 views
  • Likes 4 likes
  • Comments 0 comments
  • iot security
  • internet of things
  • cypress psoc6 wi-fi bt pioneer kit
  • ibm iot
  • cypress
  • psoc
Related
Recommended

PSoC®︎ 6 WiFi-BT Pioneer Kit (CY8CKIT-062-WiFi-BT) - Remote Reset Over Secured MQTT Connection

mahmood.hassan
mahmood.hassan
14 Aug 2019
image

Remote Monitoring & Control

Enter Your Electronics & Design Project for a chance to win up to a $200 Shopping Cart of Product!

Back to The Project14 homepage image

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:

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

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

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube