A simple MQTT library for the ESP8266
Fixed the bug in PUBLISH. Tested with Adafruit IO and data can be published successfully! This was the original goal of this project -- being able to blindly fire values at Adafruit and not having to worry about receiving data. However, I've widened the scope of the project, and want to eventually do a full MQTT implementation per the spec. The basic receive functionality looks at what type of message is receieved, and then prints out basic messages depending on what the payload is. Mostly useful as a debugging tool, but it can easily be expanded. This should probably be split out into its own function at some point. strtoarr.py: Because of the weird bug that makes passing string pointers impossible with the ESP8266, strings have to be stored as hex arrays and then passed as a pointer. This is a pain for the user, so I've written a basic script which will take two arguments: the string to be encoded, and the variable name to be used. It then spits out C code which can be copied and pasted into the user code. This saves a bunch of time, and because it spits them out as static const, it also tells the compiler to store them in flash (as far as I can tell). As mentioned, it has now been tested with Adafruit IO. There are a few more functions in the "user" area of the file. These are only for debugging and will eventually be surrounded with \#ifdef DEBUG so they won't be included in the compiled version. Also, more ifdef preprocessor directives have been added so that debug messages won't be printed to a user's output unless they want them. The next step is to implement SUBSCRIBE, and to compile the code as mqtt.a - a linkable object file. |
||
---|---|---|
.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.