Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

MQTT Implementation and HiveMQ connection #40

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
fb59193
Added Commands for the MQTT Implementation
Ti-ger Jun 10, 2022
b6d16d3
Copied esp-mqtt, if this works out we whould probably fork it
Ti-ger Jun 10, 2022
f8e7712
added skeleton of a MQTTManager
Ti-ger Jun 10, 2022
01b4425
Well, even I didn't expect this would work :see_no_evil:
Ti-ger Jun 10, 2022
6be5083
actually used the commandline parameters and added the unsub command
Ti-ger Jun 10, 2022
ff6bfcf
added some logic to delay commands if the connection is not establish…
Ti-ger Jun 10, 2022
d57a2fd
renamed certificate
Ti-ger Jun 10, 2022
c62147c
Changed MQTT configuration from hardcoded to Kconfig
Ti-ger Jun 23, 2022
44b62ed
simple mqtt example
Ti-ger Jun 24, 2022
04c75db
Added Commands for the MQTT Implementation
Ti-ger Jun 10, 2022
309c00f
added skeleton of a MQTTManager
Ti-ger Jun 10, 2022
e84e223
Well, even I didn't expect this would work :see_no_evil:
Ti-ger Jun 10, 2022
e4a1dd7
actually used the commandline parameters and added the unsub command
Ti-ger Jun 10, 2022
8b66ee8
added some logic to delay commands if the connection is not establish…
Ti-ger Jun 10, 2022
c5c4f01
renamed certificate
Ti-ger Jun 10, 2022
80e1692
removed unnecessary files
bowski23 Jun 24, 2022
992934b
feat(subscribe): WIP for subscription (#33)
vringar Jun 23, 2022
32d818c
Fixed leftover merge markers
vringar Jun 27, 2022
820881b
removed unused header
Ti-ger Jun 30, 2022
fb72053
Update MQTTManager.h
Ti-ger Jun 30, 2022
c388c89
Update Subscription.h
Ti-ger Jun 30, 2022
ea5790c
Update MQTTManager.cpp
Ti-ger Jun 30, 2022
661f470
Merge branch 'feat/mqtt' of github.com:OpenFogStack/matterHub into fe…
Ti-ger Jun 30, 2022
00c47f0
changed the DDATA format to be conform with our sparkplug specification
Ti-ger Jun 30, 2022
d34b3f7
fixed chip tool to support "\!" in the ssid name (thanks AVM)
Ti-ger Jun 30, 2022
0feb735
noted the MQTT configuration in the README
Ti-ger Jun 30, 2022
76fd2af
Changed subscribe to support callbacks, created first prototype for DCMD
Ti-ger Jun 30, 2022
7f72b5e
extended MQTT handling, added DCMD callback
Ti-ger Jul 2, 2022
81e76e6
Merge branch 'main' into feat/mqtt
vringar Jul 3, 2022
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 src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ idf_build_set_property(COMPILE_OPTIONS "-Wno-format-nonliteral;-Wno-format-secur
#
# See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635
idf_build_set_property(COMPILE_OPTIONS "-Wno-error=maybe-uninitialized" APPEND)
target_add_binary_data(matterHub.elf "main/mqtt_hivemq.pem" TEXT)


flashing_script()
4 changes: 3 additions & 1 deletion src/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ idf_component_register(PRIV_INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/light-switch-app"
"${CMAKE_CURRENT_LIST_DIR}/include"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated/ids"
vringar marked this conversation as resolved.
Show resolved Hide resolved
SRC_DIRS
"${CMAKE_CURRENT_LIST_DIR}"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated/attributes"
Expand Down Expand Up @@ -47,7 +48,8 @@ idf_component_register(PRIV_INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/ota-requestor"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/groups-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/group-key-mgmt-server"
PRIV_REQUIRES chip QRCode bt app_update)
PRIV_REQUIRES mqtt chip QRCode bt app_update json)


set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17)
target_compile_options(${COMPONENT_LIB} PRIVATE "-DLWIP_IPV6_SCOPES=0" "-DCHIP_HAVE_CONFIG_H")
Expand Down
3 changes: 3 additions & 0 deletions src/main/InteractionModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include "InteractionModel.h"
#include "esp_log.h"

using namespace chip;
using namespace chip::app;
Expand Down Expand Up @@ -100,11 +101,13 @@ void InteractionModel::Shutdown()
/////////// ReadClient Callback Interface /////////
void InteractionModel::OnAttributeData(const ConcreteDataAttributePath & path, TLV::TLVReader * data, const StatusIB & status)
{
ESP_LOGE("ATTRIBUTE DATA", "EndpointId: %x", path.mEndpointId);
OnResponse(status, data);
}

void InteractionModel::OnEventData(const EventHeader & eventHeader, TLV::TLVReader * data, const StatusIB * status)
{
ESP_LOGE("ATTRIBUTE DATA", "Event Data");
OnResponse(status == nullptr ? StatusIB() : *status, data);
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,11 @@ menu "Demo"
default 8 if RENDEZVOUS_MODE_ETHERNET

endmenu
menu "MQTT Configuration"
config MQTT_CLIENT_URI
string "MQTT URI"
config MQTT_CLIENT_USERNAME
string "MQTT Username"
config MQTT_CLIENT_PASSWORD
string "MQTT Password"
endmenu
vringar marked this conversation as resolved.
Show resolved Hide resolved
141 changes: 141 additions & 0 deletions src/main/MQTTCommands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Published under MIT License
* Copyright (c) 2022 OpenFogStack
* Based on code written by Project CHIP Authors, which was published under Apache License, Version 2.0 (c) 2022
*/
#include "MQTTCommands.h"
#include "MQTTManager.h"
#include "app/server/Server.h"
#include <lib/support/CodeUtils.h>

#if CONFIG_ENABLE_CHIP_SHELL
#include "lib/shell/Engine.h"
#include "lib/shell/commands/Help.h"
#endif // ENABLE_CHIP_SHELL

using namespace chip;
using namespace chip::app;

#if CONFIG_ENABLE_CHIP_SHELL
using Shell::Engine;
using Shell::shell_command_t;
using Shell::streamer_get;
using Shell::streamer_printf;

Engine sShellMQTTSubCommands;

#endif // defined(ENABLE_CHIP_SHELL)

static const char * TAG = "MQTT Commands";
namespace shell {

#ifdef CONFIG_ENABLE_CHIP_SHELL

/********************************************************
* MQTT shell functions
*********************************************************/

CHIP_ERROR MQTTHelpHandler(int argc, char ** argv)
{
sShellMQTTSubCommands.ForEachCommand(Shell::PrintCommandHelp, nullptr);
return CHIP_NO_ERROR;
}

CHIP_ERROR MQTTCommandHandler(int argc, char ** argv)
{
if (argc == 0)
{
return MQTTHelpHandler(argc, argv);
}

return sShellMQTTSubCommands.ExecCommand(argc, argv);
}

/********************************************************
* MQTT sub and pub shell functions
*********************************************************/

CHIP_ERROR MQTTSubCommandHelpHandler(int argc, char ** argv)
{
sShellMQTTSubCommands.ForEachCommand(Shell::PrintCommandHelp, nullptr);
return CHIP_NO_ERROR;
}

CHIP_ERROR MQTTSubHandler(int argc, char ** argv)
{
if (argc != 1)
{
return MQTTSubCommandHelpHandler(argc, argv);
}
MQTTCommandData * data = Platform::New<MQTTCommandData>();
data->topic = argv[0];
data->data = NULL;
data->task = MQTTCommandTask::subscribe;
DeviceLayer::PlatformMgr().ScheduleWork(MQTTCommandWorkerFunction, reinterpret_cast<intptr_t>(data));
return CHIP_NO_ERROR;
}

CHIP_ERROR MQTTUnsubHandler(int argc, char ** argv)
{
if (argc != 1)
{
return MQTTSubCommandHelpHandler(argc, argv);
}
MQTTCommandData * data = Platform::New<MQTTCommandData>();
data->topic = argv[0];
data->data = NULL;
data->task = MQTTCommandTask::unsubscribe;
DeviceLayer::PlatformMgr().ScheduleWork(MQTTCommandWorkerFunction, reinterpret_cast<intptr_t>(data));
return CHIP_NO_ERROR;
}

CHIP_ERROR MQTTPubHandler(int argc, char ** argv)
{
if (argc != 2)
{
return MQTTSubCommandHelpHandler(argc, argv);
}
MQTTCommandData * data = Platform::New<MQTTCommandData>();
data->topic = argv[0];
data->data = argv[1];
data->task = MQTTCommandTask::publish;
DeviceLayer::PlatformMgr().ScheduleWork(MQTTCommandWorkerFunction, reinterpret_cast<intptr_t>(data));
return CHIP_NO_ERROR;
}

#endif // ENABLE_CHIP_SHELL

} // namespace shell

namespace shell {
void RegisterMQTTCommands()
{
using namespace shell;
static const shell_command_t sMQTTSubCommands[] = { { &MQTTSubCommandHelpHandler, "help", "Usage: mqtt <subcommand>" },
{ &MQTTSubHandler, "sub", " Usage: cluster sub <topic>" },
{ &MQTTUnsubHandler, "unsub", " Usage: cluster unsub <topic>" },
{ &MQTTPubHandler, "pub", " Usage: cluster pub <topic> <data>" } };

static const shell_command_t sMQTTCommand = { &MQTTCommandHandler, "mqtt", "MQTT commands. Usage: mqtt <subcommand>" };

sShellMQTTSubCommands.RegisterCommands(sMQTTSubCommands, ArraySize(sMQTTSubCommands));

Engine::Root().RegisterCommands(&sMQTTCommand, 1);
}

void MQTTCommandWorkerFunction(intptr_t context)
{
if (context == 0)
{
ChipLogError(NotSpecified, "MQTTCommandWorkerFunction - Invalid work data");
return;
}
shell::MQTTCommandData * data = reinterpret_cast<shell::MQTTCommandData *>(context);
ESP_LOGI(TAG, " - Topic: '%s'", data->topic);
if (data->data)
{
ESP_LOGI(TAG, " - Data: '%s'", data->data);
}
MQTTManager::GetInstance().ProcessCommand(data);
}
} // End namespace shell
Loading