mqtt handling
This commit is contained in:
parent
e921ab59e3
commit
fc5e6ab151
@ -37,7 +37,7 @@ BUILD_DIR = build
|
|||||||
######################################
|
######################################
|
||||||
# C sources
|
# C sources
|
||||||
C_SOURCES = \
|
C_SOURCES = \
|
||||||
User/Src/mqttTest.c User/Src/tcpTest.c User/Src/ports.c User/Src/eeprom.c User/Src/frontend.c User/Src/logger.c User/Src/loopCtrl.c User/Src/main2.c User/Src/mbusComm.c User/Src/ringbuffer.c User/Src/show.c User/Src/utils.c User/Src/wizHelper.c hottislib/PontCoopScheduler.c \
|
User/Src/mqttComm.c User/Src/tcpTest.c User/Src/ports.c User/Src/eeprom.c User/Src/frontend.c User/Src/logger.c User/Src/loopCtrl.c User/Src/main2.c User/Src/mbusComm.c User/Src/ringbuffer.c User/Src/show.c User/Src/utils.c User/Src/wizHelper.c hottislib/PontCoopScheduler.c \
|
||||||
libmbus/mbus/mbus-protocol.c \
|
libmbus/mbus/mbus-protocol.c \
|
||||||
Core/Src/main.c \
|
Core/Src/main.c \
|
||||||
Core/Src/gpio.c \
|
Core/Src/gpio.c \
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
#ifndef _MQTT_COMM_H_
|
#ifndef _MQTTCOMM_H_
|
||||||
#define _MQTT_COMM_H_
|
#define _MQTTCOMM_H_
|
||||||
|
|
||||||
|
|
||||||
// do not call it periodically, just call it with a NULL handle
|
void mqttCommInit();
|
||||||
void mqttCommInit(void *handle);
|
|
||||||
|
|
||||||
|
#endif // _MQTTCOMM_H_
|
||||||
|
|
||||||
|
|
||||||
#endif /* _MQTT_COMM_H_ */
|
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
#ifndef _MQTTTEST_H_
|
|
||||||
#define _MQTTTEST_H_
|
|
||||||
|
|
||||||
|
|
||||||
void mqttTestInit();
|
|
||||||
|
|
||||||
|
|
||||||
#endif // _MQTTTEST_H_
|
|
||||||
|
|
||||||
|
|
@ -17,8 +17,7 @@
|
|||||||
#include <frontend.h>
|
#include <frontend.h>
|
||||||
#include <eeprom.h>
|
#include <eeprom.h>
|
||||||
#include <wizHelper.h>
|
#include <wizHelper.h>
|
||||||
#include <tcpTest.h>
|
#include <mqttComm.h>
|
||||||
#include <mqttTest.h>
|
|
||||||
|
|
||||||
|
|
||||||
void my_setup_1() {
|
void my_setup_1() {
|
||||||
@ -193,14 +192,14 @@ void my_setup_2() {
|
|||||||
|
|
||||||
wizInit();
|
wizInit();
|
||||||
|
|
||||||
mqttTestInit();
|
mqttCommInit();
|
||||||
|
|
||||||
|
|
||||||
// frontendInit();
|
frontendInit();
|
||||||
// frontendSetThreshold(240);
|
frontendSetThreshold(240);
|
||||||
|
|
||||||
// schAdd(scheduleMBusRequest, NULL, 0, 1000);
|
schAdd(scheduleMBusRequest, NULL, 0, 1000);
|
||||||
// schAdd(triggerMBusRequest, NULL, 0, 100);
|
schAdd(triggerMBusRequest, NULL, 0, 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
void my_loop() {
|
void my_loop() {
|
||||||
|
@ -1,104 +1,120 @@
|
|||||||
#include <mqttComm.h>
|
#include <mqttComm.h>
|
||||||
#include <logger.h>
|
#include <logger.h>
|
||||||
|
#include <main.h>
|
||||||
|
|
||||||
|
#include <pubsubc.h>
|
||||||
|
#include <platformAdaption.h>
|
||||||
#include <PontCoopScheduler.h>
|
#include <PontCoopScheduler.h>
|
||||||
#include <wizHelper.h>
|
#include <wizHelper.h>
|
||||||
#include <show.h>
|
#include <mbusComm.h>
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#include <mqtt_interface.h>
|
|
||||||
#include <MQTTClient.h>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define RX_BUFFER_SIZE 2048
|
|
||||||
static uint8_t rxBuffer[RX_BUFFER_SIZE];
|
|
||||||
|
|
||||||
#define TX_BUFFER_SIZE 128
|
|
||||||
static uint8_t txBuffer[TX_BUFFER_SIZE];
|
|
||||||
|
|
||||||
extern const uint8_t MQTT_SOCK;
|
extern const uint8_t MQTT_SOCK;
|
||||||
|
|
||||||
|
client_t client;
|
||||||
|
mqttClient_t mqttClient;
|
||||||
|
|
||||||
|
uint8_t brokerAddress[] = { 172, 16, 2, 16 };
|
||||||
|
uint16_t brokerPort = 1883;
|
||||||
|
|
||||||
static uint8_t targetIP[4] = { 172, 16, 2, 16 };
|
const static char WatchdogTopic[] = "IoT/Watchdog";
|
||||||
static uint16_t targetPort = 1883;
|
const static char StartupTopic[] = "IoT/MBGW3/Startup";
|
||||||
|
const static char StatusTopic[] = "IoT/MBGW3/Status";
|
||||||
|
|
||||||
struct opts_struct
|
// typedef void (*callback_t)(char*, uint8_t*, uint16_t);
|
||||||
{
|
static void mqttCallback(char *topic, uint8_t *payload, uint16_t payloadLength) {
|
||||||
char* clientid;
|
logMsg("mqcb: %s : %.*s", topic, payloadLength, payload);
|
||||||
int nodelimiter;
|
|
||||||
char* delimiter;
|
|
||||||
enum QoS qos;
|
|
||||||
char* username;
|
|
||||||
char* password;
|
|
||||||
uint8_t *host;
|
|
||||||
uint16_t port;
|
|
||||||
int showtopics;
|
|
||||||
} opts ={ (char*)"stdout-subscriber", 0, (char*)"\n", QOS0, NULL, NULL, NULL, 0, 0 };
|
|
||||||
|
|
||||||
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
|
if (0 == strcmp(topic, WatchdogTopic)) {
|
||||||
|
coloredMsg(LOG_GREEN, "mqcb: watchdog message received");
|
||||||
MQTTClient mqttClient;
|
}
|
||||||
|
|
||||||
|
|
||||||
static void messageArrived(MessageData* md)
|
|
||||||
{
|
|
||||||
unsigned char testbuffer[100];
|
|
||||||
MQTTMessage* message = md->message;
|
|
||||||
|
|
||||||
if (opts.showtopics)
|
|
||||||
{
|
|
||||||
memcpy(testbuffer,(char*)message->payload,(int)message->payloadlen);
|
|
||||||
*(testbuffer + (int)message->payloadlen + 1) = "\n";
|
|
||||||
logMsg("%s\r\n",testbuffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts.nodelimiter)
|
static void mqttStatusPublisher(void *handle) {
|
||||||
logMsg("%.*s", (int)message->payloadlen, (char*)message->payload);
|
logMsg("mqsp: publishing status");
|
||||||
else
|
t_mbusCommStats *mbusCommStats = mbusCommGetStats();
|
||||||
logMsg("%.*s%s", (int)message->payloadlen, (char*)message->payload, opts.delimiter);
|
char buf[64];
|
||||||
}
|
snprintf(buf, 64, "{\"uptime\":\"%ld\", \"requests\":\"%d\", \"errors\":\"%d\"}",
|
||||||
|
HAL_GetTick()/1000, mbusCommStats->requestCnt, mbusCommStats->errorCnt);
|
||||||
static void mqttHandler(void *handle) {
|
bool res = publish(&mqttClient, StatusTopic, (const char*)buf, strlen(buf), false);
|
||||||
show(DEBUG_2, ON);
|
coloredMsg(LOG_YELLOW, "mqch, publish returned %d", res);
|
||||||
MQTTYield(&mqttClient, data.keepAliveInterval);
|
|
||||||
show(DEBUG_2, OFF);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void mqttCommInit(void *handle) {
|
void mqttCommHandler(void *handle) {
|
||||||
if (! isNetworkAvailable()) {
|
static uint8_t state = 0;
|
||||||
coloredMsg(LOG_RED, "mqci, can not start mqtt yet, network unavailable, try again in a second");
|
static uint8_t message[] = "MeterbusGateway3Variant starting";
|
||||||
schAdd(mqttCommInit, NULL, 1000, 0);
|
|
||||||
|
if (isNetworkAvailable()) {
|
||||||
|
switch (state) {
|
||||||
|
case 0:
|
||||||
|
coloredMsg(LOG_YELLOW, "mqch, initializing mqtt client");
|
||||||
|
client.sockNum = MQTT_SOCK;
|
||||||
|
mqttClientInit(&mqttClient, &client, mqttCallback);
|
||||||
|
coloredMsg(LOG_YELLOW, "mqch: mqtt client initialized");
|
||||||
|
|
||||||
|
state = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
coloredMsg(LOG_YELLOW, "mqch, connecting to broker ");
|
||||||
|
bool res = mqttConnect(&mqttClient, brokerAddress, 1883, "mbv3gw-client", NULL, NULL, NULL, 0, false, NULL, false);
|
||||||
|
coloredMsg(LOG_YELLOW, "mqch, mqttConnect returns %d", res);
|
||||||
|
|
||||||
|
if (res) {
|
||||||
|
coloredMsg(LOG_YELLOW, "mqch, ok, connected");
|
||||||
|
state = 2;
|
||||||
} else {
|
} else {
|
||||||
coloredMsg(LOG_RED, "mqci, starting mqtt");
|
state = 255;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
Network n;
|
case 2:
|
||||||
|
coloredMsg(LOG_YELLOW, "mqch, publish start-up");
|
||||||
|
res = publish(&mqttClient, StartupTopic, (const char*)message, strlen(message), false);
|
||||||
|
coloredMsg(LOG_YELLOW, "mqch, publish returned %d", res);
|
||||||
|
schAdd(mqttStatusPublisher, NULL, 0, 60000);
|
||||||
|
coloredMsg(LOG_YELLOW, "mqch, status publisher scheduled");
|
||||||
|
state = 3;
|
||||||
|
break;
|
||||||
|
|
||||||
NewNetwork(&n, MQTT_SOCK);
|
case 3:
|
||||||
ConnectNetwork(&n, targetIP, targetPort);
|
coloredMsg(LOG_YELLOW, "mqch, subscribe watchdog");
|
||||||
MQTTClientInit(&mqttClient, &n, 1000, txBuffer, TX_BUFFER_SIZE, rxBuffer, RX_BUFFER_SIZE);
|
res = subscribe(&mqttClient, WatchdogTopic, MQTTQOS0);
|
||||||
|
coloredMsg(LOG_YELLOW, "mqch, subscribe returned %d", res);
|
||||||
|
state = 4;
|
||||||
|
break;
|
||||||
|
|
||||||
data.willFlag = 0;
|
case 4:
|
||||||
data.MQTTVersion = 3;
|
coloredMsg(LOG_YELLOW, "mqch, now entering the loop");
|
||||||
data.clientID.cstring = opts.clientid;
|
state = 5;
|
||||||
data.username.cstring = opts.username;
|
break;
|
||||||
data.password.cstring = opts.password;
|
|
||||||
|
|
||||||
data.keepAliveInterval = 60;
|
case 5:
|
||||||
data.cleansession = 1;
|
// coloredMsg(LOG_YELLOW, "mqch, looping");
|
||||||
|
if (! mqttLoop(&mqttClient)) {
|
||||||
|
state = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
int rc = MQTTConnect(&mqttClient, &data);
|
case 255:
|
||||||
logMsg("Connected %d\r\n", rc);
|
coloredMsg(LOG_YELLOW, "mqch, error state, will stop here");
|
||||||
|
schDel(mqttCommHandler, NULL);
|
||||||
opts.showtopics = 1;
|
coloredMsg(LOG_YELLOW, "mqch, trying again in one minute");
|
||||||
|
schAdd(mqttCommHandler, NULL, 60000, 100);
|
||||||
logMsg("Subscribing to %s\r\n", "hello/wiznet");
|
break;
|
||||||
rc = MQTTSubscribe(&mqttClient, "hello/wiznet", opts.qos, messageArrived);
|
}
|
||||||
logMsg("Subscribed %d\r\n", rc);
|
} else {
|
||||||
|
coloredMsg(LOG_YELLOW, "mqch, network not yet ready");
|
||||||
schAdd(mqttHandler, NULL, 0, 100);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void mqttCommInit() {
|
||||||
|
schAdd(mqttCommHandler, NULL, 0, 100);
|
||||||
|
}
|
@ -1,96 +0,0 @@
|
|||||||
#include <mqttTest.h>
|
|
||||||
#include <logger.h>
|
|
||||||
|
|
||||||
#include <pubsubc.h>
|
|
||||||
#include <platformAdaption.h>
|
|
||||||
#include <PontCoopScheduler.h>
|
|
||||||
#include <wizHelper.h>
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
|
|
||||||
extern const uint8_t MQTT_SOCK;
|
|
||||||
|
|
||||||
client_t client;
|
|
||||||
mqttClient_t mqttClient;
|
|
||||||
|
|
||||||
uint8_t brokerAddress[] = { 172, 16, 2, 16 };
|
|
||||||
uint16_t brokerPort = 1883;
|
|
||||||
|
|
||||||
// typedef void (*callback_t)(char*, uint8_t*, uint16_t);
|
|
||||||
static void mqttCallback(char *topic, uint8_t *payload, uint16_t payloadLength) {
|
|
||||||
logMsg("mcb: %s : %*s", topic, payloadLength, payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mqttTestHandler(void *handle) {
|
|
||||||
static uint8_t state = 0;
|
|
||||||
static uint8_t message[] = "Hello world\n\r";
|
|
||||||
|
|
||||||
if (isNetworkAvailable()) {
|
|
||||||
switch (state) {
|
|
||||||
case 0:
|
|
||||||
coloredMsg(LOG_YELLOW, "mth, initializing mqtt client");
|
|
||||||
client.sockNum = MQTT_SOCK;
|
|
||||||
mqttClientInit(&mqttClient, &client, mqttCallback);
|
|
||||||
coloredMsg(LOG_YELLOW, "mth: mqtt client initialized");
|
|
||||||
|
|
||||||
state = 1;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 1:
|
|
||||||
coloredMsg(LOG_YELLOW, "mth, connecting to broker ");
|
|
||||||
bool res = mqttConnect(&mqttClient, brokerAddress, 1883, "mbv3gw-client", NULL, NULL, NULL, 0, false, NULL, false);
|
|
||||||
coloredMsg(LOG_YELLOW, "mth, mqttConnect returns %d", res);
|
|
||||||
|
|
||||||
if (res) {
|
|
||||||
coloredMsg(LOG_YELLOW, "mth, ok, connected");
|
|
||||||
state = 2;
|
|
||||||
} else {
|
|
||||||
state = 255;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
coloredMsg(LOG_YELLOW, "mth, publish something");
|
|
||||||
res = publish(&mqttClient, "wiznet/hello", message, strlen(message), false);
|
|
||||||
coloredMsg(LOG_YELLOW, "mth, publish returned %d", res);
|
|
||||||
state = 3;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 3:
|
|
||||||
coloredMsg(LOG_YELLOW, "mth, subscribe something");
|
|
||||||
res = subscribe(&mqttClient, "wiznet/helloback", MQTTQOS0);
|
|
||||||
coloredMsg(LOG_YELLOW, "mth, subscribe returned %d", res);
|
|
||||||
state = 4;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 4:
|
|
||||||
coloredMsg(LOG_YELLOW, "mth, now entering the loop");
|
|
||||||
state = 5;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 5:
|
|
||||||
// coloredMsg(LOG_YELLOW, "mth, looping");
|
|
||||||
if (! mqttLoop(&mqttClient)) {
|
|
||||||
state = 0;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 255:
|
|
||||||
coloredMsg(LOG_YELLOW, "mth, error state, will stop here");
|
|
||||||
schDel(mqttTestHandler, NULL);
|
|
||||||
coloredMsg(LOG_YELLOW, "mth, trying again in one minute");
|
|
||||||
schAdd(mqttTestHandler, NULL, 60000, 100);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
coloredMsg(LOG_YELLOW, "mth, network not yet ready");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void mqttTestInit() {
|
|
||||||
schAdd(mqttTestHandler, NULL, 0, 100);
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user