The code is now pretty messy, as I've just been hacking it together so far. It's basically hardcoded, so the next steps will be to parameterise everything. I also need to rethink the data structures, and function names/structures. I need to make this feel more like an API to be called by a user. This will naturally help organize the functions.
57 lines
1.6 KiB
C
57 lines
1.6 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;
|
|
uint8_t *topic_name;
|
|
uint8_t qos_level;
|
|
uint8_t *username;
|
|
uint8_t *password;
|
|
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 uint8_t ICACHE_FLASH_ATTR mqtt_send(mqtt_session_t *session, uint8_t *data, uint32_t len, mqtt_message_type msgType);
|
|
LOCAL uint8_t ICACHE_FLASH_ATTR mqtt_connect(mqtt_session_t *session);
|