Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NOISSUE-Add stm32 mqtts capability #6

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions targets/stm32/mqtts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.pio
.vscode
17 changes: 17 additions & 0 deletions targets/stm32/mqtts/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
all:
platformio -f run

upload:
platformio -f run --target upload

clean:
platformio -f run --target clean

program:
platformio -f run --target program

uploadfs:
platformio -f run --target uploadfs

update:
platformio -f update
26 changes: 26 additions & 0 deletions targets/stm32/mqtts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# MQTTS- stm32 target
## Requirements
1. Mainflux broker details including: hostname, ThingID, Thing Credentials and Channel ID
2. [PlatformIO](https://platformio.org/)
3. [dfu-util](https://dfu-util.sourceforge.net/)
4. [STM32CubeIDE](https://www.st.com/en/development-tools/stm32cubeide.html)

## Configure
Resolving nucleo_f429zi dependencies...
1. Use the STM32CUbeIDE to generate the specific files for your target. Please ensure to add the [Lwip](https://git.savannah.nongnu.org/git/lwip.git) and [Paho embedded c](https://github.com/eclipse/paho.mqtt.embedded-c.git) amd [mbedtls](https://github.com/Mbed-TLS/mbedtls) libraries as third party libraries. Then copy the files to this section
Edit the platform.ini file for the specific target.
2. Edit the [config file](include/config.h) with your broker and network details.
3. Generate and add a certificate in the [MQTTINterface source file](src\MQTTInterface.c).

## Build
The project can be built by utilising the make file within the target directory

```bash
make
```
## Flash
Platform io generate a build directory with the fimware.bin within it. Use the make command to flash to board
```bash
make upload
```

27 changes: 27 additions & 0 deletions targets/stm32/mqtts/include/MQTTClientapp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef MQTT_CLIENT_APP_H_
#define MQTT_CLIENT_APP_H_

#include <string.h>

#include "MQTTClient.h"
#include "MQTTInterface.h"
#include "cmsis_os.h"
#include "config.h"

#define MQTT_PORT 1883
#define MQTT_BUFSIZE 1024
#define ERR_CODE -1

Network net;
MQTTClient mqttClient;

uint8_t sndBuffer[MQTT_BUFSIZE];
uint8_t rcvBuffer[MQTT_BUFSIZE];
uint8_t msgBuffer[MQTT_BUFSIZE];

void mqttClientSubTask(void const *argument);
void mqttClientPubTask(void const *argument);
int mqttConnectBroker(void);
void mqttMessageArrived(MessageData *msg);

#endif
34 changes: 34 additions & 0 deletions targets/stm32/mqtts/include/MQTTInterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef _MQTTInterface_H
#define _MQTTInterface_H

typedef struct Timer Timer;

struct Timer
{
unsigned long systick_period;
unsigned long end_time;
};

typedef struct Network Network;

struct Network
{
int (*mqttread)(Network *, unsigned char *, int, int);
int (*mqttwrite)(Network *, unsigned char *, int, int);
void (*disconnect)(Network *);
};

void initTimer(Timer *);
char timerIsExpired(Timer *);
void timerCountdownMS(Timer *, unsigned int);
void timerCountdown(Timer *, unsigned int);
int timerLeftMS(Timer *);

int netInit(Network *);
int netConnect(Network *, char *, int);
int netRead(Network *, unsigned char *, int, int);
int netWrite(Network *, unsigned char *, int, int);
void netDisconnect(Network *);
void netClear(void);

#endif
18 changes: 18 additions & 0 deletions targets/stm32/mqtts/include/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef CONFIG_H
#define CONFIG_H

#define ERR_CODE -1
#define CLIENTID "STM32F4"
#define MQTT_WILL_FLAG 0
#define MQTT_VERSION 3
#define MQTT_CLEAN_SESSION 1
#define TOPIC_BUFFER_SIZE 128

const char *mfThingId = " ";
const char *mfThingPass = " ";
const char *mfChannelId = " ";
char mfTopic[TOPIC_BUFFER_SIZE];

const char *server = " ";

#endif
29 changes: 29 additions & 0 deletions targets/stm32/mqtts/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:nucleo_f429zi]
platform = ststm32
board = nucleo_f429zi
framework = stm32cube
monitor_speed=115200
lib_deps=
https://git.savannah.nongnu.org/git/lwip.git
https://github.com/eclipse/paho.mqtt.embedded-c.git
https://github.com/Mbed-TLS/mbedtls

upload_protocol = dfu
build_flags =
-D ENABLE_USB_SERIAL
-D USBCON
-D USBD_VID=0x0483
-D USBD_PID=0x5740
-D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC

-D HAL_PCD_MODULE_ENABLED
108 changes: 108 additions & 0 deletions targets/stm32/mqtts/src/MQTTClientapp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include "main.h"
#include "MQTTClientapp.h"

#define MESSAGE_DELAY 1000
#define OS_DELAY 100
#define KEEP_ALIVE_INT 60

void createMainfluxChannel(void)
{
const char *_preId = "channels/";
const char *_postId = "/messages";
strcpy(mfTopic, _preId);
strcat(mfTopic, mfChannelId);
strcat(mfTopic, _postId);
}

void mqttClientSubTask(void const *argument)
{
while (1)
{
if (!mqttClient.isconnected)
{
MQTTDisconnect(&mqttClient);
mqttConnectBroker();
osDelay(MESSAGE_DELAY);
}
else
{
MQTTYield(&mqttClient, MESSAGE_DELAY);
osDelay(OS_DELAY);
}
}
}

void mqttClientPubTask(void const *argument)
{
const char *str = "{'message':'hello'}";
MQTTMessage message;

while (1)
{
if (mqttClient.isconnected)
{
message.payload = (void *)str;
message.payloadlen = strlen(str);

MQTTPublish(&mqttClient, mfTopic, &message);
}
osDelay(MESSAGE_DELAY);
}
}

int mqttConnectBroker()
{
int ret;

net_clear();
ret = net_init(&net);
if (ret != MQTT_SUCCESS)
{
printf("net_init failed.\n");
return ERR_CODE;
}

ret = net_connect(&net, server, MQTT_PORT);
if (ret != MQTT_SUCCESS)
{
printf("net_connect failed.\n");
return ERR_CODE;
}

MQTTClientInit(&mqttClient, &net, MESSAGE_DELAY, sndBuffer, sizeof(sndBuffer), rcvBuffer, sizeof(rcvBuffer));
createMainfluxChannel();

MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.willFlag = MQTT_WILL_FLAG;
data.MQTTVersion = MQTT_VERSION;
data.clientID.cstring = CLIENTID;
data.username.cstring = mfThingId;
data.password.cstring = mfThingPass;
data.keepAliveInterval = KEEP_ALIVE_INT;
data.cleansession = MQTT_CLEAN_SESSION;

ret = MQTTConnect(&mqttClient, &data);
if (ret != MQTT_SUCCESS)
{
printf("MQTTConnect failed.\n");
return ret;
}

ret = MQTTSubscribe(&mqttClient, mfChannelId, QOS0, mqttMessageArrived);
if (ret != MQTT_SUCCESS)
{
printf("MQTT Subscribe failed.\n");
return ret;
}

return MQTT_SUCCESS;
}

void mqttMessageArrived(MessageData *msg)
{
MQTTMessage *message = msg->message;
memset(msgBuffer, 0, sizeof(msgBuffer));
memcpy(msgBuffer, message->payload, message->payloadlen);

printf("MQTT MSG[%d]:%s\n", (int)message->payloadlen, msgBuffer);
}
Loading