Previous Parts: Micro Monster - Succulent Plants Monitoring System (part-1)
Next Parts: coming soon
Introduction:
For remote monitoring and controlling of any device required internet access, MQTT broker, storage for remote & local data logging and management system. IBM watson fulfill all these requirements and it will be used in this project.
To use the wireless capabilities of PSoC6 kit we will use WICED Studio (TUTORIALS). This IDE has builtin all the required platform related files and WiFi SDK is also already configured and ready to use with similar to ESP8266/ESP32 functions but far more capabilities. We will use secure MQTT communication protocol v3 with TLS and token authentication with QoS level 1 (because this is requirement of IBM bluemix for manged device communication) to ensure at least one time delivery of MQTT data.
Wiced Studio Code:
Important topics names required to successfully monitor, control and remote management of the device.
/* Name of publish topic */ #define WICED_PUBLISH_TOPIC "iot-2/evt/status/fmt/json" /* Name of subscribe topic */ #define WICED_SUBSCRIBE_TOPIC "iot-2/cmd/update/fmt/json" /* Name of managed device topic */ #define WICED_MANAGEDEVICE_TOPIC "iotdm-1/#" /* Name of DM requests topic */ #define WICED_DMRESPONSE_TOPIC "iotdm-1/response" #define WICED_DMOBSERVE_TOPIC "iotdm-1/observe" #define WICED_DMCANCEL_TOPIC "iotdm-1/cancel" /* Name of response topic */ #define WICED_RESPONSE_TOPIC "iotdevice-1/response" #define WICED_NOTIFY_TOPIC "iotdevice-1/notify" /* Name of management topic */ #define WICED_MANAGE_TOPIC "iotdevice-1/mgmt/manage" #define WICED_UUNMANAGE_TOPIC "iotdevice-1/mgmt/unmanage" /* Name of device management topic */ #define WICED_UPDATE_LOCATION_TOPIC "iotdevice-1/device/update/location" #define WICED_CREATE_ERRCODES_TOPIC "iotdevice-1/add/diag/errorCodes" #define WICED_CLEAR_ERRCODES_TOPIC "iotdevice-1/clear/diag/errorCodes" #define WICED_ADD_LOG_TOPIC "iotdevice-1/add/diag/log" #define WICED_CLEAR_LOG_TOPIC "iotdevice-1/clear/diag/log" /* Name of firmware actions topic */ #define WICED_FWACTIONS_TOPIC "iotdm-1/device/update" #define WICED_FWUPDATE_TOPIC "iotdm-1/mgmt/initiate/firmware/update" #define WICED_FWDOWNLOAD_TOPIC "iotdm-1/mgmt/initiate/firmware/download" /* Name of device actions topic */ #define WICED_REBOOT_TOPIC "iotdm-1/mgmt/initiate/device/reboot" #define WICED_FACTORYRESET_TOPIC "iotdm-1/mgmt/initiate/device/factory_reset"
MQTT client configuration required to connect with IBM cloud and data packet required to enable the remote management of the device.
/* MQTT client Details */ /* MQTT client organization */ #define ORG "xxxxxx" // organization ID /* MQTT client device type */ #define DEVICE_TYPE "PSoC6WiFiBTpioneerKIT" // Device type /* MQTT client device ID */ #define DEVICE_ID "cy8ckit_062_wifi_bt" // Device ID /* MQTT client authentication token */ #define AUTHENTICATION_TOKEN "xxxxxxxxxxxxxxxxxx" // Self or Auto generated authentication token /* MQTT client authentication method */ #define AUTHENTICATION_METHOD "use-token-auth" /* Broker domain address */ #define MQTT_BROKER_ADDRESS ORG ".messaging.internetofthings.ibmcloud.com" /* MQTT client ID */ #define CLIENT_ID "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID /* Data packet to enable remote device management (Restart, Factory Reset or Firmware update) */ #define WICED_MANNAGE_STR "{\"d\":{\"supports\":{\"deviceActions\":true,\"firmwareActions\":true}},\"reqId\":\"REPLACE WITH 40 RANDOM CHARACTERS STRING\"}"
With the above configuration we can do everything but over an insecure MQTT connection. In order to connect our device securely over TLS with IBM cloud we need to authenticate the device using either the server or client side CA certificate.
In this project I have used TLS authentication with digitally signed ibm-bluemix CA certificate. I have used the digitally signed CA certificate "comodo_bluemix.pem" of IBM cloud available at "..\STM32CubeExpansion_Cloud_WATSON_V1.0.0\Projects\Common\Bluemix" path in X-CUBE-WATSON (STM32 cube expansion package).
You can either copy it inside WICED-Studio resource folder and let the wiced studio to convertor its content into data arrays or you can open this file in any text editor and create a new array using all the data given in this file including BEGIN and END CERTIFICATE comments as shown below.
Automatically generated .c file in WICED Studio using comodo_bluemix.pem CA certificate file available in X-CUBE-WATSON STM32 cube expansion package.
For auto generated .c file for CA certificates use the following code.
static wiced_mqtt_security_t security; uint32_t size_out; /* Read root CA certificate (self/digitally certified) */ resource_get_readonly_buffer( &resources_apps_DIR_secure_mqtt_DIR_comodo_bluemix_pem, 0, MQTT_MAX_RESOURCE_SIZE, &size_out, (const void **) &security.ca_cert ); security.ca_cert_len = size_out;
Here the variable resources_apps_DIR_secure_mqtt_DIR_comodo_bluemix_pem represent the location of comodo_bluemix.pem file inside the 43xxx_Wi-Fi folder of WICED Studio (..\WICED-Studio-6.2\43xxx_Wi-Fi\resources\apps\secure_mqtt\comodo_bluemix.pem).
If you have placed this file in any other folder inside the WICED Studio then rename this variable accordingly. Always free the resources after using the TLS authentication CA certificate using the code line given below.
resource_free_readonly_buffer( &resources_apps_DIR_secure_mqtt_DIR_comodo_bluemix_pem, security.ca_cert );
For remote management of any device connected with IBM cloud, first device need to specifically tell the cloud about its remote management capabilities by publishing the "WICED_MANNAGE_STR" at "WICED_MANAGE_TOPIC". After successfully publishing the device remote management capabilities at IBM cloud server the device will appear under "Device Management Actions" below.
To check if remote device management go to =>Devices=>Action=> Initiate Action=> select action=>select device=>Add devices=>YOUR MANAGED DEVICE
For more detail about remote device management visit MQTT connectivity for devices and Device management requests.
After successful adding the remote device management capabilities we need to subscribe to the following two topics to receive management and command requests data.
WICED_SUBSCRIBE_TOPIC WICED_MANAGEDEVICE_TOPIC
After subscribing to these two topis we can transmit our data to IBM cloud at following topic.
WICED_PUBLISH_TOPIC
These are the basic settings and configuration required to successfully communicate and enabling the remote device management using IBM cloud.
All the details regarding "How to handle device commands and management requests" will be explained in next posts.
IBM Cloud configuration:
After creating a free account on IBM cloud search for node-red in search box (Important: here dash between node and red is important otherwise it will not show the result as shown below)
Create Internet of things platform starter using IBM cloud.
We can also choose internet of things options from Categories but it will require several additional configuration to enable real time data and event storage with remote device control. This option will automatically configure everything required for our project. Choose the appropriate name for the IBM cloud foundry app and create it as shown below.
It will take few minute to setup. After successful setup launch the IOT platform as shown below:
{gallery} Launch IBM Bluemix IOT Platform |
---|
IMAGE TITLE: Step 1- Launching IBM bluemix IOT platform |
IMAGE TITLE: Step 2- Launching IBM bluemix IOT platform |
IMAGE TITLE: Step 3- Launching IBM bluemix IOT platform |
After launching IBM bluemix IOT platform our first step will be Add Device. All the steps required to add a new device are shown and explained in detail in this How to register Device in IBM IOT? tutorial. Only follow the steps 3 and 4 even though its based on old version of IBM iot platform but it will not be difficult. Or you can also follow the IOT platform getting started tutorial for adding a new device.
After adding a new device the device credential shown in last step are very important. Kindly take a print screen or save these values somewhere safe because these contain the device authentication token and you can not view or change it after this step.
source: IBM How to register device with IOT platform tutorial
Newly added device will appear in landing page of Devices in IBM bluemix as shown below.
PSoC6 device is added successfully in IBM IOT platform.
If you don't have any type CA certificate and want to use insecure MQTT communication then go to security => Connection Security => change the default security level to TLS optional. But in this project we will be using default connection security level of TLS with token authentication as shown below.
Connection settings used in this project to ensure only secure access to our cloud network.
In the last step we will enable the storage database connected with IOT platform to record all the recevied data. To do this go to extensions window and configure the cloudentNOSQLDB externsion already connected with our IOT platform. the basic setting for this extension are given below. you can change these setting according to your requirements.
Cloudant NOSQL database settings used in this project to save all the received data automatically based on these settings.
Testing and Verification:
Now we will write our first testing code using above mentioned particulars and test the secure communication with IBM bluemix.
So far we have successfully established a secure mqtt connection with IBM IoT platform, enabled the remote device management, subscribing and receiving the device management commands from IBM cloud, displaying data in real time.
In the next part we will write code to handle remote restart of device and factory resetting request and then test it. We also add the feature of "Saving important parameters on external flash".
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