Quite a few updates. Subscription feature is now working, which is great. This has been tested with mosquitto broker as well as Adafruit IO, and it works perfectly with both. I tried to compile this project as a library (see the changes to Makefile) but when I used that .a file in another project the linking process failed. More research needed, I've never tried to do that before. I compressed some of the case statements in mqtt_send as they are so similar, it's a waste of code space to duplicate them. Disconnect and ping are identical except for one byte, and unsubscribe and subscribe differ in only a few lines. The data receive callback now prints information on what kind of packet/data was received; again, mostly useful during debugging, but the framework is there to expand it to do useful things like triggering other tasks/timers, etc. There is now a keepalive ping timer which keeps both the MQTT and thus the TCP connection alive. It currently pings every 5 seconds, though I might change that closer to the timeout maximum (50 seconds). Signed-off-by: A.M. Rowsell <amrowsell@frozenelectronics.ca>
62 lines
1.8 KiB
C
62 lines
1.8 KiB
C
#include "user_interface.h"
|
|
#include "ets_sys.h"
|
|
#include "osapi.h"
|
|
#include "espconn.h"
|
|
#include "os_type.h"
|
|
|
|
typedef enum mqtt_message_enum
|
|
{
|
|
MQTT_MSG_TYPE_CONNECT = 1,
|
|
MQTT_MSG_TYPE_CONNACK = 2,
|
|
MQTT_MSG_TYPE_PUBLISH = 3,
|
|
MQTT_MSG_TYPE_PUBACK = 4,
|
|
MQTT_MSG_TYPE_PUBREC = 5,
|
|
MQTT_MSG_TYPE_PUBREL = 6,
|
|
MQTT_MSG_TYPE_PUBCOMP = 7,
|
|
MQTT_MSG_TYPE_SUBSCRIBE = 8,
|
|
MQTT_MSG_TYPE_SUBACK = 9,
|
|
MQTT_MSG_TYPE_UNSUBSCRIBE = 10,
|
|
MQTT_MSG_TYPE_UNSUBACK = 11,
|
|
MQTT_MSG_TYPE_PINGREQ = 12,
|
|
MQTT_MSG_TYPE_PINGRESP = 13,
|
|
MQTT_MSG_TYPE_DISCONNECT = 14
|
|
} mqtt_message_type;
|
|
|
|
typedef struct {
|
|
uint8_t *fixedHeader;
|
|
uint32_t fixedHeader_len;
|
|
uint8_t *varHeader;
|
|
uint32_t varHeader_len;
|
|
uint8_t *payload;
|
|
uint32_t payload_len;
|
|
uint32_t length;
|
|
mqtt_message_type msgType;
|
|
} mqtt_packet_t;
|
|
|
|
|
|
typedef struct {
|
|
uint8_t ip[4];
|
|
uint32_t port;
|
|
uint32_t localPort;
|
|
uint8_t *client_id;
|
|
uint32_t client_id_len;
|
|
uint8_t *topic_name;
|
|
uint32_t topic_name_len;
|
|
uint8_t qos_level;
|
|
uint8_t *username;
|
|
uint32_t username_len;
|
|
uint8_t *password;
|
|
uint32_t password_len;
|
|
char valid_connection;
|
|
struct espconn *activeConnection;
|
|
} mqtt_session_t;
|
|
|
|
LOCAL uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg);
|
|
LOCAL void ICACHE_FLASH_ATTR disconnected_callback(void *arg);
|
|
LOCAL void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err);
|
|
LOCAL void ICACHE_FLASH_ATTR connected_callback(void *arg);
|
|
LOCAL void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned short len);
|
|
LOCAL void ICACHE_FLASH_ATTR data_sent_callback(void *arg);
|
|
LOCAL void ICACHE_FLASH_ATTR pingAlive(void *arg);
|
|
LOCAL uint8_t ICACHE_FLASH_ATTR *encodeLength(uint32_t trueLength);
|
|
LOCAL uint8_t ICACHE_FLASH_ATTR mqtt_send(mqtt_session_t *session, uint8_t *data, uint32_t len, mqtt_message_type msgType);
|