MQTT client rcommand completion

This commit is contained in:
cyberman54 2020-12-30 22:25:37 +01:00
parent 5d8cec547c
commit 98f83d9679
3 changed files with 35 additions and 16 deletions

View File

@ -50,6 +50,9 @@ LoLin32lite + [LoraNode32-Lite shield](https://github.com/hallard/LoLin32-Lite-L
- Generic ESP32
Depending on board hardware following features are supported:
- LoRaWAN communication, supporting various payload formats (see enclosed .js converters)
- MQTT communication via TCP/IP and Ethernet interface (note: payload transmitted over MQTT will be base64 encoded)
- SPI serial communication to a local host
- LED (shows power & status)
- OLED Display (shows detailed status)
- RGB LED (shows colorized status)
@ -62,7 +65,6 @@ Depending on board hardware following features are supported:
- Switch external power / battery
- LED Matrix display (similar to [this 64x16 model](https://www.instructables.com/id/64x16-RED-LED-Marquee/), can be ordered on [Aliexpress](https://www.aliexpress.com/item/P3-75-dot-matrix-led-module-3-75mm-high-clear-top1-for-text-display-304-60mm/32616683948.html))
- SD-card (see section SD-card here) for logging pax data
- Ethernet interface for MQTT communication via TCP/IP
Target platform must be selected in `platformio.ini`.<br>
Hardware dependent settings (pinout etc.) are stored in board files in /hal directory. If you want to use a ESP32 board which is not yet supported, use hal file generic.h and tailor pin mappings to your needs. Pull requests for new boards welcome.<br>

View File

@ -5,6 +5,7 @@
#include "rcommand.h"
#include <MQTT.h>
#include <ETH.h>
#include <mbedtls/base64.h>
#ifndef MQTT_CLIENTNAME
#define MQTT_CLIENTNAME clientId
@ -17,8 +18,7 @@ uint32_t mqtt_queuewaiting(void);
void mqtt_queuereset(void);
void mqtt_client_task(void *param);
int mqtt_connect(const char *my_host, const uint16_t my_port);
void mqtt_callback(MQTTClient *client, char topic[], char payload[],
int length);
void mqtt_callback(MQTTClient *client, char *topic, char *payload, int length);
void NetworkEvent(WiFiEvent_t event);
esp_err_t mqtt_init(void);
void mqtt_deinit(void);

View File

@ -1,7 +1,6 @@
#ifdef HAS_MQTT
#include "mqttclient.h"
#include <base64.h>
static const char TAG[] = __FILE__;
@ -99,12 +98,20 @@ void mqtt_client_task(void *param) {
// prepare mqtt topic
char topic[16];
snprintf(topic, 16, "%s/%u", MQTT_OUTTOPIC, msg.MessagePort);
size_t out_len = 0;
// send base64 encoded message to mqtt server and delete it from queue
if (mqttClient.publish(topic,
base64::encode(msg.Message, msg.MessageSize))) {
ESP_LOGD(TAG, "%s/%s sent to MQTT server", topic,
base64::encode(msg.Message, msg.MessageSize));
// get length of base64 encoded message
mbedtls_base64_encode(NULL, 0, &out_len, (unsigned char *)msg.Message,
msg.MessageSize);
// base64 encode the message
unsigned char encoded[out_len];
mbedtls_base64_encode(encoded, out_len, &out_len,
(unsigned char *)msg.Message, msg.MessageSize);
// send encoded message to mqtt server and delete it from queue
if (mqttClient.publish(topic, (const char *)encoded, out_len)) {
ESP_LOGD(TAG, "%u bytes sent to MQTT server", out_len);
xQueueReceive(MQTTSendQueue, &msg, (TickType_t)0);
} else
ESP_LOGD(TAG, "Couldn't sent message to MQTT server");
@ -117,19 +124,29 @@ void mqtt_client_task(void *param) {
} // while (1)
}
// process incoming MQTT messages
void mqtt_callback(MQTTClient *client, char *topic, char *payload, int length) {
if (strcmp(topic, MQTT_INTOPIC) == 0) {
// get length of base64 encoded message
size_t out_len = 0;
mbedtls_base64_decode(NULL, 0, &out_len, (unsigned char *)payload, length);
// decode the base64 message
unsigned char decoded[out_len];
mbedtls_base64_decode(decoded, out_len, &out_len, (unsigned char *)payload,
length);
rcommand(decoded, out_len);
}
}
// enqueue outgoing messages in MQTT send queue
void mqtt_enqueuedata(MessageBuffer_t *message) {
if (xQueueSendToBack(MQTTSendQueue, (void *)message, (TickType_t)0) != pdTRUE)
ESP_LOGW(TAG, "MQTT sendqueue is full");
}
// process incoming MQTT messages
void mqtt_callback(MQTTClient *client, char topic[], char payload[],
int length) {
if (strcmp(topic, MQTT_INTOPIC) == 0)
rcommand((const uint8_t *)payload, (const uint8_t)length);
}
void mqtt_queuereset(void) { xQueueReset(MQTTSendQueue); }
uint32_t mqtt_queuewaiting(void) {