ESP32-PaxCounter/src/mqttclient.cpp

176 lines
5.1 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-06-20 15:04:03 +02:00
esp_err_t mqtt_init(void) {
// setup network connection
WiFi.onEvent(NetworkEvent);
ETH.begin();
2020-10-10 00:49:55 +02:00
// WiFi.mode(WIFI_STA);
// WiFi.begin("SSID", "PASSWORD");
2020-06-20 15:04:03 +02:00
// setup mqtt client
mqttClient.begin(MQTT_SERVER, MQTT_PORT, netClient);
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...");
mqttTimer.attach(MQTT_KEEPALIVE, setMqttIRQ);
2020-06-20 15:04:03 +02:00
xTaskCreate(mqtt_client_task, "mqttloop", 4096, (void *)NULL, 1, &mqttTask);
return ESP_OK;
}
int mqtt_connect(const char *my_host, const uint16_t my_port) {
IPAddress mqtt_server_ip;
2020-12-10 15:09:24 +01:00
static String clientId = "paxcounter-" + ETH.macAddress();
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
2020-05-20 12:40:37 +02:00
void NetworkEvent(WiFiEvent_t event) {
2020-05-16 23:49:34 +02:00
switch (event) {
case SYSTEM_EVENT_ETH_START:
2020-06-20 15:04:03 +02:00
case SYSTEM_EVENT_STA_START:
ESP_LOGI(TAG, "Network link layer started");
// ETH.setHostname(ETH.macAddress().c_str());
2020-05-16 23:49:34 +02:00
break;
2020-06-16 20:29:49 +02:00
case SYSTEM_EVENT_ETH_STOP:
2020-06-20 15:04:03 +02:00
case SYSTEM_EVENT_STA_STOP:
ESP_LOGI(TAG, "Network link layer stopped");
2020-06-16 20:29:49 +02:00
break;
2020-05-16 23:49:34 +02:00
case SYSTEM_EVENT_ETH_CONNECTED:
2020-06-16 20:29:49 +02:00
case SYSTEM_EVENT_STA_CONNECTED:
2020-05-17 23:07:38 +02:00
ESP_LOGI(TAG, "Network link connected");
2020-05-16 23:49:34 +02:00
break;
2020-06-16 20:29:49 +02:00
case SYSTEM_EVENT_ETH_DISCONNECTED:
case SYSTEM_EVENT_STA_DISCONNECTED:
ESP_LOGI(TAG, "Network link disconnected");
break;
2020-05-16 23:49:34 +02:00
case SYSTEM_EVENT_ETH_GOT_IP:
2020-06-20 15:04:03 +02:00
ESP_LOGI(TAG, "IP: %s", ETH.localIP().toString().c_str());
2020-05-20 18:29:07 +02:00
ESP_LOGI(TAG, "Link Speed: %d Mbps %s", ETH.linkSpeed(),
2020-05-16 23:49:34 +02:00
ETH.fullDuplex() ? "full duplex" : "half duplex");
2020-06-20 15:04:03 +02:00
mqtt_connect(MQTT_SERVER, MQTT_PORT);
break;
2020-06-16 20:29:49 +02:00
case SYSTEM_EVENT_STA_GOT_IP:
2020-06-20 15:04:03 +02:00
ESP_LOGI(TAG, "IP: %s", WiFi.localIP().toString().c_str());
2020-05-20 18:29:07 +02:00
mqtt_connect(MQTT_SERVER, MQTT_PORT);
2020-05-16 23:49:34 +02:00
break;
2020-06-16 20:29:49 +02:00
2020-05-16 23:49:34 +02:00
default:
break;
}
}
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) {
// fetch next or wait for payload to send from queue
2020-12-09 10:15:12 +01:00
// do not delete item from queue until it is transmitted
if (xQueuePeek(MQTTSendQueue, &msg, portMAX_DELAY) != pdTRUE) {
2020-05-16 23:49:34 +02:00
ESP_LOGE(TAG, "Premature return from xQueueReceive() with no data!");
continue;
}
2020-05-21 19:42:42 +02:00
// send data to mqtt server, if we are connected
2020-05-17 23:07:38 +02:00
if (mqttClient.connected()) {
2020-06-20 15:04:03 +02:00
char buffer[PAYLOAD_BUFFER_SIZE + 3];
2020-10-31 12:50:48 +01:00
snprintf(buffer, msg.MessageSize + 3, "%u/%s", msg.MessagePort,
2020-06-20 15:04:03 +02:00
msg.Message);
if (mqttClient.publish(MQTT_OUTTOPIC, buffer)) {
2020-06-16 20:29:49 +02:00
ESP_LOGI(TAG, "%d byte(s) sent to MQTT server", msg.MessageSize + 2);
2020-12-09 10:15:12 +01:00
// delete sent item from queue
xQueueReceive(MQTTSendQueue, &msg, (TickType_t)0);
2020-06-16 20:29:49 +02:00
continue;
2020-12-09 10:15:12 +01:00
} else
ESP_LOGD(TAG, "Couldn't sent message to MQTT server");
2020-05-21 19:42:42 +02:00
2020-05-22 00:07:49 +02:00
} else {
2020-05-16 23:49:34 +02:00
// attempt to reconnect to MQTT server
2020-06-16 20:29:49 +02:00
ESP_LOGD(TAG, "MQTT client reconnecting...");
2020-06-20 15:04:03 +02:00
ESP_LOGD(TAG, "MQTT last_error = %d / rc = %d", mqttClient.lastError(),
mqttClient.returnCode());
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-06-16 20:29:49 +02:00
2020-05-17 23:07:38 +02:00
} // while(1)
2020-05-16 23:49:34 +02:00
}
void mqtt_enqueuedata(MessageBuffer_t *message) {
// enqueue message in MQTT send queue
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-06-20 15:04:03 +02:00
void mqtt_callback(MQTTClient *client, char topic[], char payload[],
int length) {
2020-10-31 12:50:48 +01:00
if (strcmp(topic, MQTT_INTOPIC) == 0)
2020-06-20 15:04:03 +02:00
rcommand((const uint8_t *)payload, (const uint8_t)length);
}
2020-06-16 20:29:49 +02:00
2020-06-20 15:04:03 +02:00
void mqtt_loop(void) {
if (!mqttClient.loop())
ESP_LOGD(TAG, "MQTT last_error = %d / rc = %d", mqttClient.lastError(),
mqttClient.returnCode());
2020-05-16 23:49:34 +02:00
}
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
}
void setMqttIRQ(void) { xTaskNotify(irqHandlerTask, MQTT_IRQ, eSetBits); }
2020-05-22 00:07:49 +02:00
2020-05-16 23:49:34 +02:00
#endif // HAS_MQTT