Skip to content
Draft
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
7 changes: 7 additions & 0 deletions targets/zephyr/s0-mqtts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.20.0)

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

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
19 changes: 19 additions & 0 deletions targets/zephyr/s0-mqtts/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
mainmenu "Mqtt application"

config NET_SAMPLE_APP_MAX_ITERATIONS
int "Number of times to Publish sample MQTT messages"
default 500
help
Send sample MQTT messages this many times in a MQTT connection
before exiting. A value of zero means that the messages are sent
forever.

config NET_SAMPLE_APP_MAX_CONNECTIONS
int "Number of times to connect to the MQTT server"
default 0
help
Number of times to connect to the MQTT server. With each connection
send NET_SAMPLE_APP_MAX_ITERATIONS amount of MQTT sample messages.
A value of zero means to continue forever.

source "Kconfig.zephyr"
63 changes: 63 additions & 0 deletions targets/zephyr/s0-mqtts/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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

CONFIG_REQUIRES_FULL_LIBC=y
CONFIG_NET_IPV6=y
CONFIG_NET_SOCKETS=y
CONFIG_ZVFS_POLL_MAX=4
CONFIG_POSIX_API=y
CONFIG_CBPRINTF_FP_SUPPORT=y

CONFIG_HTTP_CLIENT=y
CONFIG_WEBSOCKET_CLIENT=y

# LOG Configuration
CONFIG_NET_LOG=y
CONFIG_NET_DHCPV4_SERVER_LOG_LEVEL_DBG=y
CONFIG_NET_SOCKETS_LOG_LEVEL_DBG=y
CONFIG_MBEDTLS_LOG_LEVEL_DBG=y
CONFIG_MBEDTLS_DEBUG=y

# Enable the MQTT Lib
CONFIG_MQTT_LIB=y

CONFIG_ENTROPY_GENERATOR=y
CONFIG_TEST_RANDOM_GENERATOR=y

# TLS Configuration
CONFIG_MBEDTLS=y
CONFIG_MBEDTLS_BUILTIN=y
CONFIG_MBEDTLS_ENABLE_HEAP=y
CONFIG_MBEDTLS_HEAP_SIZE=60000
CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=7168
CONFIG_MBEDTLS_HASH_ALL_ENABLED=y
CONFIG_MBEDTLS_CMAC=y
CONFIG_NET_SOCKETS_SOCKOPT_TLS=y
CONFIG_TLS_CREDENTIAL_FILENAMES=y

# MBED DEBUG CONFIGURATION
CONFIG_MBEDTLS_DEBUG=y
CONFIG_MBEDTLS_LOG_LEVEL_DBG=y
17 changes: 17 additions & 0 deletions targets/zephyr/s0-mqtts/src/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#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_IP "192.168.8.108" // Replace with your Magistrala instance IP
#define MAGISTRALA_MQTT_PORT 8883
#define DOMAIN_ID "a258028e-7bd9-40eb-8260-b2ab2e4b1f69" // Replace with your Domain ID
#define CLIENT_ID "5eface71-f8c2-44ce-a5e4-60bf9b052799" // Replace with your Client ID
#define CLIENT_SECRET "79156d99-9cbd-4f9b-a1a2-b602c55dfe23" // Replace with your Client secret
#define CHANNEL_ID "3c58dd6a-98b0-4703-a5e2-e1bcde980c4d" // Replace with your Channel ID
#define MQTT_CLIENTID "5eface71-f8c2-44ce-a5e4-60bf9b052799" // Replace with your actual client ID

#endif
74 changes: 74 additions & 0 deletions targets/zephyr/s0-mqtts/src/creds.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#ifndef CREDS_H
#define CREDS_H

#include <zephyr/kernel.h>
#include <zephyr/net/tls_credentials.h>

/* Security tags for credentials */
#define TLS_TAG_CA_CERT 1
#define TLS_TAG_CLIENT_CERT 2
#define TLS_TAG_CLIENT_KEY 3

/* CA Certificate - Replace with your broker's CA certificate */
const unsigned char ca_cert[] = ""; // Replace with your CA certificate in PEM format

const size_t ca_cert_len = sizeof(ca_cert) - 1;

/* Client Certificate - Optional, for client authentication */
const unsigned char client_cert[] = ""; // Replace with your client certificate in PEM format

const size_t client_cert_len = sizeof(client_cert) - 1;

/* Client Private Key - Optional, for client authentication */
const unsigned char client_key[] = ""; // Replace with your client private key in PEM format

const size_t client_key_len = sizeof(client_key) - 1;

static bool certs_initialized = false;

static inline int certs_init(void)
{
int ret;

if (certs_initialized) {
printk("Certificates already initialized");
return 0;
}

printk("Initializing mutual TLS certificates...");

// Add the CA certificate
ret = tls_credential_add(TLS_TAG_CA_CERT, TLS_CREDENTIAL_CA_CERTIFICATE,
ca_cert, ca_cert_len);
if (ret < 0) {
printk("Failed to add CA certificate: %d", ret);
return ret;
}
printk("CA certificate added successfully");

/* Add the client certificate */
ret = tls_credential_add(TLS_TAG_CLIENT_CERT, TLS_CREDENTIAL_PUBLIC_CERTIFICATE,
client_cert, client_cert_len);
if (ret < 0) {
printk("Failed to add client certificate: %d", ret);
return ret;
}
printk("Client certificate added successfully");

// Add the client private key
ret = tls_credential_add(TLS_TAG_CLIENT_KEY, TLS_CREDENTIAL_PRIVATE_KEY,
client_key, client_key_len);
if (ret < 0) {
printk("Failed to add client private key: %d", ret);
return ret;
}
printk("Client private key added successfully");


certs_initialized = true;
printk("Standard TLS certificates initialized successfully");

return ret;
}

#endif /* CREDS_H */
Loading