integrate iot devices into the smartthings ecosystem objective learn how to create your own iot device using smartthings sdk for direct connected devices overview the smartthings sdk for direct connected devices is provided to ease the development of devices which operate with smartthings cloud and mobile application it is small enough to work on resource limited embedded devices in this code lab, you will learn how to create your own iot device profile at smartthings cloud and how to implement on your test device it is explained into three parts the first explains how you can register and deploy to test your own iot device profile in smartthings using the developer workspace then, it demonstrates how to create your own iot device application with smartthings sdk the last part shows how to onboard and control instances of your device with smartthings mobile application noteyou will name the device as code lab example for this tutorial set up your environment you will need the following host pc this code lab is based on ubuntu linux x64 toolchain and board support package bsp for micro-controller unit mcu board you will use the esp32-c3-devkitc board you may set up the environment by referring to this github repository github repositories for the library and references or samples of smartthings sdk for direct connected devices for c $ git clone https //github com/smartthingscommunity/st-device-sdk-c-ref git $ cd st-cevice-sdk-c-ref $ python3 setup py esp32 smartthings mobile application sample code here is a sample code for you to start coding in this code lab download it and start your learning experience! smartthings sdk sample code 2 60 kb alternatively, you may simply get it by git cloning or downloading a zip file from this github repository register and deploy to test your device using the developer workspace, create a device profile and customize the instructions for onboarding the device on the smartthings mobile app, and deploy devices to be tested these configurations can be edited up until the point you submit the device for publication in the production of the smartthings ecosystem sign in to smartthings developers sign in to smartthings developer workspace using your samsung account create new project you need to create an integration project for a direct connected device create a new device project for device integration select device integration, then direct-connected afterwards, name your project respectively add a device profile define your device’s functionalities with smartthings capability first, navigate and select define device profile and then click create device profile fill the required fields with respective values afterwards, select a capability for your device you can get more information about the capabilities available here notethe health check capability is automatically added for all direct connected devices it should not be removed the required capabilities are switch and health check add device onboarding the device onboarding guides device owners when their device is first registering and connecting to smartthings you can customize the screens presented by adding a device onboarding profile the ownership validation type is also defined at this stage you may customize the screens displayed when the device onboards via the smartthings mobile app, or just use the default values noteuse just works or button confirm for this example add a product info the product info defines how this device is shown at smartthings mobile app catalog you can define device’s category and its regional availability deploy your device to test you can start testing by deploying your device to test there are two ways to do this via overview menu via test > test devices you will be able to see your device in the smartthings mobile app when in developer mode only after it has been deployed for testing register test devices you can add the identity of device for authenticating it to the smartthings cloud this requires device information like serial number and device public key ed25519 since the maximum number of test device is limited per user, once you’ve reached it, you should remove the existing one this example shows how to create ed25519 key pair with sdk tools you can get device_info json file as a result from tools/keygen/linux/output_{ serialnumber} linux version of key generator keygen utility is located at st-iot-device-sdk-c-reference/iot-core/tools/keygen/ or st-device-sdk-c/tools/keygen/ serial number for testing device would be randomly generated by this tool which has stdk + 12-digit alphanumeric format $ cd ~/workspace/st-device-sdk-c-ref/iot-core/tools/keygen/ $ python3 stdk-keygen py –firmware switch_example_001 use following serial number and public key for the identity of your device in developer workspace serial number stdk**e90w***ucx public key nfn5x***uqusq****zhobsfaaop9***kndlnjdjrew= copy stdk**e90w***ucx from keygen output and paste it into serial number field of register a test device page copy public key string from keygen output nfn5x***uqusq****zhobsfaaop9***kndlnjdjrew= in this example and paste it into public key field generate device qr code using the device qr code could be helpful while device onboarding qr code should have format like below refer here for more details urlhttps //qr samsungiots com/?m={your mnid}&s={device onboardingid}&r={device serialnumber} {your mnid} 4-digit alphanumeric mnid of your account {device onboardingid} 3-digit number onboarding id, you can find it from your developer workspace project device onboarding > other info page {device serialnumber} device serial number which is registered at your developer workspace project you can simply generate qr code with using below python script import qrcode mnid = 'ffff' # "ffff" is an example you should replace it with yours onboardingid = '111' # "111" is an example you should replace it with yours serialnumber = 'stdktest0001' # "stdktest0001" is an exmaple you should replace it with yours qrurl = 'https //qr samsungiots com/?m=' + mnid + '&s=' + onboardingid + '&r=' + serialnumber img = qrcode make qrurl img save serialnumber + ' png' qrcode qrcode box_size=10, border=4 write device application open and edit sample device app currently, you are doing the code lab example, which is located at apps/esp32/code_lab_example directory in the github repository download onboarding_profile json from the overview of your device in developer workspace copy onboarding_profile json file into your application directory, apps/esp32/code_lab_example/main $ cd ~/workspace/st-device-sdk-c-ref/apps/esp32/code_lab_example/main/ $ cp ~/downloads/onboarding_config json copy device_info json from tools/keygen/linux/output_{serialnumber} which contains your device specific information to application directory, apps/esp32/code_lab_example/main $ cd ~/workspace/st-device-sdk-c-ref/iot-core/tools/keygen/ $ cp output_{serialnumber}/device_info json ~/workspace/st-device-sdk-c-ref/apps/esp32/code_lab_example/main/ firmwareversion version string privatekey base64 encoded ed25519 private key this value should be paired with what you registered to developer workspace publickey base64 encoded ed25519 public key this value should be same with what you registered to developer workspace serialnumber this value should be same with what you registered to developer workspace { "deviceinfo" { "firmwareversion" "switch_example_001", "privatekey" "dh**jkmrd5x****bav+fgoxa3qzfnw3v****jhxomd0=", "publickey" "nfn5x***uqusq****zhobsfaaop9***kndlnjdjrew=", "serialnumber" "stdk**e90w***ucx" } } open sample application source code browse in apps/esp32/code_lab_example/main/ directory open main c file write your device application code this example already has cloud connection functionality using smartthings sdk apis void app_main void { /** easily integrate your direct connected device using the direct connected devices sdk the sdk manages all mqtt topics and onboarding requirements, freeing you to focus on the capabilities of your device that is, you can simply develop a basic application by just calling the apis provided by st_iot_core layer like below // create a iot context 1 st_conn_init ; // create a handle to process capability 2 st_cap_handle_init ; called in function 'capability_init' // register a callback function to process capability command when it comes from the smartthings server 3 st_cap_cmd_set_cb ; called in function 'capability_init' // process on-boarding procedure there is nothing more to do on the app side than call the api 4 st_conn_start ; */ unsigned char *onboarding_config = unsigned char * onboarding_config_start; unsigned int onboarding_config_len = onboarding_config_end - onboarding_config_start; unsigned char *device_info = unsigned char * device_info_start; unsigned int device_info_len = device_info_end - device_info_start; int err; iot_gpio_init ; xtaskcreate app_main_task, "app_main_task", 4096, null, 10, null ; //create a iot context iot_ctx = st_conn_init onboarding_config, onboarding_config_len, device_info, device_info_len ; if iot_ctx != null { err = st_conn_set_noti_cb iot_ctx, iot_noti_cb, null ; if err printf "fail to set notification callback function\n" ; } else { printf "fail to create the iot_context\n" ; } //create a handle to process capability and initialize capability info capability_init ; //connect to server err = st_conn_start iot_ctx, st_status_cb &iot_status_cb, iot_status_all, null, null ; if err { printf "fail to start connection err %d\n", err ; } } complete your command callback function for each capability the command callback function should contain your device control upon each command such as led on or off control capability example for each capability would be helpful to handle attribute command for each capability you can find it at apps/capability_example/main static void update_switch_state int switch_state { const char* switch_value; if switch_state == switch_on { switch_value = caps_helper_switch attr_switch value_on; } else { switch_value = caps_helper_switch attr_switch value_off; } //todo set switch attribute value and send // // example //=============================================================================== // cap_switch_data->set_switch_value cap_switch_data, switch_value ; // cap_switch_data->attr_switch_send cap_switch_data ; //=============================================================================== } static int get_switch_state void { const char* switch_value; int switch_state = switch_off; //todo get switch attribute value // // example //=============================================================================== // switch_value = cap_switch_data->get_switch_value cap_switch_data ; // // if !strcmp switch_value, caps_helper_switch attr_switch value_on { // switch_state = switch_on; // } else if !strcmp switch_value, caps_helper_switch attr_switch value_off { // switch_state = switch_off; // } //=============================================================================== return switch_state; } static void cap_switch_cmd_cb struct caps_switch_data *caps_data { int switch_state = get_switch_state ; set_led_mode switch_state ; } static void capability_init { //todo initialize switch capability with using capability sample's initializae function // // example // //=============================================================================== // cap_switch_data = caps_switch_initialize iot_ctx, "main", null, null ; // if cap_switch_data { // const char *switch_init_value = caps_helper_switch attr_switch value_on; // // cap_switch_data->cmd_on_usr_cb = cap_switch_cmd_cb; // cap_switch_data->cmd_off_usr_cb = cap_switch_cmd_cb; // // cap_switch_data->set_switch_value cap_switch_data, switch_init_value ; // } //=============================================================================== } build, flash, and monitor you can build, flash, and monitor in the console with the following $ cd ~/workspace/st-device-sdk-c-ref $ python3 build py apps/esp32/code_lab_example $ python3 build py apps/esp32/code_lab_example flash $ python3 build py apps/esp32/code_lab_example monitor or $ cd ~/workspace/st-device-sdk-c-ref $ python3 build py apps/esp32/code_lab_example flash monitor onboard and control the device via smartthings app onboarding a enable developer mode developer mode should be enabled on smartthings mobile application launch the smartthings app, go to dashboard > settings long-press about smartthings for 20 seconds enable the developer mode, then restart the smartthings app you can find out more information here b add a new device go to add device and click my testing devices or scan qr code you can now see and add your self-published devices control by smartthings application a device control from dashboard you can turn your device’s led on or off by clicking the button in the smartthings app b device control from plugin you can control various attributes and get more detailed information from the device plugin user interface you're done! congratulations! you have successfully achieved the goal of this code lab now, you can control your iot devices using the smartthings app all by yourself! if you're having trouble, you may download this file smartthings device sdk complete code 2 49 kb