Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Embedded Mainflux Components

This repo contains code to connect embedded devices to mainflux.

## Usage

The build instructions for each of the targets can be found in the [targets](/targets) folder under each specific target.

## Community

- [Gitter](https://gitter.im/mainflux/mainflux?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
- [Twitter](https://twitter.com/mainflux)

## License

[Apache-2.0](LICENSE)
2 changes: 1 addition & 1 deletion targets/zephyr/coap/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(coap_client)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
target_include_directories(app PRIVATE ${ZEPHYR_BASE}/subsys/net/ip)
134 changes: 127 additions & 7 deletions targets/zephyr/coap/README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,146 @@
MQTTS- esp32 target
# Magistrala CoAP Client for Zephyr

This is a Zephyr RTOS application that connects to a Magistrala IoT platform using CoAP (Constrained Application Protocol) and sends telemetry data.

## Features

- WiFi connectivity with automatic IP address acquisition via DHCP
- CoAP client with confirmable (CON) message support
- UDP-based communication
- Query parameter-based authentication
- Simulated sensor data (temperature, humidity, battery level, LED state)
- SenML JSON payload format
- Automatic error handling
- Configurable telemetry interval

## Hardware Requirements

- ESP32-S3 or compatible board with WiFi support
- Zephyr RTOS support

## Requirements

1. Mainflux broker details including: hostname, ThingID, Thing Credentials and Channel ID
1. Magistrala broker details including: hostname, Domain ID, Client ID, Client Secret and Channel ID
2. [Zephyr](https://www.zephyrproject.org/)

## Configure
## Configuration

Edit `src/config.h` to configure your settings:

### WiFi Configuration

```c
#define WIFI_SSID "your-wifi-ssid"
#define WIFI_PSK "your-wifi-password"
```

### Magistrala Configuration

```c
#define MAGISTRALA_HOSTNAME "your-magistrala-instance.com"
#define MAGISTRALA_COAP_PORT 5683
#define DOMAIN_ID "your-domain-id"
#define CLIENT_ID "your-client-id"
#define CLIENT_SECRET "your-client-secret"
#define CHANNEL_ID "your-channel-id"
```

1. Edit the [config file](include/config.h) with your broker details.
### Application Configuration

```c
#define TELEMETRY_INTERVAL_SEC 30 // Telemetry send interval in seconds
```

## Build

The project can be built by utilising the make file within the target directory
The project can be built by utilizing west within the target directory:

```bash
west build -p always -b <your-board-name> mqtt
west build -p always -b esp32s3_devkitc/esp32s3/procpu
```

## Flash

Platform io generate a build directory with the fimware.bin within it. Use the make command to flash to board
Use the west command to flash to board:

```bash
west flash
```

## Monitoring

Monitor serial output:

```bash
west espressif monitor
```

## CoAP Resource Structure

The client sends POST requests to:

```text
coap://{MAGISTRALA_HOSTNAME}:{MAGISTRALA_COAP_PORT}/m/{DOMAIN_ID}/c/{CHANNEL_ID}?auth={CLIENT_SECRET}
```

## Payload Format

The client sends data in SenML JSON format:

```json
[
{
"bn": "esp32s3:",
"bt": 1761700000,
"bu": "Cel",
"bver": 5,
"n": "temperature",
"u": "Cel",
"v": 23.45
},
{
"n": "humidity",
"u": "%RH",
"v": 65.30
},
{
"n": "battery",
"u": "%",
"v": 85
},
{
"n": "led",
"vb": false
}
]
```

## Authentication

The client uses CoAP URI query parameter authentication:

- Query parameter: `auth={CLIENT_SECRET}`

## CoAP Features

- CoAP Version 1
- Confirmable (CON) message type
- POST method for telemetry data
- Token-based message correlation
- Message ID for deduplication
- Response timeout handling (5 seconds)

## Error Handling

- Automatic system reboot on WiFi connection failure
- Automatic system reboot on DHCP timeout
- Socket creation error handling
- CoAP packet initialization and parsing error handling
- Response timeout handling

## Protocol Details

- Transport: UDP (SOCK_DGRAM)
- Default port: 5683
- Message format: CoAP binary
- Payload marker support
62 changes: 42 additions & 20 deletions targets/zephyr/coap/prj.conf
Original file line number Diff line number Diff line change
@@ -1,30 +1,52 @@
# Generic networking options
CONFIG_NETWORKING=y
CONFIG_NEWLIB_LIBC=y
CONFIG_NET_IPV6=y
# Wi-Fi Configuration
CONFIG_WIFI=y

# Network Configuration
CONFIG_NET_CONFIG_AUTO_INIT=n
CONFIG_NET_CONNECTION_MANAGER=y
CONFIG_NET_DHCPV4=y
CONFIG_NET_DHCPV4_SERVER=y
CONFIG_NET_IF_MAX_IPV4_COUNT=2
CONFIG_NET_IF_MAX_IPV6_COUNT=2
CONFIG_NET_IPV4=y
CONFIG_NET_L2_ETHERNET=y
CONFIG_NET_L2_WIFI_MGMT=y
CONFIG_NET_MGMT=y
CONFIG_NET_MGMT_EVENT=y
CONFIG_NET_MGMT_EVENT_INFO=y
CONFIG_NET_MGMT_EVENT_QUEUE_SIZE=10
CONFIG_NET_MGMT_EVENT_STACK_SIZE=4096
CONFIG_NET_PKT_RX_COUNT=16
CONFIG_NET_PKT_TX_COUNT=16
CONFIG_NET_SOCKETS_SERVICE_STACK_SIZE=4096
CONFIG_NET_TCP=y
CONFIG_NET_UDP=y
CONFIG_NETWORKING=y
CONFIG_ESP32_WIFI_STA_AUTO_DHCPV4=y

# Socket configuration
CONFIG_REQUIRES_FULL_LIBC=y
CONFIG_NET_IPV6=y
CONFIG_NET_SOCKETS=y
CONFIG_ZVFS_POLL_MAX=4
CONFIG_POSIX_API=y
CONFIG_NET_SOCKETS_POLL_MAX=4

CONFIG_COAP=y
CONFIG_CBPRINTF_FP_SUPPORT=y

# Kernel options
# DNS Configuration (for hostname resolution)
CONFIG_DNS_RESOLVER=y
CONFIG_DNS_RESOLVER_ADDITIONAL_BUF_CTR=5
CONFIG_DNS_RESOLVER_ADDITIONAL_QUERIES=2
CONFIG_DNS_NUM_CONCUR_QUERIES=2
CONFIG_DNS_RESOLVER_MAX_SERVERS=2
CONFIG_DNS_SERVER_IP_ADDRESSES=y

# Random Number Generation
CONFIG_ENTROPY_GENERATOR=y
CONFIG_TEST_RANDOM_GENERATOR=y

# Logging
CONFIG_PRINTK=y
CONFIG_NET_LOG=y

# Network Shell
CONFIG_NET_SHELL=y
# System Configuration
CONFIG_REBOOT=y

CONFIG_NET_CONFIG_SETTINGS=y
CONFIG_NET_CONFIG_NEED_IPV6=y
CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::2"
CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::1"

CONFIG_HEAP_MEM_POOL_SIZE=2048
# LOG Configuration
CONFIG_NET_LOG=y
CONFIG_NET_DHCPV4_SERVER_LOG_LEVEL_DBG=y
19 changes: 19 additions & 0 deletions targets/zephyr/coap/src/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef CONFIG_H
#define CONFIG_H

/* STA Mode Configuration */
#define WIFI_SSID "SSID" // Replace `SSID` with WiFi ssid
#define WIFI_PSK "PASSWORD" // Replace `PASSWORD` with Router password

/* Magistrala Configuration */
#define MAGISTRALA_HOSTNAME "MAGISTRALA_HOSTNAME" // Replace with your Magistrala instance hostname or IP
#define MAGISTRALA_COAP_PORT 5683
#define DOMAIN_ID "DOMAIN_ID" // Replace with your Domain ID
#define CLIENT_ID "CLIENT_ID" // Replace with your Client ID
#define CLIENT_SECRET "CLIENT_SECRET" // Replace with your Client secret
#define CHANNEL_ID "CHANNEL_ID" // Replace with your Channel ID

/* Application Configuration */
#define TELEMETRY_INTERVAL_SEC 30

#endif
Loading