ESP32-PaxCounter/src/mqttclient.cpp

158 lines
4.7 KiB
C++
Raw Normal View History

2020-05-16 23:49:34 +02:00
#ifdef HAS_MQTT
#include "mqttclient.h"
static const char TAG[] = __FILE__;
QueueHandle_t MQTTSendQueue;
TaskHandle_t mqttTask;
2020-06-16 20:29:49 +02:00
Ticker mqttTimer;
2020-06-20 15:04:03 +02:00
WiFiClient netClient;
MQTTClient mqttClient;
2020-06-16 20:29:49 +02:00
2020-12-23 16:31:31 +01:00
void mqtt_deinit(void) {
2020-12-31 17:47:48 +01:00
mqttClient.unsubscribe(MQTT_INTOPIC);
2020-12-23 16:31:31 +01:00
mqttClient.onMessageAdvanced(NULL);
mqttClient.disconnect();
vTaskDelete(mqttTask);
}
2020-06-20 15:04:03 +02:00
esp_err_t mqtt_init(void) {
2020-12-26 23:36:56 +01:00
// setup network connection and MQTT client
2020-06-20 15:04:03 +02:00
ETH.begin();
mqttClient.begin(MQTT_SERVER, MQTT_PORT, netClient);
2020-12-31 17:47:48 +01:00
mqttClient.setKeepAlive(MQTT_KEEPALIVE);
2020-06-20 15:04:03 +02:00
mqttClient.onMessageAdvanced(mqtt_callback);
2020-10-30 12:24:16 +01:00
_ASSERT(SEND_QUEUE_SIZE > 0);
2020-06-20 15:04:03 +02:00
MQTTSendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t));
if (MQTTSendQueue == 0) {
ESP_LOGE(TAG, "Could not create MQTT send queue. Aborting.");
return ESP_FAIL;
}
ESP_LOGI(TAG, "MQTT send queue created, size %d Bytes",
SEND_QUEUE_SIZE * PAYLOAD_BUFFER_SIZE);
ESP_LOGI(TAG, "Starting MQTTloop...");
2020-12-26 23:36:56 +01:00
xTaskCreatePinnedToCore(mqtt_client_task, "mqttloop", 4096, (void *)NULL, 1,
&mqttTask, 1);
2020-06-20 15:04:03 +02:00
return ESP_OK;
}
int mqtt_connect(const char *my_host, const uint16_t my_port) {
IPAddress mqtt_server_ip;
2020-12-27 13:09:10 +01:00
uint8_t mac[6];
2020-12-28 00:03:37 +01:00
char clientId[20];
2020-12-27 13:09:10 +01:00
2020-12-28 00:03:37 +01:00
// hash 6 byte MAC to 4 byte hash
2020-12-27 13:09:10 +01:00
esp_eth_get_mac(mac);
2020-12-28 00:03:37 +01:00
const uint32_t hashedmac = hash((const char *)mac, 6);
snprintf(clientId, 20, "paxcounter_%08x", hashedmac);
2020-12-27 00:40:07 +01:00
2020-06-20 15:04:03 +02:00
ESP_LOGI(TAG, "MQTT name is %s", MQTT_CLIENTNAME);
// resolve server host name
if (WiFi.hostByName(my_host, mqtt_server_ip)) {
ESP_LOGI(TAG, "Attempting to connect to %s [%s]", my_host,
mqtt_server_ip.toString().c_str());
} else {
ESP_LOGI(TAG, "Could not resolve %s", my_host);
return -1;
}
if (mqttClient.connect(MQTT_CLIENTNAME, MQTT_USER, MQTT_PASSWD)) {
ESP_LOGI(TAG, "MQTT server connected, subscribing...");
mqttClient.publish(MQTT_OUTTOPIC, MQTT_CLIENTNAME);
// Clear retained messages that may have been published earlier on topic
mqttClient.publish(MQTT_INTOPIC, "", true, 1);
mqttClient.subscribe(MQTT_INTOPIC);
ESP_LOGI(TAG, "MQTT topic subscribed");
} else {
ESP_LOGD(TAG, "MQTT last_error = %d / rc = %d", mqttClient.lastError(),
mqttClient.returnCode());
ESP_LOGW(TAG, "MQTT server not responding, retrying later");
return -1;
}
2020-10-10 00:49:55 +02:00
return 0;
2020-06-20 15:04:03 +02:00
}
2020-05-16 23:49:34 +02:00
void mqtt_client_task(void *param) {
2020-05-17 23:07:38 +02:00
MessageBuffer_t msg;
2020-05-16 23:49:34 +02:00
while (1) {
2020-05-17 23:07:38 +02:00
if (mqttClient.connected()) {
2020-06-20 15:04:03 +02:00
2020-12-23 16:31:31 +01:00
// check for incoming messages
mqttClient.loop();
// fetch next or wait for payload to send from queue
// do not delete item from queue until it is transmitted
// consider mqtt timeout while waiting
if (xQueuePeek(MQTTSendQueue, &msg,
MQTT_KEEPALIVE * 1000 / portTICK_PERIOD_MS) != pdTRUE)
continue;
2020-12-29 20:48:32 +01:00
// prepare mqtt topic
char topic[16];
snprintf(topic, 16, "%s/%u", MQTT_OUTTOPIC, msg.MessagePort);
2020-12-30 22:25:37 +01:00
size_t out_len = 0;
2020-06-20 15:04:03 +02:00
2020-12-30 22:25:37 +01:00
// 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);
2020-12-09 10:15:12 +01:00
xQueueReceive(MQTTSendQueue, &msg, (TickType_t)0);
} else
ESP_LOGD(TAG, "Couldn't sent message to MQTT server");
2020-05-22 00:07:49 +02:00
} else {
2020-05-16 23:49:34 +02:00
// attempt to reconnect to MQTT server
2020-12-23 16:31:31 +01:00
ESP_LOGD(TAG, "MQTT client reconnecting...");
2020-05-22 00:07:49 +02:00
delay(MQTT_RETRYSEC * 1000);
2020-05-20 18:29:07 +02:00
mqtt_connect(MQTT_SERVER, MQTT_PORT);
2020-05-16 23:49:34 +02:00
}
2020-12-23 16:31:31 +01:00
} // while (1)
2020-05-16 23:49:34 +02:00
}
2020-12-30 22:25:37 +01:00
// 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);
}
}
2020-12-26 23:36:56 +01:00
// enqueue outgoing messages in MQTT send queue
2020-05-16 23:49:34 +02:00
void mqtt_enqueuedata(MessageBuffer_t *message) {
if (xQueueSendToBack(MQTTSendQueue, (void *)message, (TickType_t)0) != pdTRUE)
2020-05-16 23:49:34 +02:00
ESP_LOGW(TAG, "MQTT sendqueue is full");
}
2020-05-22 00:07:49 +02:00
void mqtt_queuereset(void) { xQueueReset(MQTTSendQueue); }
2020-12-09 10:15:12 +01:00
uint32_t mqtt_queuewaiting(void) {
2020-12-10 15:09:24 +01:00
return uxQueueMessagesWaiting(MQTTSendQueue);
2020-12-09 10:15:12 +01:00
}
2020-05-16 23:49:34 +02:00
#endif // HAS_MQTT