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-05-20 18:29:07 +02:00
|
|
|
WiFiClient EthClient;
|
|
|
|
PubSubClient mqttClient(EthClient);
|
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-05-17 23:07:38 +02:00
|
|
|
ESP_LOGI(TAG, "Ethernet link layer started");
|
2020-05-21 19:42:42 +02:00
|
|
|
ETH.setHostname(ETH.macAddress().c_str());
|
2020-05-16 23:49:34 +02:00
|
|
|
break;
|
|
|
|
case SYSTEM_EVENT_ETH_CONNECTED:
|
2020-05-17 23:07:38 +02:00
|
|
|
ESP_LOGI(TAG, "Network link connected");
|
2020-05-16 23:49:34 +02:00
|
|
|
break;
|
|
|
|
case SYSTEM_EVENT_ETH_GOT_IP:
|
2020-05-17 23:07:38 +02:00
|
|
|
ESP_LOGI(TAG, "ETH MAC: %s", ETH.macAddress().c_str());
|
|
|
|
ESP_LOGI(TAG, "IPv4: %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-05-20 18:29:07 +02:00
|
|
|
mqtt_connect(MQTT_SERVER, MQTT_PORT);
|
2020-05-16 23:49:34 +02:00
|
|
|
break;
|
|
|
|
case SYSTEM_EVENT_ETH_DISCONNECTED:
|
2020-05-17 23:07:38 +02:00
|
|
|
ESP_LOGI(TAG, "Network link disconnected");
|
2020-05-16 23:49:34 +02:00
|
|
|
break;
|
|
|
|
case SYSTEM_EVENT_ETH_STOP:
|
2020-05-17 23:07:38 +02:00
|
|
|
ESP_LOGI(TAG, "Ethernet link layer stopped");
|
2020-05-16 23:49:34 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 18:29:07 +02:00
|
|
|
int mqtt_connect(const char *my_host, const uint16_t my_port) {
|
|
|
|
IPAddress mqtt_server_ip;
|
|
|
|
|
2020-05-21 19:42:42 +02:00
|
|
|
static String clientId = "paxcounter-" + ETH.macAddress();
|
2020-05-20 18:29:07 +02:00
|
|
|
ESP_LOGI(TAG, "MQTT name is %s", clientId.c_str());
|
|
|
|
|
2020-05-17 23:07:38 +02:00
|
|
|
// resolve server
|
2020-05-20 18:29:07 +02:00
|
|
|
if (WiFi.hostByName(my_host, mqtt_server_ip)) {
|
|
|
|
ESP_LOGI(TAG, "Attempting to connect to %s [%s]", my_host,
|
2020-05-17 23:07:38 +02:00
|
|
|
mqtt_server_ip.toString().c_str());
|
|
|
|
} else {
|
2020-05-20 18:29:07 +02:00
|
|
|
ESP_LOGI(TAG, "Could not resolve %s", my_host);
|
2020-05-17 23:07:38 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-16 23:49:34 +02:00
|
|
|
// attempt to connect to MQTT server
|
2020-05-21 19:42:42 +02:00
|
|
|
if (EthClient.connect(mqtt_server_ip, my_port, HOMECYCLE * 2 * 1000)) {
|
2020-05-20 18:29:07 +02:00
|
|
|
mqttClient.setServer(mqtt_server_ip, my_port);
|
2020-05-21 19:42:42 +02:00
|
|
|
mqttClient.setKeepAlive(HOMECYCLE * 2);
|
2020-05-17 23:07:38 +02:00
|
|
|
mqttClient.setCallback(mqtt_callback);
|
|
|
|
|
|
|
|
if (mqttClient.connect(clientId.c_str())) {
|
|
|
|
ESP_LOGI(TAG, "MQTT server connected, subscribing...");
|
2020-05-21 19:42:42 +02:00
|
|
|
mqttClient.publish(MQTT_OUTTOPIC, clientId.c_str());
|
2020-05-17 23:07:38 +02:00
|
|
|
mqttClient.subscribe(MQTT_INTOPIC);
|
|
|
|
ESP_LOGI(TAG, "MQTT topic subscribed");
|
2020-05-16 23:49:34 +02:00
|
|
|
} else {
|
|
|
|
ESP_LOGW(TAG, "MQTT server not responding, retrying later");
|
2020-05-17 23:07:38 +02:00
|
|
|
return -1;
|
2020-05-16 23:49:34 +02:00
|
|
|
}
|
2020-05-17 23:07:38 +02:00
|
|
|
} else {
|
2020-05-16 23:49:34 +02:00
|
|
|
ESP_LOGW(TAG, "MQTT server not connected, retrying later");
|
2020-05-17 23:07:38 +02:00
|
|
|
return -1;
|
|
|
|
}
|
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) {
|
|
|
|
|
|
|
|
// fetch next or wait for payload to send from queue
|
|
|
|
if (xQueueReceive(MQTTSendQueue, &msg, portMAX_DELAY) != pdTRUE) {
|
|
|
|
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-05-21 19:42:42 +02:00
|
|
|
mqttClient.beginPublish(MQTT_OUTTOPIC, msg.MessageSize + 2, false);
|
|
|
|
mqttClient.write(msg.MessagePort);
|
|
|
|
mqttClient.write('/');
|
|
|
|
mqttClient.write(msg.Message, msg.MessageSize);
|
|
|
|
if (mqttClient.endPublish()) {
|
|
|
|
ESP_LOGI(TAG, "%d byte(s) sent to MQTT", msg.MessageSize + 2);
|
|
|
|
continue; // while(1)
|
|
|
|
} else
|
|
|
|
goto reconnect;
|
|
|
|
|
|
|
|
} else { // not connected, thus re-enqueue the undelivered message
|
|
|
|
|
|
|
|
reconnect:
|
|
|
|
|
|
|
|
mqtt_enqueuedata(&msg);
|
2020-05-16 23:49:34 +02:00
|
|
|
delay(10000);
|
|
|
|
// attempt to reconnect to MQTT server
|
2020-05-20 18:29:07 +02:00
|
|
|
mqtt_connect(MQTT_SERVER, MQTT_PORT);
|
2020-05-16 23:49:34 +02:00
|
|
|
}
|
2020-05-17 23:07:38 +02:00
|
|
|
} // while(1)
|
2020-05-16 23:49:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t mqtt_init(void) {
|
|
|
|
assert(SEND_QUEUE_SIZE);
|
|
|
|
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...");
|
|
|
|
xTaskCreate(mqtt_client_task, "mqttloop", 4096, (void *)NULL, 2, &mqttTask);
|
|
|
|
|
2020-05-20 12:40:37 +02:00
|
|
|
WiFi.onEvent(NetworkEvent);
|
2020-05-17 23:07:38 +02:00
|
|
|
ETH.begin();
|
|
|
|
|
2020-05-16 23:49:34 +02:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_enqueuedata(MessageBuffer_t *message) {
|
|
|
|
// enqueue message in MQTT send queue
|
|
|
|
BaseType_t ret;
|
|
|
|
MessageBuffer_t DummyBuffer;
|
|
|
|
sendprio_t prio = message->MessagePrio;
|
|
|
|
|
|
|
|
switch (prio) {
|
|
|
|
case prio_high:
|
|
|
|
// clear space in queue if full, then fallthrough to normal
|
|
|
|
if (!uxQueueSpacesAvailable(MQTTSendQueue))
|
|
|
|
xQueueReceive(MQTTSendQueue, &DummyBuffer, (TickType_t)0);
|
|
|
|
case prio_normal:
|
|
|
|
ret = xQueueSendToFront(MQTTSendQueue, (void *)message, (TickType_t)0);
|
|
|
|
break;
|
|
|
|
case prio_low:
|
|
|
|
default:
|
|
|
|
ret = xQueueSendToBack(MQTTSendQueue, (void *)message, (TickType_t)0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ret != pdTRUE)
|
|
|
|
ESP_LOGW(TAG, "MQTT sendqueue is full");
|
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_queuereset(void) { xQueueReset(MQTTSendQueue); }
|
|
|
|
|
|
|
|
void mqtt_callback(char *topic, byte *payload, unsigned int length) {
|
2020-05-21 19:42:42 +02:00
|
|
|
ESP_LOGD(TAG, "MQTT %d byte(s) received", length);
|
2020-05-16 23:49:34 +02:00
|
|
|
rcommand(payload, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAS_MQTT
|