From 6044f0013783f28ee1c72be6bf508e6918c58668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Grome=C5=A1?= Date: Tue, 10 Jul 2018 15:12:50 +0200 Subject: [PATCH] ESP8266 - Added method to receive PUBLISH packets from server --- keywords.txt | 1 + src/TypeDef.h | 1 + src/modules/ESP8266.cpp | 45 +++++++++++++++++++++++++++++++++++++++++ src/modules/ESP8266.h | 1 + 4 files changed, 48 insertions(+) diff --git a/keywords.txt b/keywords.txt index f2fb5fc3..d9a677e7 100644 --- a/keywords.txt +++ b/keywords.txt @@ -117,6 +117,7 @@ ERR_MQTT_CONN_SERVER_UNAVAILABLE LITERAL1 ERR_MQTT_CONN_BAD_USERNAME_PASSWORD LITERAL1 ERR_MQTT_CONN_NOT_AUTHORIZED LITERAL1 ERR_MQTT_UNEXPECTED_PACKET_ID LITERAL1 +ERR_MQTT_NO_NEW_PACKET_AVAILABLE LITERAL1 MQTT_SUBS_SUCCESS_QOS_0 LITERAL1 MQTT_SUBS_SUCCESS_QOS_1 LITERAL1 MQTT_SUBS_SUCCESS_QOS_2 LITERAL1 diff --git a/src/TypeDef.h b/src/TypeDef.h index 3ae1ae2c..e6adfd0d 100644 --- a/src/TypeDef.h +++ b/src/TypeDef.h @@ -83,6 +83,7 @@ #define ERR_MQTT_CONN_BAD_USERNAME_PASSWORD 0x08 #define ERR_MQTT_CONN_NOT_AUTHORIZED 0x09 #define ERR_MQTT_UNEXPECTED_PACKET_ID 0x0A +#define ERR_MQTT_NO_NEW_PACKET_AVAILABLE 0x0B #define MQTT_SUBS_SUCCESS_QOS_0 0x00 #define MQTT_SUBS_SUCCESS_QOS_1 0x01 #define MQTT_SUBS_SUCCESS_QOS_2 0x02 diff --git a/src/modules/ESP8266.cpp b/src/modules/ESP8266.cpp index ef3c359f..5b550032 100644 --- a/src/modules/ESP8266.cpp +++ b/src/modules/ESP8266.cpp @@ -619,6 +619,51 @@ uint8_t ESP8266::MqttPing() { return(ERR_RESPONSE_MALFORMED); } +uint8_t ESP8266::MqttCheck(void (*func)(const char*, const char*)) { + // ping the server + uint8_t state = MqttPing(); + if(state != ERR_NONE) { + return(state); + } + + // check new data + uint16_t numBytes = getNumBytes(); + if(numBytes == 0) { + return(ERR_MQTT_NO_NEW_PACKET_AVAILABLE); + } + + // read the PUBLISH packet from server + uint8_t* dataIn = new uint8_t[numBytes]; + receive(dataIn, numBytes); + if(dataIn[0] == MQTT_PUBLISH << 4) { + // TODO: properly decode remaining length + uint8_t remainingLength = dataIn[1]; + + // get the topic + size_t topicLength = dataIn[3] | dataIn[2] << 8; + char* topic = new char[topicLength + 1]; + memcpy(topic, dataIn + 4, topicLength); + topic[topicLength] = 0x00; + + // get the message + size_t messageLength = remainingLength - topicLength - 2; + char* message = new char[messageLength + 1]; + memcpy(message, dataIn + 4 + topicLength, messageLength); + message[messageLength] = 0x00; + + // execute the callback function provided by user + func(topic, message); + + delete[] topic; + delete[] message; + delete[] dataIn; + return(ERR_NONE); + } + delete[] dataIn; + + return(MQTT_NO_NEW_PACKET); +} + uint8_t ESP8266::send(const char* data) { // build AT command char lenStr[8]; diff --git a/src/modules/ESP8266.h b/src/modules/ESP8266.h index c252d3b9..51c1a80c 100644 --- a/src/modules/ESP8266.h +++ b/src/modules/ESP8266.h @@ -49,6 +49,7 @@ class ESP8266 { uint8_t MqttSubscribe(const char* topicFilter); uint8_t MqttUnsubscribe(const char* topicFilter); uint8_t MqttPing(); + uint8_t MqttCheck(void (*func)(const char*, const char*)); // Transport layer methods uint8_t openTransportConnection(const char* host, const char* protocol, uint16_t port, uint16_t tcpKeepAlive = 0);