MQTT client (experimental)

This commit is contained in:
Klaus K Wilting 2020-05-17 23:07:38 +02:00
parent e720728ff5
commit c5c9e933d4
2 changed files with 55 additions and 33 deletions

View File

@ -6,16 +6,18 @@
#include <ETH.h> #include <ETH.h>
#include <PubSubClient.h> #include <PubSubClient.h>
#define MQTT_NAME "paxcounter" #define MQTT_CLIENT "paxcounter"
#define MQTT_INTOPIC "rcommand" #define MQTT_INTOPIC "pax_IN"
#define MQTT_OUTTOPIC "pax_OUT"
#define MQTT_PORT 1883 #define MQTT_PORT 1883
#define MQTT_SERVER "broker.hivemq.com"
extern TaskHandle_t mqttTask; extern TaskHandle_t mqttTask;
void mqtt_enqueuedata(MessageBuffer_t *message); void mqtt_enqueuedata(MessageBuffer_t *message);
void mqtt_queuereset(void); void mqtt_queuereset(void);
void mqtt_client_task(void *param); void mqtt_client_task(void *param);
void mqtt_connect(IPAddress mqtt_host, uint16_t mqtt_port); int mqtt_connect(IPAddress mqtt_host, uint16_t mqtt_port);
void mqtt_callback(char *topic, byte *payload, unsigned int length); void mqtt_callback(char *topic, byte *payload, unsigned int length);
void WiFiEvent(WiFiEvent_t event); void WiFiEvent(WiFiEvent_t event);
esp_err_t mqtt_init(void); esp_err_t mqtt_init(void);

View File

@ -4,58 +4,81 @@
static const char TAG[] = __FILE__; static const char TAG[] = __FILE__;
IPAddress mqtt_server_ip(192, 168, 11, 57); IPAddress mqtt_server_ip;
QueueHandle_t MQTTSendQueue; QueueHandle_t MQTTSendQueue;
TaskHandle_t mqttTask; TaskHandle_t mqttTask;
WiFiClient ipClient; WiFiClient ipClient;
PubSubClient client(ipClient); PubSubClient mqttClient(ipClient);
void WiFiEvent(WiFiEvent_t event) { void WiFiEvent(WiFiEvent_t event) {
switch (event) { switch (event) {
case SYSTEM_EVENT_ETH_START: case SYSTEM_EVENT_ETH_START:
ESP_LOGI(TAG, "ETH Started"); ESP_LOGI(TAG, "Ethernet link layer started");
ETH.setHostname(MQTT_NAME); ETH.setHostname(MQTT_CLIENT);
break; break;
case SYSTEM_EVENT_ETH_CONNECTED: case SYSTEM_EVENT_ETH_CONNECTED:
ESP_LOGI(TAG, "ETH Connected"); ESP_LOGI(TAG, "Network link connected");
break; break;
case SYSTEM_EVENT_ETH_GOT_IP: case SYSTEM_EVENT_ETH_GOT_IP:
ESP_LOGI(TAG, "ETH MAC: %s", ETH.macAddress()); ESP_LOGI(TAG, "ETH MAC: %s", ETH.macAddress().c_str());
ESP_LOGI(TAG, "IPv4: %s", ETH.localIP()); ESP_LOGI(TAG, "IPv4: %s", ETH.localIP().toString().c_str());
ESP_LOGI(TAG, "Link Speed %d Mbps %s", ETH.linkSpeed(), ESP_LOGI(TAG, "Link Speed %d Mbps %s", ETH.linkSpeed(),
ETH.fullDuplex() ? "full duplex" : "half duplex"); ETH.fullDuplex() ? "full duplex" : "half duplex");
mqtt_connect(mqtt_server_ip, MQTT_PORT); mqtt_connect(mqtt_server_ip, MQTT_PORT);
break; break;
case SYSTEM_EVENT_ETH_DISCONNECTED: case SYSTEM_EVENT_ETH_DISCONNECTED:
ESP_LOGI(TAG, "ETH Disconnected"); ESP_LOGI(TAG, "Network link disconnected");
break; break;
case SYSTEM_EVENT_ETH_STOP: case SYSTEM_EVENT_ETH_STOP:
ESP_LOGI(TAG, "ETH Stopped"); ESP_LOGI(TAG, "Ethernet link layer stopped");
break; break;
default: default:
break; break;
} }
} }
void mqtt_connect(IPAddress mqtt_host, uint16_t mqtt_port) { int mqtt_connect(IPAddress mqtt_host, uint16_t mqtt_port) {
// resolve server
if (WiFi.hostByName(MQTT_SERVER, mqtt_server_ip)) {
ESP_LOGI(TAG, "Attempting to connect to %s [%s]", MQTT_SERVER,
mqtt_server_ip.toString().c_str());
} else {
ESP_LOGI(TAG, "Could not resolve %s", MQTT_SERVER);
return -1;
}
// attempt to connect to MQTT server // attempt to connect to MQTT server
if (ipClient.connect(mqtt_server_ip, MQTT_PORT)) { if (ipClient.connect(mqtt_host, mqtt_port)) {
if (client.connect(MQTT_NAME)) {
ESP_LOGW(TAG, "MQTT server connected, subscribing"); mqttClient.setServer(mqtt_server_ip, MQTT_PORT);
client.subscribe(MQTT_INTOPIC); mqttClient.setCallback(mqtt_callback);
String clientId = "Paxcounter-";
clientId += String(random(0xffff), HEX);
if (mqttClient.connect(clientId.c_str())) {
ESP_LOGI(TAG, "MQTT server connected, subscribing...");
mqttClient.publish(MQTT_OUTTOPIC, "hello world");
mqttClient.subscribe(MQTT_INTOPIC);
ESP_LOGI(TAG, "MQTT topic subscribed");
} else { } else {
ESP_LOGW(TAG, "MQTT server not responding, retrying later"); ESP_LOGW(TAG, "MQTT server not responding, retrying later");
return -1;
} }
} else } else {
ESP_LOGW(TAG, "MQTT server not connected, retrying later"); ESP_LOGW(TAG, "MQTT server not connected, retrying later");
return -1;
}
} }
void mqtt_client_task(void *param) { void mqtt_client_task(void *param) {
MessageBuffer_t msg;
char cPort[4], cMsg[PAYLOAD_BUFFER_SIZE + 1];
while (1) { while (1) {
MessageBuffer_t msg;
char cPort[4], cMsg[PAYLOAD_BUFFER_SIZE + 1];
// fetch next or wait for payload to send from queue // fetch next or wait for payload to send from queue
if (xQueueReceive(MQTTSendQueue, &msg, portMAX_DELAY) != pdTRUE) { if (xQueueReceive(MQTTSendQueue, &msg, portMAX_DELAY) != pdTRUE) {
@ -63,12 +86,12 @@ void mqtt_client_task(void *param) {
continue; continue;
} }
// send data // send data to mqtt server
if (client.connected()) { if (mqttClient.connected()) {
snprintf(cPort, sizeof(cPort), "%d", msg.MessagePort); snprintf(cPort, sizeof(cPort), "Port_%d", msg.MessagePort);
snprintf(cMsg, sizeof(cMsg), "%s", msg.Message); snprintf(cMsg, sizeof(cMsg), "%s", msg.Message);
client.publish(cPort, cMsg); mqttClient.publish(cPort, cMsg);
client.loop(); mqttClient.loop();
ESP_LOGI(TAG, "%d byte(s) sent to MQTT", msg.MessageSize); ESP_LOGI(TAG, "%d byte(s) sent to MQTT", msg.MessageSize);
} else { } else {
mqtt_enqueuedata(&msg); // re-enqueue the undelivered message mqtt_enqueuedata(&msg); // re-enqueue the undelivered message
@ -76,7 +99,7 @@ void mqtt_client_task(void *param) {
// attempt to reconnect to MQTT server // attempt to reconnect to MQTT server
mqtt_connect(mqtt_server_ip, MQTT_PORT); mqtt_connect(mqtt_server_ip, MQTT_PORT);
} }
} } // while(1)
} }
esp_err_t mqtt_init(void) { esp_err_t mqtt_init(void) {
@ -89,15 +112,12 @@ esp_err_t mqtt_init(void) {
ESP_LOGI(TAG, "MQTT send queue created, size %d Bytes", ESP_LOGI(TAG, "MQTT send queue created, size %d Bytes",
SEND_QUEUE_SIZE * PAYLOAD_BUFFER_SIZE); SEND_QUEUE_SIZE * PAYLOAD_BUFFER_SIZE);
WiFi.onEvent(WiFiEvent);
assert(ETH.begin());
client.setServer(mqtt_server_ip, MQTT_PORT);
client.setCallback(mqtt_callback);
ESP_LOGI(TAG, "Starting MQTTloop..."); ESP_LOGI(TAG, "Starting MQTTloop...");
xTaskCreate(mqtt_client_task, "mqttloop", 4096, (void *)NULL, 2, &mqttTask); xTaskCreate(mqtt_client_task, "mqttloop", 4096, (void *)NULL, 2, &mqttTask);
WiFi.onEvent(WiFiEvent);
ETH.begin();
return ESP_OK; return ESP_OK;
} }
@ -127,7 +147,7 @@ void mqtt_enqueuedata(MessageBuffer_t *message) {
void mqtt_queuereset(void) { xQueueReset(MQTTSendQueue); } void mqtt_queuereset(void) { xQueueReset(MQTTSendQueue); }
void mqtt_callback(char *topic, byte *payload, unsigned int length) { void mqtt_callback(char *topic, byte *payload, unsigned int length) {
if ((length) && (topic == MQTT_INTOPIC)) if ((length >= 1) && (topic == MQTT_INTOPIC))
rcommand(payload, length); rcommand(payload, length);
} }