A simple MQTT library for the ESP8266
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> |
||
---|---|---|
.gitignore | ||
LICENSE | ||
Makefile | ||
mqtt.c | ||
mqtt.h | ||
README.md | ||
strtoarr.py | ||
user_config.h |
MQTT Library for ESP8266 SDK
written by Alexander Rowsell (MrAureliusR)
Released under the terms of the MIT License -- see LICENSE for more detail.
Summary
While working on a series of tutorials for Hackaday, I realized MQTT would be the perfect solution for one of the projects I was presenting. However, I didn't want to use a pre-existing MQTT library - that would be boring, and I wouldn't be able to teach the readers as much. Instead, I started to write one from scratch.
At this point, it's a bit messy. It still needs a lot of work. This documentation will help me keep everything organized and easy-to-use.
API Structures
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;```
This is the main structure that contains all the information about the connection to the server. The IP address, port, client\_id, topic\_name, username, and password are all specified here by the end user. localPort is set by the API, and qos\_level should be set to 0. *activeConnection will point to the espconn struct that represents the TCP connection to the server. The end-user shouldn't need to touch anything in this struct.
## API Functions
### `mqtt_connect(mqtt_session_t *session, uint8_t *username, uint8_t *password)`
This function establishes a TCP connection with the server specified in session->ip, on session->port. Most MQTT brokers listen on port 1883 for unencrypted packets, and on 8883 for TLS connections. This API currently only supports unencrypted packets on 1883. However, the port still needs to be specified, in case you use a non standard port.