start mqtt stuff

This commit is contained in:
Wolfgang Hottgenroth 2020-11-10 15:56:07 +01:00
parent 8a36a3da82
commit 662a440179
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
6 changed files with 129 additions and 3 deletions

View File

@ -37,7 +37,7 @@ BUILD_DIR = build
###################################### ######################################
# C sources # C sources
C_SOURCES = \ C_SOURCES = \
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/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 \
@ -125,6 +125,7 @@ C_INCLUDES = \
-IUser/Inc \ -IUser/Inc \
-IioLibrary_Driver/Ethernet \ -IioLibrary_Driver/Ethernet \
-IioLibrary_Driver/Internet/DHCP \ -IioLibrary_Driver/Internet/DHCP \
-IioLibrary_Driver/Internet/MQTT \
-ICore/Inc \ -ICore/Inc \
-IDrivers/STM32F1xx_HAL_Driver/Inc \ -IDrivers/STM32F1xx_HAL_Driver/Inc \
-IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \ -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \

9
cube/User/Inc/mqttComm.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef _MQTT_COMM_H_
#define _MQTT_COMM_H_
// do not call it periodically, just call it with a NULL handle
void mqttCommInit(void *handle);
#endif /* _MQTT_COMM_H_ */

View File

@ -18,6 +18,9 @@
#include <eeprom.h> #include <eeprom.h>
#include <wizHelper.h> #include <wizHelper.h>
#include <mqtt_interface.h>
#include <mqttComm.h>
void my_setup_1() { void my_setup_1() {
schInit(); schInit();
logInit(); logInit();
@ -190,6 +193,8 @@ void my_setup_2() {
wizInit(); wizInit();
mqttCommInit(NULL);
// frontendInit(); // frontendInit();
// frontendSetThreshold(240); // frontendSetThreshold(240);
@ -205,7 +210,11 @@ void my_loop() {
} }
void SYSTICK_Callback() { void SYSTICK_Callback() {
// Pont Scheduler
schUpdate(); schUpdate();
// MQTT Interface
MilliTimer_Handler()
} }
void HAL_GPIO_EXTI_Callback(uint16_t pin) { void HAL_GPIO_EXTI_Callback(uint16_t pin) {

99
cube/User/Src/mqttComm.c Normal file
View File

@ -0,0 +1,99 @@
#include <mqttComm.h>
#include <logger.h>
#include <PontCoopScheduler.h>
#include <wizHelper.h>
#include <stdint.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;
static uint8_t targetIP[4] = { 172, 16, 2, 16 };
static uint16_t targetPort = 1883;
struct opts_struct
{
char* clientid;
int nodelimiter;
char* delimiter;
enum QoS qos;
char* username;
char* password;
char* host;
int port;
int showtopics;
} opts ={ (char*)"stdout-subscriber", 0, (char*)"\n", QOS0, NULL, NULL, targetIP, targetPort, 0 };
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
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)
logMsg("%.*s", (int)message->payloadlen, (char*)message->payload);
else
logMsg("%.*s%s", (int)message->payloadlen, (char*)message->payload, opts.delimiter);
}
static void mqttHandler(void *handle) {
MQTTYield(&mqttClient, data.keepAliveInterval);
}
void mqttCommInit(void *handle) {
if (! isNetworkAvailable()) {
coloredMsg(LOG_RED, "mqci, can not start mqtt yet, network unavailable, try again in a second");
schAdd(mqttCommInit, NULL, 1000, 0);
} else {
coloredMsg(LOG_RED, "mqci, starting mqtt");
Network n;
NewNetwork(&n, MQTT_SOCK);
ConnectNetwork(&n, targetIP, targetPort);
MQTTClientInit(&mqttClient, &n, 1000, txBuffer, TX_BUFFER_SIZE, rxBuffer, RX_BUFFER_SIZE);
data.willFlag = 0;
data.MQTTVersion = 3;
data.clientID.cstring = opts.clientid;
data.username.cstring = opts.username;
data.password.cstring = opts.password;
data.keepAliveInterval = 60;
data.cleansession = 1;
int rc = MQTTConnect(&mqttClient, &data);
printf("Connected %d\r\n", rc);
opts.showtopics = 1;
printf("Subscribing to %s\r\n", "hello/wiznet");
rc = MQTTSubscribe(&mqttClient, "hello/wiznet", opts.qos, messageArrived);
printf("Subscribed %d\r\n", rc);
schAdd(mqttHandler, NULL, 0, 10);
}
}

8
cube/User/Src/ports.c Normal file
View File

@ -0,0 +1,8 @@
#include <stdint.h>
// on the W5500 there are eight ports available
const uint8_t DHCP_SOCK = 0;
const uint8_t MQTT_SOCK = 1;

View File

@ -20,7 +20,7 @@ wiz_NetInfo netInfo = {
#define DHCP_BUFFER_SIZE 2048 #define DHCP_BUFFER_SIZE 2048
static uint8_t dhcpBuffer[DHCP_BUFFER_SIZE]; static uint8_t dhcpBuffer[DHCP_BUFFER_SIZE];
const uint8_t DHCP_SOCK = 0; extern const uint8_t DHCP_SOCK;
static bool networkAvailable = false; static bool networkAvailable = false;
@ -75,7 +75,7 @@ static void wizDHCPAssign() {
coloredMsg(LOG_GREEN, "wizda, DNS: %d.%d.%d.%d", netInfo.dns[0], netInfo.dns[1], netInfo.dns[2], netInfo.dns[3]); coloredMsg(LOG_GREEN, "wizda, DNS: %d.%d.%d.%d", netInfo.dns[0], netInfo.dns[1], netInfo.dns[2], netInfo.dns[3]);
wizchip_setnetinfo(&netInfo); wizchip_setnetinfo(&netInfo);
coloredMsg(LOG_RED, "wizda, set netinfo again"); coloredMsg(LOG_GREEN, "wizda, set netinfo again");
networkAvailable = true; networkAvailable = true;
coloredMsg(LOG_GREEN, "wizda, network is available"); coloredMsg(LOG_GREEN, "wizda, network is available");