This repository contains a LwM2M Client application example for ESP32 devices, based on open-source Anjay library and Espressif IoT Development Framework.
The following boards are supported natively in the project:
- ESP-WROVER-KIT
- ESP32-DevKitC
- M5StickC-Plus
You may also use other boards, by selecting the Unknown
board and manually configuring available peripherals through Kconfig.
The following LwM2M Objects are supported:
Target | Objects |
---|---|
Common | Security (/0) Server (/1) Device (/3) Firmware Update (/5) WLAN connectivity (/12) |
ESP-WROVER-KIT | Push button (/3347) Light control (/3311) |
ESP32-DevKitC | Push button (/3347) |
M5StickC-Plus | Push button (/3347) Light control (/3311) Temperature sensor (/3303) Accelerometer (/3313) Gyroscope (/3343) |
- Install ESP-IDF and its dependencies on your computer. Please follow the instructions at https://docs.espressif.com/projects/esp-idf/en/v4.4/esp32/get-started/index.html up to and including the point where you call
. $HOME/esp/esp-idf/export.sh
- The project has been tested with ESP-IDF v4.4, but may work with other versions as well.
- Clone the repository
git clone https://github.com/AVSystem/Anjay-esp32-client.git
and navigate to project directory - Initialize and update submodules with
git submodule update --init --recursive
- Run
idf.py set-target esp32
in the project directory - Run
idf.py menuconfig
- navigate to
Component config/anjay-esp32-client
:- select one of supported boards or manually configure the board in
Board options
menu - configure Anjay in
Client options
menu - configure WiFi in
Connection configuration
menu
- select one of supported boards or manually configure the board in
- navigate to
- Run
idf.py build
to compile - Run
idf.py flash
to flash- NOTE: M5StickC-Plus does not support default baudrate, run
idf.py -b 750000 flash
to flash it
- NOTE: M5StickC-Plus does not support default baudrate, run
- The logs will be on the same
/dev/ttyUSB<n>
port that the above used for flashing, 115200 8N1- You can use
idf.py monitor
to see logs on serial output from a connected device, or even more convenientlyidf.py flash monitor
as one command to see logs right after the device is flashed
- You can use
To connect to Coiote IoT Device Management LwM2M Server, please register at https://eu.iot.avsystem.cloud/. The default Server URI (Kconfig option ANJAY_CLIENT_SERVER_URI
) is set to EU Cloud Coiote DM instance, but you must manually set other client configuration options.
NOTE: You may use any LwM2M Server compliant with LwM2M 1.0 TS. The server URI can be changed in the example configuration options.
It is possible to use pre-built binaries to flash the board and provide credentials by flashing a NVS partition binary.
To do that, esptool.py
is required, which can be installed running pip install esptool
esptool.py --chip esp32 merge_bin --flash_mode dio --flash_size 4MB --flash_freq 40m 0x1000 build/bootloader/bootloader.bin 0x8000 build/partition_table/partition-table.bin 0x10000 build/anjay-esp32-client.bin 0x310000 build/storage.bin --output m5stickc-plus.bin
To generate NVS partition, create a nvs_config.csv
file with following content:
key,type,encoding,value
config,namespace,,
wifi_ssid,data,string,[wifi_ssid]
wifi_pswd,data,string,[wifi_password]
wifi_inter_en,data,u8,1
endpoint_name,data,string,[endpoint_name]
identity,data,string,[identity]
psk,data,string,[psk]
uri,data,string,[lwm2m_server_uri]
writable_wifi,namespace,,
wifi_ssid,data,string,[wifi_ssid]
wifi_pswd,data,string,[wifi_password]
wifi_inter_en,data,u8,0
And fill proper values for [wifi_ssid]
, [wifi_password]
, [endpoint_name]
, [identity]
, [psk]
, [lwm2m_server_uri]
.
After that create config partition by running:
python3 $IDF_PATH/components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py generate nvs_config.csv nvs_config.bin 0x4000
After partition is created, flash firmware and configuration using following commands (baudrate may vary depending on your board):
esptool.py -b 750000 --chip esp32 write_flash 0x0000 m5stickc-plus.bin
esptool.py -b 750000 --chip esp32 write_flash 0x9000 nvs_config.bin
Device will be reset and run with provided configuration.
To switch to TCP socket instead of UDP run idf.py menuconfig
, navigate to Component config/anjay-esp32-client/Client options/Choose socket
and select TCP (remember that you must also provide a proper URI in the nvs_config.csv
file, e.g. coaps+tcp://eu.iot.avsystem.cloud:5684
).
- Prepare your certificates. All certificates should have a
.der
extension and should be added to the directory where thisREADME.md
file is located. The names of the certificates should be as follows:- client public certificate -
client_cert.der
- client private certificate -
client_key.der
- server public certificate -
server_cert.der
- client public certificate -
- Run
idf.py menuconfig
, navigate toComponent config/anjay-esp32-client/Client options/Choose security mode
and selectCertificates
.
After compilation, you can perform FOTA with Coiote DM. Required binary file location:
$PROJECT_DIR/build/anjay-esp32-client/build/anjay-esp32-client.bin
- Prepare your BG96 module, connect it to the selected ESP32 UART interface.
- Run
idf.py menuconfig
- navigate to
Component config/anjay-esp32-client
:- select
External BG96 module
inChoose an interface
menu - configure BG96 in
BG96 module configuration
menu - configure PDN in
Connection configuration
menu
- select
- navigate to