Skip to content

Commit

Permalink
Move to local async client
Browse files Browse the repository at this point in the history
  • Loading branch information
steadramon committed Sep 1, 2024
1 parent 8bd09da commit ea1db73
Show file tree
Hide file tree
Showing 71 changed files with 3,277 additions and 2 deletions.
12 changes: 12 additions & 0 deletions lib/default/async-mqtt-client/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

[keywords.txt]
indent_style = tab
2 changes: 2 additions & 0 deletions lib/default/async-mqtt-client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/config.json
.vscode/
21 changes: 21 additions & 0 deletions lib/default/async-mqtt-client/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015-2021 Marvin Roger

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions lib/default/async-mqtt-client/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cpplint:
cpplint --repository=. --recursive --filter=-whitespace/line_length,-legal/copyright,-runtime/printf,-build/include,-build/namespace ./src
.PHONY: cpplint
19 changes: 19 additions & 0 deletions lib/default/async-mqtt-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Async MQTT client for ESP8266 and ESP32

![Build with PlatformIO](https://github.com/marvinroger/async-mqtt-client/workflows/Build%20with%20Platformio/badge.svg)
![cpplint](https://github.com/marvinroger/async-mqtt-client/workflows/cpplint/badge.svg)

An Arduino for ESP8266 and ESP32 asynchronous [MQTT](http://mqtt.org/) client implementation, built on [me-no-dev/ESPAsyncTCP (ESP8266)](https://github.com/me-no-dev/ESPAsyncTCP) | [me-no-dev/AsyncTCP (ESP32)](https://github.com/me-no-dev/AsyncTCP) .

## Features

* Compliant with the 3.1.1 version of the protocol
* Fully asynchronous
* Subscribe at QoS 0, 1 and 2
* Publish at QoS 0, 1 and 2
* SSL/TLS support
* Available in the [PlatformIO registry](http://platformio.org/lib/show/346/AsyncMqttClient)

## Requirements, installation and usage

The project is documented in the [/docs folder](docs).
6 changes: 6 additions & 0 deletions lib/default/async-mqtt-client/async-mqtt-client.cppcheck
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="1">
<includedir>
<dir name="src/"/>
</includedir>
</project>
28 changes: 28 additions & 0 deletions lib/default/async-mqtt-client/docs/1.-Getting-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Getting started

To use AsyncMqttClient, you need:

* An ESP8266 or ESP32
* The Arduino IDE or equivalent IDE for ESP8266/32
* Basic knowledge of the Arduino environment (use the IDE, upload a sketch, import libraries, ...)

## Installing AsyncMqttClient

There are two ways to install AsyncMqttClient.

### 1a. For the Arduino IDE

1. Download the [corresponding release](https://github.com/marvinroger/async-mqtt-client/releases/latest)
2. Load the `.zip` with **Sketch → Include Library → Add .ZIP Library**

AsyncMqttClient has 1 dependency:
* For ESP8266: [ESPAsyncTCP](https://github.com/me-no-dev/ESPAsyncTCP). Download the [.zip](https://github.com/me-no-dev/ESPAsyncTCP/archive/master.zip) and install it with the same method as above.
* Fors ESP32: [AsyncTCP](https://github.com/me-no-dev/AsyncTCP). Download the [.zip](https://github.com/me-no-dev/AsyncTCP/archive/master.zip) and install it with the same method as above.

## Fully-featured sketch

See [examples/FullyFeatured-ESP8266.ino](../examples/FullyFeatured-ESP8266/FullyFeatured-ESP8266.ino)

**<u>Very important:</u> As a rule of thumb, never use blocking functions in the callbacks (don't use `delay()` or `yield()`).** Otherwise, you may very probably experience unexpected behaviors.

You can go to the [API reference](2.-API-reference.md).
166 changes: 166 additions & 0 deletions lib/default/async-mqtt-client/docs/2.-API-reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# API reference

#### AsyncMqttClient()

Instantiate a new AsyncMqttClient object.

### Configuration

#### AsyncMqttClient& setKeepAlive(uint16_t `keepAlive`)

Set the keep alive. Defaults to 15 seconds.

* **`keepAlive`**: Keep alive in seconds

#### AsyncMqttClient& setClientId(const char\* `clientId`)

Set the client ID. Defaults to `esp8266<chip ID on 6 hex caracters>`.

* **`clientId`**: Client ID

#### AsyncMqttClient& setCleanSession(bool `cleanSession`)

Whether or not to set the CleanSession flag. Defaults to `true`.

* **`cleanSession`**: clean session wanted or not

#### AsyncMqttClient& setMaxTopicLength(uint16_t `maxTopicLength`)

Set the maximum allowed topic length to receive. If an MQTT packet is received
with a topic longer than this maximum, the packet will be ignored. Defaults to `128`.

* **`maxTopicLength`**: Maximum allowed topic length to receive

#### AsyncMqttClient& setCredentials(const char\* `username`, const char\* `password` = nullptr)

Set the username/password. Defaults to non-auth.

* **`username`**: Username
* **`password`**: Password

#### AsyncMqttClient& setWill(const char\* `topic`, uint8_t `qos`, bool `retain`, const char\* `payload` = nullptr, size_t `length` = 0)

Set the Last Will Testament. Defaults to none.

* **`topic`**: Topic of the LWT
* **`qos`**: QoS of the LWT
* **`retain`**: Retain flag of the LWT
* **`payload`**: Payload of the LWT. If unset, the payload will be empty
* **`length`**: Payload length. If unset or set to 0, the payload will be considered as a string and its size will be calculated using `strlen(payload)`

#### AsyncMqttClient& setServer(IPAddress `ip`, uint16_t `port`)

Set the server.

* **`ip`**: IP of the server
* **`port`**: Port of the server

#### AsyncMqttClient& setServer(const char\* `host`, uint16_t `port`)

Set the server.

* **`host`**: Host of the server
* **`port`**: Port of the server

#### AsyncMqttClient& setSecure(bool `secure`)

Whether or not to use SSL. Defaults to `false`.

* **`secure`**: SSL wanted or not.

#### AsyncMqttClient& addServerFingerprint(const uint8_t\* `fingerprint`)

Adds an acceptable server fingerprint (SHA1). This may be called multiple times to permit any one of the specified fingerprints. By default, if no fingerprint is added, any fingerprint is accepted.

* **`fingerprint`**: Fingerprint to add

### Events handlers

#### AsyncMqttClient& onConnect(AsyncMqttClientInternals::OnConnectUserCallback `callback`)

Add a connect event handler.

* **`callback`**: Function to call

#### AsyncMqttClient& onDisconnect(AsyncMqttClientInternals::OnDisconnectUserCallback `callback`)

Add a disconnect event handler.

* **`callback`**: Function to call

#### AsyncMqttClient& onSubscribe(AsyncMqttClientInternals::OnSubscribeUserCallback `callback`)

Add a subscribe acknowledged event handler.

* **`callback`**: Function to call

#### AsyncMqttClient& onUnsubscribe(AsyncMqttClientInternals::OnUnsubscribeUserCallback `callback`)

Add an unsubscribe acknowledged event handler.

* **`callback`**: Function to call

#### AsyncMqttClient& onMessage(AsyncMqttClientInternals::OnMessageUserCallback `callback`)

Add a publish received event handler.

* **`callback`**: Function to call

#### AsyncMqttClient& onPublish(AsyncMqttClientInternals::OnPublishUserCallback `callback`)

Add a publish acknowledged event handler.

* **`callback`**: Function to call

### Operation functions

#### bool connected()

Return if the client is currently connected to the broker or not.

#### void connect()

Connect to the server.

#### void disconnect(bool `force` = false)

Disconnect from the server.

* **`force`**: Whether to force the disconnection. Defaults to `false` (clean disconnection).

#### uint16_t subscribe(const char\* `topic`, uint8_t `qos`)

Subscribe to the given topic at the given QoS.

Return the packet ID or 0 if failed.

* **`topic`**: Topic
* **`qos`**: QoS

#### uint16_t unsubscribe(const char\* `topic`)

Unsubscribe from the given topic.

Return the packet ID or 0 if failed.

* **`topic`**: Topic

#### uint16_t publish(const char\* `topic`, uint8_t `qos`, bool `retain`, const char\* `payload` = nullptr, size_t `length` = 0, bool dup = false, uint16_t message_id = 0)

Publish a packet.

Return the packet ID (or 1 if QoS 0) or 0 if failed.

* **`topic`**: Topic
* **`qos`**: QoS
* **`retain`**: Retain flag
* **`payload`**: Payload. If unset, the payload will be empty
* **`length`**: Payload length. If unset or set to 0, the payload will be considered as a string and its size will be calculated using `strlen(payload)`
* **`dup`**: ~~Duplicate flag. If set or set to 1, the payload will be flagged as a duplicate~~ Setting is not used anymore
* **`message_id`**: ~~The message ID. If unset or set to 0, the message ID will be automtaically assigned. Use this with the DUP flag to identify which message is being duplicated~~ Setting is not used anymore

#### bool clearQueue()

When disconnected, clears all queued messages

Returns true on succes, false on failure (client is no disconnected)
12 changes: 12 additions & 0 deletions lib/default/async-mqtt-client/docs/3.-Memory-management.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Memory management

AsyncMqttClient buffers outgoing messages in a queue. On sending data is copied to a raw TCP buffer. Received data is passed directly to the API.

## Outgoing messages

You can send data as long as memory permits. A minimum amount of free memory is set at 4096 bytes. You can lower (or raise) this value by setting `MQTT_MIN_FREE_MEMORY` to your desired value.
If the free memory was sufficient to send your packet, the `publish` method will return a packet ID indicating the packet was queued. Otherwise, a `0` will be returned, and it's your responsability to resend the packet with `publish`.

## Incoming messages

No incoming data is buffered by this library. Messages received by the TCP library is passed directly to the API. The max receive size is about 1460 bytes per call to your onMessage callback but the amount of data you can receive is unlimited. If you receive, say, a 300kB payload (such as an OTA payload), then your `onMessage` callback will be called about 200 times, with the according len, index and total parameters. Keep in mind the library will call your `onMessage` callbacks with the same topic buffer, so if you change the buffer on one call, the buffer will remain changed on subsequent calls.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Limitations and known issues

* The library is spec compliant with one limitation. In case of power loss the following is not honored:

> Must be kept in memory:
* All messages in a QoS 1 or 2 flow, which are not confirmed by the broker
* All received QoS 2 messages, which are not yet confirmed to the broker

This means retransmission is not honored in case of a power failure. This behaviour is like explained in point 4.1.1 of the MQTT specification v3.1.1

* You cannot send payload larger that what can fit on RAM.

## SSL limitations

* SSL requires the build flag -DASYNC_TCP_SSL_ENABLED=1
* SSL only supports fingerprints for server validation.
* If you do not specify one or more acceptable server fingerprints, the SSL connection will be vulnerable to man-in-the-middle attacks.
* Some server certificate signature algorithms do not work. SHA1, SHA224, SHA256, and MD5 are working. SHA384, and SHA512 will cause a crash.
* TLS1.2 is not supported.
11 changes: 11 additions & 0 deletions lib/default/async-mqtt-client/docs/5.-Troubleshooting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Troubleshooting

* The payload of incoming messages contains **raw data**. You cannot just print out the data without formatting. This is because Arduino's `print` functions expect a C-string as input and a MQTT payload is not. A simple solution is to print each character of the payload:

```cpp
for (size_t i = 0; i < len; ++i) {
Serial.print(payload[i]);
}
```

Further reading: https://en.wikipedia.org/wiki/C_string_handling
4 changes: 4 additions & 0 deletions lib/default/async-mqtt-client/docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
AsyncMqttClient documentation
=============================

See [index.md](index.md) to view it locally, or http://marvinroger.viewdocs.io/async-mqtt-client/ to view it online.
11 changes: 11 additions & 0 deletions lib/default/async-mqtt-client/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Welcome to the AsyncMqttClient for ESP8266 docs.

**<p align="center">This documentation is only valid for the AsyncMqttClient version in this repo/directory</p>**

-----

#### 1. [Getting started](1.-Getting-started.md)
#### 2. [API reference](2.-API-reference.md)
#### 3. [Memory management](3.-Memory-management.md)
#### 4. [Limitations and known issues](4.-Limitations-and-known-issues.md)
#### 5. [Troubleshooting](5.-Troubleshooting.md)
Loading

0 comments on commit ea1db73

Please sign in to comment.