How to Build Direct Connected Devices

Not sure how much help I can be until you can figure out a way to look at a logfile. As I mentioned, there is a facility to create log files (which I’ve not used myself), and it looks like you enable it by defining a couple constants in /st-device-sdk-c/stdkconfig:

CONFIG_STDK_IOT_CORE_LOG_FILE
CONFIG_STDK_IOT_CORE_LOG_FILE_FLASH_WITH_RAM

Look at the /st-device-sdk-c/src/iot_log_file.c to see what it does. I don’t know of any documentation on this unfortunately. Maybe someone here can provide more insight.

What kind of mobile phone do you have? I have iphone and it first asks if you want to connect to the AP. If you can verify that the device has gone into SoftAP mode and is broadcasting it’s SSID but you never see any progress in the mobile app after initiating the onboarding through Add device, then it sounds like it’s getting hung up in the TLS handshaking between mobile and device. Are you typing in the device serial number manually in the mobile app or using a QR code?

Is the button capability even working? I thought there were issues, so I was staying away from it.

That’s pretty interesting - what is your device?

Hi, @bban! @orangebucket is right, here you can find the capability definition.
In case you have more doubts about this, you can open a new post and tag us to keep this thread on the direct-connected devices topic as it is device presentation-related.

1 Like

Just a typo, I’m sending in the correct values. As I stated, it works but I have double press or hold first. Presumably this changes the capability value to double or held in Smartthings, then when ‘pushed’ comes in on a subsequent press the automation is triggered. Single presses after that do not trigger the automation till you do a double or held to change the state.

To restate my question so it doesn’t get lost with the typo re-explanation. Does the button device have to reset its button capability value after sending in ‘pushed’ for subsequent pressed to to trigger an automation?

does this mean it did not out up to esp32?

[3/10] Performing build step for ‘bootloader’
[1/2] Linking C executable bootloader.elf
[2/2] Generating binary image from built executable
esptool.py v2.8
Generated C:/Users/eddie/st-device-sdk-c-ref/apps/esp32/switch_example/build/bootloader/bootloader.bin
ninja: build stopped: subcommand failed.
ninja failed with exit code 1
WARN : Failed to copy output file (C:\Users\eddie\st-device-sdk-c-ref\apps\esp32\switch_example\build\switch_example.bin)
WARN : Failed to copy output file (C:\Users\eddie\st-device-sdk-c-ref\apps\esp32\switch_example\build\switch_example.elf)
WARN : Failed to copy output file (C:\Users\eddie\st-device-sdk-c-ref\apps\esp32\switch_example\build\switch_example.map)

binary path : C:\Users\eddie\st-device-sdk-c-ref\output\esp32\iotcore_switch_example_20210302_ca43c5e_f883b41

C:\Users\eddie\st-device-sdk-c-ref>

this is what i get when running

python build.py apps/esp32/switch_example flash

raise SerialTimeoutException(‘Write timeout’)
serial.serialutil.SerialTimeoutException: Write timeout
esptool.py failed with exit code 1

Usage: python build.py apps/[BSP_NAME]/[APP_NAME]
or
python build.py [BSP_NAME] [APP_NAME]

[BSP_NAME] : [APP_NAME]
emw3080 : light_example, switch_example
emw3166 : light_example, switch_example
esp32 : light_example, ota_demo, switch_example
esp32s2 : light_example
esp8266 : light_example, switch_example
rtl8195 : light_example, switch_example
rtl8720c : light_example, switch_example
rtl8721c : light_example, switch_example

ex) python build.py apps/esp32/switch_example
ex) python build.py esp32 light_example

C:\Users\eddie\st-device-sdk-c-ref>

It’s critical for anyone developing direct-connected device apps to know that I have discovered that there is a significant memory leak in the current core SDK code. Anyone who is using a compiled core library that uses the POSIX OS module may experience this (check your stdkconfig file entries for STDK_IOT_CORE_OS_SUPPORT_POSIX).

It’s fairly significant - consuming over 3 megs of memory in a 24 hour period - with the device app (in this case the example switch app) sitting idle for most of the time. I’ve verified this using Valgrind and have found the biggest problem to be a communications-related timer that is repeatedly created and malloc’s memory that is never freed. Of course if your platform has garbage collection, or you are using a different OS module, you may not be seeing this. I don’t know if there are similar issues with other configurations, but I’d highly recommend checking yours.

Reported this to Samsung 10 days ago on github, so Kwang-Hui is aware of it. No fix available yet.

UPDATE: Kwang-Hui has fixed the POSIX port memory leak as well as improved handling on wifi init errors and new switch example code to update deprecated functions. This will be part of the 1.5.10 release so be sure to update your core SDK code when it gets posted.

THANK YOU Kwang-Hui!

8 Likes

Thanks @TAustin .
There shouldn’t be such a problem. POSIX port was used only for testing purpose from my side. there was some problem.

Thanks for your effort. your report is very helpful for us.
Really appreciated.

1 Like

Hi I may be doing something wrong but have added motion sensor capability and used the switch example and all works lovely. I can add my device and set routines etc for it. However, as soon as power is lost and restored the device doesn’t reconnect automatically I have to go through adding the device again (once re added it all works well but still when power cycled I need to re add again) is there something I not doing correctly?
Many thanks

Hi - it sounds like the provisioning files that SmartThings saves once you onboard your device may be getting clobbered when power is lost. I develop on a Raspberry Pi, so not too familiar with MCU environment, but do you know if the files that the core SDK is writing to your device are getting stored in non-volatile storage?

1 Like

Hi, I am using an esp8266 and to be honest I have no idea what get saved where. Don’t get me wrong I’ve written code and deployed to esp8266 saving to internal and external eeprom, I’ve even stored data in an sql database on my webserver but this has been using the basic arduino editor before which seems to have simpler syntax than the code used on this tutorial.

I would presume all the program code gets saved to the internal storage but no idea when running where the SDK saves anything once its been on boarded.

You have given me an idea on what to look for now though, I will hunt through the included files and see if I can debug step through the program once its running to see if I can see what it’s doing thank you very much.

1 Like

Hi, I am a college student trying to learn more about IoT and am brand new to SmarThings Dev.

I have what might be a dumb question here. How is the device connected to the internet/cloud? It seems to me that the SmartThings docs assume that the device must take care of its own handshake with a phone in order to connect to the cloud via Bluetooth or wifi. Is this the case? And if so, does SmartThings doc have a set of assumed requirements for the IoT device (I get that’s a pretty obvious one for IoT devices, I just figured SmartThings would handle it for you)?

Hey, @percydagreat

In case of WiFi devices, they’re often programmed to configure the network settings through its WiFi Access Point, i.e. App connects to the device > Setup Network Config.

To get a better feel of this workflow, I recommend you to play around with Direct-Connected setting up the examples for the ESP32 or ESP8266 boards.

… by the way, welcome to the community!!! :nerd_face:

1 Like

Hi @jody.albritton,
Thank you for the tutorial on building direct connected devices with SmartThings.

I would like to connect my ESP32-WROOM-32E using this method for a product I’m making. I have a couple of questions:

  • Google Cloud IoT has a pricing structure page that shows how much the data costs. Does Samsung/SmartThings have a similar page? I’m trying to understand how much it will cost me in data fees if I sell say 10,000 of my product, and each sends a few MBs of data to the SmartThings Cloud each day.

  • You mention that you should use Ubuntu as the development OS. I am currently using Windows 10 with the ESP-IDF toolchain. Will I need to switch to Ubuntu?

  • With the direct connected device method, will we be able to see plots of the historical data in the SmartThings app? Where is this data stored? How long is it stored there?

Thank you for your help.

You can do it with windows but the toolchain examples are all using linux. WSL for windows will be the easiest path to development if you don’t want to do a full VM or install the distro on your computer directly. Any debian based distro should be fine.

There is currently no cost associated with direct connected devices on the SmartThings side.

If your capability supports showing historical data, then it would work like any other device.

@jody.albritton thank you for the help. I set everything up in WSL Ubuntu as you suggested. I have run through your tutorial, and now am at the stage of flashing to my device, and I am running into the following error:

esptool.py -p /dev/ttyS4 -b 460800 --before default_reset --after hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 4MB 0x8000 partition_table/partition-table.bin 0x11000 ota_data_initial.bin 0x1000 bootloader/bootloader.bin 0x20000 switch_example.bin
esptool.py v2.8
Serial port /dev/ttyS4
Connecting…_
Detecting chip type… ESP32
Chip is ESP32D0WDQ5 (revision 3)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 44:17:93:7b:c9:f8
Uploading stub…
Running stub…
Stub running…
Changing baud rate to 460800
Changed.
Configuring flash size…

A fatal error occurred: Timed out waiting for packet header
esptool.py failed with exit code 2

When I look up the “Timed out waiting for packet header” error, I find results showing I need to push the bootloader button on the board. I do not think this is the issue, as the esptool seems to be connecting to my ESP32-WROOM-32E just fine. Any ideas?

@jody.albritton, when I flash a different program I made in ESP-IDF for Windows 10, it is successful, and has the following output:

esptool.py esp32 -p COM4 -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 4MB 0x8000 partition_table/partition-table.bin 0x16000 ota_data_initial.bin 0x1000 bootloader/bootloader.bin 0x20000 aqm.bin
esptool.py v3.1-dev
Serial port COM4
Connecting…
Chip is ESP32-D0WD-V3 (revision 3)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 44:17:93:7b:c9:f8
Uploading stub…
Running stub…
Stub running…
Changing baud rate to 460800
Changed.
Configuring flash size…
Flash will be erased from 0x00008000 to 0x00008fff…
Flash will be erased from 0x00016000 to 0x00017fff…
Flash will be erased from 0x00001000 to 0x00007fff…
Flash will be erased from 0x00020000 to 0x00120fff…
Compressed 3072 bytes to 178…
Writing at 0x00008000… (100 %)

The only real differences I see is that the successful flash is using esptool.py v3.1-dev (vs v2.8) and where ota_data_initial.bin is located (0x16000 vs 0x11000).

1 Like

@jody.albritton I managed to get around the issue above by moving to a native Ubuntu install (no Windows WSL). I now have most everything working, but I cannot figure out how to use the Firmware Update capability. I’ve followed the ota_demo example, but I don’t see anywhere on the SmartThings Developer Workspace where I can upload the latest firmware I want to OTA update to the device. I did see that the Firmware Update capability is “proposed” so maybe it’s not ready yet? Or is it not available when the project is in “Test”, but will be available once it’s “Published”?