Skip to content

Commit e9b3966

Browse files
authored
Enqueue messages
* queue outgoing messages * add optional debug logging * add simple "mutex" for ESP8266 * add connection states * add CI (Github Actions) * update deps * pass disconnectreason to user * update docs (#252) * various bugfixes
1 parent 29485e7 commit e9b3966

39 files changed

+1183
-530
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Build with Platformio
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v1
12+
- name: Set up Python
13+
uses: actions/setup-python@v1
14+
- name: Install dependencies
15+
run: |
16+
python -m pip install --upgrade pip
17+
pip install platformio
18+
- name: Add libraries
19+
run: |
20+
platformio lib -g install AsyncTCP
21+
platformio lib -g install ESPAsyncTCP
22+
- name: Getting ready
23+
run: |
24+
chmod +x ./scripts/CI/build_examples_pio.sh
25+
- name: Build examples
26+
run: |
27+
./scripts/CI/build_examples_pio.sh

.github/workflows/cpplint.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: cpplint
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v1
12+
- name: Set up Python
13+
uses: actions/setup-python@v1
14+
- name: Install dependencies
15+
run: |
16+
python -m pip install --upgrade pip
17+
pip install cpplint
18+
- name: Linting
19+
run: |
20+
cpplint --repository=. --recursive --filter=-whitespace/line_length,-legal/copyright,-runtime/printf,-build/include,-build/namespace ./src

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/config.json
2+
.vscode/

.travis.yml

-20
This file was deleted.

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015 Marvin Roger
3+
Copyright (c) 2015-2021 Marvin Roger
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
Async MQTT client for ESP8266 and ESP32
2-
=============================
1+
# Async MQTT client for ESP8266 and ESP32
32

4-
[![Build Status](https://img.shields.io/travis/marvinroger/async-mqtt-client/master.svg?style=flat-square)](https://travis-ci.org/marvinroger/async-mqtt-client)
3+
![Build with PlatformIO](https://github.com/marvinroger/async-mqtt-client/workflows/Build%20with%20Platformio/badge.svg)
4+
![cpplint](https://github.com/marvinroger/async-mqtt-client/workflows/cpplint/badge.svg)
55

66
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) .
7+
78
## Features
89

910
* Compliant with the 3.1.1 version of the protocol

docs/1.-Getting-started.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
To use AsyncMqttClient, you need:
44

5-
* An ESP8266
6-
* The Arduino IDE for ESP8266 (version 2.2.0 minimum)
7-
* Basic knowledge of the Arduino environment (upload a sketch, import libraries, ...)
5+
* An ESP8266 or ESP32
6+
* The Arduino IDE or equivalent IDE for ESP8266/32
7+
* Basic knowledge of the Arduino environment (use the IDE, upload a sketch, import libraries, ...)
88

99
## Installing AsyncMqttClient
1010

@@ -15,7 +15,9 @@ There are two ways to install AsyncMqttClient.
1515
1. Download the [corresponding release](https://github.com/marvinroger/async-mqtt-client/releases/latest)
1616
2. Load the `.zip` with **Sketch → Include Library → Add .ZIP Library**
1717

18-
AsyncMqttClient has 1 dependency: [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.
18+
AsyncMqttClient has 1 dependency:
19+
* 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.
20+
* 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.
1921

2022
## Fully-featured sketch
2123

docs/2.-API-reference.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,11 @@ Return the packet ID (or 1 if QoS 0) or 0 if failed.
156156
* **`retain`**: Retain flag
157157
* **`payload`**: Payload. If unset, the payload will be empty
158158
* **`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)`
159-
* **`dup`**: Duplicate flag. If set or set to 1, the payload will be flagged as a duplicate
160-
* **`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
159+
* **`dup`**: ~~Duplicate flag. If set or set to 1, the payload will be flagged as a duplicate~~ Setting is not used anymore
160+
* **`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
161+
162+
#### bool clearQueue()
163+
164+
When disconnected, clears all queued messages
165+
166+
Returns true on succes, false on failure (client is no disconnected)

docs/3.-Memory-management.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# Memory management
22

3-
AsyncMqttClient does not use an internal buffer, it uses the raw TCP buffer.
3+
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.
44

5-
The max receive size is about 1460 bytes per call to your onMessage callback. But the amount of data you can receive is unlimited, as 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.
5+
## Outgoing messages
66

7-
You can send data as long as you stay below the available TCP window (which is about 3-4kB on the ESP8266). The data is indeed held in memory by the async TCP code until ACK is received. If the TCP window was sufficient to send your packet, the `publish` method will return a packet ID indicating the packet was sent. Otherwise, a `0` will be returned, and it's your responsability to resend the packet with `publish`.
7+
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.
8+
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`.
9+
10+
## Incoming messages
11+
12+
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.
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# Limitations and known issues
22

3-
* When the CleanSession is set to `false`, the implementation is not spec compliant. The following is not honored:
3+
* The library is spec compliant with one limitation. In case of power loss the following is not honored:
44

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

9-
This means retransmission is not honored in case of a failure.
9+
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
1010

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

1313
## SSL limitations
1414

15-
* SSL requires use of esp8266/Arduino 2.4.0, which is not yet released (platform = espressif8266_stage in PlatformIO).
1615
* SSL requires the build flag -DASYNC_TCP_SSL_ENABLED=1
1716
* SSL only supports fingerprints for server validation.
1817
* If you do not specify one or more acceptable server fingerprints, the SSL connection will be vulnerable to man-in-the-middle attacks.
19-
* Some server certificate signature algorithms do not work. SHA1, SHA224, SHA256, and MD5 are working. SHA384, and SHA512 will cause a crash.
18+
* Some server certificate signature algorithms do not work. SHA1, SHA224, SHA256, and MD5 are working. SHA384, and SHA512 will cause a crash.
19+
* TLS1.2 is not supported.

docs/5.-Troubleshooting.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
# Troubleshooting
22

3-
To be completed when issues arise.
3+
* 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:
4+
5+
```cpp
6+
for (size_t i = 0; i < len; ++i) {
7+
Serial.print(payload[i]);
8+
}
9+
```
10+
11+
Further reading: https://en.wikipedia.org/wiki/C_string_handling

examples/FullyFeatured-ESP8266/FullyFeatured-ESP8266.ino

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ void connectToWifi() {
2020
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
2121
}
2222

23+
void connectToMqtt() {
24+
Serial.println("Connecting to MQTT...");
25+
mqttClient.connect();
26+
}
27+
2328
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
2429
Serial.println("Connected to Wi-Fi.");
2530
connectToMqtt();
@@ -31,11 +36,6 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
3136
wifiReconnectTimer.once(2, connectToWifi);
3237
}
3338

34-
void connectToMqtt() {
35-
Serial.println("Connecting to MQTT...");
36-
mqttClient.connect();
37-
}
38-
3939
void onMqttConnect(bool sessionPresent) {
4040
Serial.println("Connected to MQTT.");
4141
Serial.print("Session present: ");

keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ disconnect KEYWORD2
3333
subscribe KEYWORD2
3434
unsubscribe KEYWORD2
3535
publish KEYWORD2
36+
clearQueue KEYWORD2
3637

3738
#######################################
3839
# Constants (LITERAL1)

library.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
"type": "git",
1313
"url": "https://github.com/marvinroger/async-mqtt-client.git"
1414
},
15-
"version": "0.8.2",
15+
"version": "0.9.0",
1616
"frameworks": "arduino",
1717
"platforms": ["espressif8266", "espressif32"],
1818
"dependencies": [
1919
{
2020
"name": "ESPAsyncTCP",
21-
"version": "1.2.0",
21+
"version": ">=1.2.2",
2222
"platforms": "espressif8266"
2323
},
2424
{
2525
"name": "AsyncTCP",
26-
"version": "^1.0.0",
26+
"version": ">=1.1.1",
2727
"platforms": "espressif32"
2828
}
2929
]

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=AsyncMqttClient
2-
version=0.8.2
2+
version=0.9.0
33
author=Marvin ROGER
44
maintainer=Marvin ROGER
55
sentence=An Arduino for ESP8266 and ESP32 asynchronous MQTT client implementation

scripts/CI/build_examples_pio.sh

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
#pip install -U platformio
4+
#platformio update
5+
platformio lib -g install AsyncTCP
6+
platformio lib -g install ESPAsyncTCP
7+
8+
RED='\033[0;31m'
9+
GREEN='\033[0;32m'
10+
YELLOW='\033[0;33m'
11+
NC='\033[0m'
12+
13+
lines=$(find ./examples/ -maxdepth 1 -mindepth 1 -type d)
14+
retval=0
15+
while read line; do
16+
if [[ "$line" != *ESP8266 && "$line" != *ESP32 ]]
17+
then
18+
echo -e "========================== BUILDING $line =========================="
19+
echo -e "${YELLOW}SKIPPING${NC}"
20+
continue
21+
fi
22+
echo -e "========================== BUILDING $line =========================="
23+
if [[ -e "$line/platformio.ini" ]]
24+
then
25+
# skipping
26+
#output=$(platformio ci --lib="." --project-conf="$line/platformio.ini" $line 2>&1)
27+
:
28+
else
29+
if [[ "$line" == *ESP8266 ]]
30+
then
31+
output=$(platformio ci --lib="." --project-conf="scripts/CI/platformio_esp8266.ini" $line 2>&1)
32+
else
33+
output=$(platformio ci --lib="." --project-conf="scripts/CI/platformio_esp32.ini" $line 2>&1)
34+
fi
35+
fi
36+
if [ $? -ne 0 ]; then
37+
echo "$output"
38+
echo -e "Building $line ${RED}FAILED${NC}"
39+
retval=1
40+
else
41+
echo -e "${GREEN}SUCCESS${NC}"
42+
fi
43+
done <<< "$lines"
44+
45+
# cleanup
46+
platformio lib -g uninstall AsyncTCP
47+
platformio lib -g uninstall ESPAsyncTCP
48+
49+
exit "$retval"

scripts/CI/platformio_esp32.ini

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[env:esp32]
12+
platform = espressif32
13+
board = esp32dev
14+
framework = arduino
15+
build_flags =
16+
-Wall

scripts/CI/platformio_esp8266.ini

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[env:esp8266]
12+
platform = espressif8266
13+
board = esp01_1m
14+
framework = arduino
15+
build_flags =
16+
-Wall

0 commit comments

Comments
 (0)