Skip to content

Commit 27c50d8

Browse files
aidangarskedanielinux
authored andcommitted
Add MQTT client support for STM32H563 bare-metal operation
- Add mqtt_client.c/h: Non-blocking MQTT client with state machine (IDLE -> CONNECTING -> TLS -> MQTT_CONNECT -> CONNECTED) - Add wolfmqtt_io.c: I/O glue layer for wolfIP sockets - Update Makefile: Add ENABLE_MQTT build option and wolfMQTT sources - Update user_settings.h: Add wolfMQTT configuration (WOLFMQTT_NONBLOCK, WOLFMQTT_USER_IO, WOLFMQTT_NO_STDIO, EWOULDBLOCK/EAGAIN defines) - Update main.c: Integrate MQTT client into main loop with periodic status publishing - Update README.md: Add MQTT documentation section Features: - TLS 1.3 encrypted connection to MQTT broker (port 8883) - Connects to test.mosquitto.org by default - Publishes status messages to wolfip/status topic - QoS 0 (fire and forget) for minimal overhead - 60 second keep-alive Build: make ENABLE_TLS=1 ENABLE_MQTT=1
1 parent a89cd0a commit 27c50d8

7 files changed

Lines changed: 1141 additions & 6 deletions

File tree

src/port/stm32h563/Makefile

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ ENABLE_HTTPS ?= 0
1717
# SSH support: set ENABLE_SSH=1 to include wolfSSH server (requires TLS)
1818
ENABLE_SSH ?= 0
1919

20+
# MQTT support: set ENABLE_MQTT=1 to include wolfMQTT client (requires TLS)
21+
ENABLE_MQTT ?= 0
22+
2023
# Library paths - default to sibling directories (clone alongside pattern)
2124
WOLFSSL_ROOT ?= $(ROOT)/../wolfssl
2225
WOLFSSH_ROOT ?= $(ROOT)/../wolfssh
26+
WOLFMQTT_ROOT ?= $(ROOT)/../wolfmqtt
2327

2428
# Base compiler flags
2529
CFLAGS := -mcpu=cortex-m33 -mthumb -mcmse -Os -ffreestanding -fdata-sections -ffunction-sections
@@ -155,19 +159,59 @@ $(WOLFSSH_ROOT)/%.o: $(WOLFSSH_ROOT)/%.c
155159

156160
endif # ENABLE_SSH
157161

162+
# -----------------------------------------------------------------------------
163+
# MQTT Support (wolfMQTT) - requires TLS
164+
# -----------------------------------------------------------------------------
165+
ifeq ($(ENABLE_MQTT),1)
166+
167+
# MQTT requires TLS
168+
ifeq ($(ENABLE_TLS),0)
169+
$(error ENABLE_MQTT=1 requires ENABLE_TLS=1)
170+
endif
171+
172+
# Validate wolfMQTT exists
173+
ifeq ($(wildcard $(WOLFMQTT_ROOT)/wolfmqtt/mqtt_client.h),)
174+
$(error wolfMQTT not found at $(WOLFMQTT_ROOT). Clone it: git clone https://github.com/wolfSSL/wolfMQTT.git)
175+
endif
176+
177+
CFLAGS += -DENABLE_MQTT
178+
CFLAGS += -DWOLFMQTT_USER_SETTINGS
179+
CFLAGS += -I$(WOLFMQTT_ROOT)
180+
181+
# MQTT client and wolfMQTT-wolfIP glue
182+
SRCS += mqtt_client.c
183+
SRCS += $(ROOT)/src/port/wolfmqtt_io.c
184+
185+
# wolfMQTT source files (minimal set for MQTT client)
186+
WOLFMQTT_SRCS := \
187+
$(WOLFMQTT_ROOT)/src/mqtt_client.c \
188+
$(WOLFMQTT_ROOT)/src/mqtt_packet.c \
189+
$(WOLFMQTT_ROOT)/src/mqtt_socket.c
190+
191+
SRCS += $(WOLFMQTT_SRCS)
192+
193+
# wolfMQTT objects use relaxed warnings + MQTT/SSL include paths + user_settings.h
194+
$(WOLFMQTT_ROOT)/%.o: $(WOLFMQTT_ROOT)/%.c
195+
$(CC) $(CFLAGS_WOLFSSL) -DENABLE_MQTT -DWOLFSSL_USER_SETTINGS -DWOLFMQTT_USER_SETTINGS -I$(WOLFMQTT_ROOT) -I$(WOLFSSL_ROOT) -c $< -o $@
196+
197+
endif # ENABLE_MQTT
198+
158199
# -----------------------------------------------------------------------------
159200
# Build rules
160201
# -----------------------------------------------------------------------------
161202
OBJS := $(patsubst %.c,%.o,$(SRCS))
162203

163204
all: app.bin
164-
@echo "Built with TZEN=$(TZEN) ENABLE_TLS=$(ENABLE_TLS) ENABLE_HTTPS=$(ENABLE_HTTPS) ENABLE_SSH=$(ENABLE_SSH)"
205+
@echo "Built with TZEN=$(TZEN) ENABLE_TLS=$(ENABLE_TLS) ENABLE_HTTPS=$(ENABLE_HTTPS) ENABLE_SSH=$(ENABLE_SSH) ENABLE_MQTT=$(ENABLE_MQTT)"
165206
ifeq ($(ENABLE_TLS),1)
166207
@echo " wolfSSL: $(WOLFSSL_ROOT)"
167208
endif
168209
ifeq ($(ENABLE_SSH),1)
169210
@echo " wolfSSH: $(WOLFSSH_ROOT)"
170211
endif
212+
ifeq ($(ENABLE_MQTT),1)
213+
@echo " wolfMQTT: $(WOLFMQTT_ROOT)"
214+
endif
171215

172216
app.elf: $(OBJS) $(LDSCRIPT)
173217
$(CC) $(CFLAGS) $(OBJS) $(LDFLAGS) -Wl,--start-group -lc -lm -lgcc -lnosys -Wl,--end-group -o $@
@@ -193,6 +237,9 @@ endif
193237
ifeq ($(ENABLE_SSH),1)
194238
rm -f $(WOLFSSH_ROOT)/src/*.o
195239
endif
240+
ifeq ($(ENABLE_MQTT),1)
241+
rm -f $(WOLFMQTT_ROOT)/src/*.o
242+
endif
196243

197244
.PHONY: all clean
198245

@@ -214,20 +261,24 @@ help:
214261
@echo " ENABLE_TLS=1 Enable TLS server (requires wolfSSL)"
215262
@echo " ENABLE_HTTPS=1 Enable HTTPS web server (requires TLS)"
216263
@echo " ENABLE_SSH=1 Enable SSH server (requires TLS + wolfSSH)"
264+
@echo " ENABLE_MQTT=1 Enable MQTT client (requires TLS + wolfMQTT)"
217265
@echo " WOLFSSL_ROOT= Path to wolfSSL (default: ../wolfssl)"
218266
@echo " WOLFSSH_ROOT= Path to wolfSSH (default: ../wolfssh)"
267+
@echo " WOLFMQTT_ROOT= Path to wolfMQTT (default: ../wolfmqtt)"
219268
@echo ""
220269
@echo "Examples:"
221-
@echo " make # Basic TCP echo (port 7)"
222-
@echo " make ENABLE_TLS=1 # TLS echo server (port 8443)"
223-
@echo " make ENABLE_TLS=1 ENABLE_HTTPS=1 # TLS + HTTPS web (port 443)"
224-
@echo " make ENABLE_TLS=1 ENABLE_SSH=1 # TLS + SSH shell (port 22)"
225-
@echo " make ENABLE_TLS=1 ENABLE_HTTPS=1 ENABLE_SSH=1 # Full featured"
270+
@echo " make # Basic TCP echo (port 7)"
271+
@echo " make ENABLE_TLS=1 # TLS echo server (port 8443)"
272+
@echo " make ENABLE_TLS=1 ENABLE_HTTPS=1 # TLS + HTTPS web (port 443)"
273+
@echo " make ENABLE_TLS=1 ENABLE_SSH=1 # TLS + SSH shell (port 22)"
274+
@echo " make ENABLE_TLS=1 ENABLE_MQTT=1 # TLS + MQTT client"
275+
@echo " make ENABLE_TLS=1 ENABLE_HTTPS=1 ENABLE_SSH=1 ENABLE_MQTT=1 # Full featured"
226276
@echo ""
227277
@echo "Testing:"
228278
@echo " nc <ip> 7 # TCP echo"
229279
@echo " echo 'Hello' | openssl s_client -connect <ip>:8443 -quiet # TLS echo"
230280
@echo " curl -k https://<ip>/ # HTTPS web server"
231281
@echo " ssh admin@<ip> # SSH (password: wolfip)"
282+
@echo " mosquitto_sub -h test.mosquitto.org -t 'wolfip/status' -v # MQTT subscribe"
232283

233284
.PHONY: help

src/port/stm32h563/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,100 @@ xxd -i ssh_host_key.der > ssh_keys.h
587587
# Update the array name in ssh_keys.h to ssh_host_key_der
588588
```
589589

590+
## MQTT Client
591+
592+
When built with `ENABLE_MQTT=1`, the device includes an MQTT client that publishes status messages to a broker.
593+
594+
### Prerequisites
595+
596+
Clone wolfMQTT alongside wolfip:
597+
598+
```bash
599+
cd /path/to/parent
600+
git clone https://github.com/wolfSSL/wolfMQTT.git
601+
# wolfip should be at /path/to/parent/wolfip
602+
```
603+
604+
### Building MQTT Mode
605+
606+
```bash
607+
# MQTT requires TLS
608+
make ENABLE_TLS=1 ENABLE_MQTT=1
609+
```
610+
611+
Or specify a custom wolfMQTT path:
612+
613+
```bash
614+
make ENABLE_TLS=1 ENABLE_MQTT=1 WOLFMQTT_ROOT=/path/to/wolfmqtt
615+
```
616+
617+
### Expected Serial Output (MQTT Mode)
618+
619+
```
620+
=== wolfIP STM32H563 Echo Server ===
621+
...
622+
Initializing TLS server on port 8443...
623+
TLS: Server ready on port 8443
624+
Initializing MQTT client...
625+
MQTT: Initializing client
626+
MQTT: Initialized, connecting to 54.36.178.49:8883
627+
Entering main loop. Ready for connections!
628+
Loop starting...
629+
MQTT: Connecting...
630+
MQTT: TCP connected
631+
MQTT: TLS handshake...
632+
MQTT: TLS connected
633+
MQTT: Sending CONNECT...
634+
MQTT: Connected to broker
635+
```
636+
637+
### MQTT Configuration
638+
639+
| Setting | Default Value |
640+
|---------|--------------|
641+
| Broker IP | 54.36.178.49 (test.mosquitto.org) |
642+
| Broker Port | 8883 (TLS) |
643+
| Client ID | wolfip-stm32h563 |
644+
| Publish Topic | wolfip/status |
645+
| Keep-alive | 60 seconds |
646+
| QoS | 0 (fire and forget) |
647+
648+
### Subscribing to Device Messages
649+
650+
To see messages published by the device:
651+
652+
```bash
653+
# Subscribe to the status topic
654+
mosquitto_sub -h test.mosquitto.org -t "wolfip/status" -v
655+
656+
# Expected output (every 60 seconds):
657+
# wolfip/status STM32H563 online, uptime: 60s
658+
# wolfip/status STM32H563 online, uptime: 120s
659+
```
660+
661+
### Full Featured Build
662+
663+
Build with all features (TLS, HTTPS, SSH, and MQTT):
664+
665+
```bash
666+
make ENABLE_TLS=1 ENABLE_HTTPS=1 ENABLE_SSH=1 ENABLE_MQTT=1
667+
```
668+
669+
This provides:
670+
- TCP echo server on port 7
671+
- TLS echo server on port 8443
672+
- HTTPS web server on port 443
673+
- SSH shell on port 22
674+
- MQTT client publishing to test.mosquitto.org
675+
676+
### MQTT Files
677+
678+
| File | Description |
679+
|------|-------------|
680+
| `mqtt_client.c/h` | MQTT client state machine and API |
681+
| `../wolfmqtt_io.c` | wolfMQTT I/O glue layer for wolfIP sockets |
682+
| `user_settings.h` | wolfMQTT compile-time configuration |
683+
590684
## Files
591685

592686
| File | Description |
@@ -608,6 +702,8 @@ xxd -i ssh_host_key.der > ssh_keys.h
608702
| `https_server.c/h` | HTTPS web server (HTTPS builds only) |
609703
| `ssh_server.c/h` | SSH shell server (SSH builds only) |
610704
| `ssh_keys.h` | Embedded SSH host key (SSH builds only) |
705+
| `mqtt_client.c/h` | MQTT client state machine (MQTT builds only) |
706+
| `../wolfmqtt_io.c` | wolfMQTT I/O callbacks for wolfIP (MQTT builds only) |
611707

612708
## Troubleshooting
613709

src/port/stm32h563/main.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
#define SSH_PORT 22
4141
#endif
4242

43+
#ifdef ENABLE_MQTT
44+
#include "mqtt_client.h"
45+
#endif
46+
4347
#ifdef ENABLE_TLS
4448

4549
/* Google IP for TLS client test (run: dig +short google.com) */
@@ -584,6 +588,22 @@ int main(void)
584588
}
585589
#endif
586590

591+
#ifdef ENABLE_MQTT
592+
uart_puts("Initializing MQTT client...\n");
593+
{
594+
mqtt_client_config_t mqtt_config = {
595+
.broker_ip = "54.36.178.49", /* test.mosquitto.org IP (updated) */
596+
.broker_port = 8883, /* TLS port */
597+
.client_id = "wolfip-stm32h563",
598+
.publish_topic = "wolfip/status",
599+
.keep_alive_sec = 60
600+
};
601+
if (mqtt_client_init(IPStack, &mqtt_config, uart_puts) < 0) {
602+
uart_puts("ERROR: MQTT client init failed\n");
603+
}
604+
}
605+
#endif
606+
587607
uart_puts("Entering main loop. Ready for connections!\n");
588608
uart_puts("Loop starting...\n");
589609

@@ -607,6 +627,51 @@ int main(void)
607627
ssh_server_poll();
608628
#endif
609629

630+
#ifdef ENABLE_MQTT
631+
/* Poll MQTT client */
632+
mqtt_client_poll();
633+
634+
/* Publish status periodically (every ~60 seconds) */
635+
{
636+
static uint64_t last_publish_tick = 0;
637+
if (mqtt_client_is_connected() &&
638+
(tick - last_publish_tick) > 60000) {
639+
char status_msg[64];
640+
ip4 ip = 0;
641+
wolfIP_ipconfig_get(IPStack, &ip, NULL, NULL);
642+
643+
/* Format: "STM32H563 online, IP: x.x.x.x, uptime: XXXXX" */
644+
strcpy(status_msg, "STM32H563 online, uptime: ");
645+
{
646+
char num_buf[12];
647+
uint32_t uptime = (uint32_t)(tick / 1000);
648+
int i = 0, j;
649+
char tmp[12];
650+
651+
if (uptime == 0) {
652+
num_buf[0] = '0';
653+
num_buf[1] = '\0';
654+
} else {
655+
while (uptime > 0 && i < 11) {
656+
tmp[i++] = '0' + (uptime % 10);
657+
uptime /= 10;
658+
}
659+
j = 0;
660+
while (i > 0) {
661+
num_buf[j++] = tmp[--i];
662+
}
663+
num_buf[j] = '\0';
664+
}
665+
strcat(status_msg, num_buf);
666+
}
667+
strcat(status_msg, "s");
668+
669+
mqtt_client_publish(status_msg);
670+
last_publish_tick = tick;
671+
}
672+
}
673+
#endif
674+
610675
#ifdef ENABLE_TLS
611676
/* TLS client test: connect to Google after network settles */
612677
if (!tls_client_test_started && tick > 5000) {

0 commit comments

Comments
 (0)