Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple changes #7

Open
wants to merge 48 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
537d7ab
Changed file structure, made module and updated for LoBo-port.
kevinkk525 Mar 9, 2018
b90de90
removed config dictionary
kevinkk525 Mar 14, 2018
9db5b99
Minimal version for esp8266
kevinkk525 Mar 15, 2018
f45a15d
extended class Lock with method locked
kevinkk525 Mar 15, 2018
e0ef862
Added missing documentation, bugfixes
kevinkk525 Mar 27, 2018
8526ecd
design fixes in documentation
kevinkk525 Mar 27, 2018
30b9104
Merge pull request #1 from kevinkk525/module
kevinkk525 Mar 29, 2018
be350c5
Added information about diff to base repo
kevinkk525 Mar 29, 2018
9f46ea5
Added information about diff to base repo
kevinkk525 Mar 29, 2018
d9b52f4
added Lock.release
kevinkk525 Jun 25, 2018
ef1eb92
Added information about diff to base repo
kevinkk525 Mar 29, 2018
5bb0091
Added information about diff to base repo
kevinkk525 Mar 29, 2018
7120bd0
Add support for unsubscribe, recognize retained messages
kevinkk525 Sep 27, 2018
6549016
Merge remote-tracking branch 'origin/master'
kevinkk525 Sep 27, 2018
490d9e0
Updated README
kevinkk525 Sep 27, 2018
3bb844d
Reliability update
kevinkk525 Sep 28, 2018
7fe9b25
Update README to reliability fix
kevinkk525 Sep 28, 2018
61158d7
Even more reliability
kevinkk525 Sep 29, 2018
dde685f
Bugfix introduced in last commit
kevinkk525 Sep 29, 2018
7b5197c
Remove workarounds needed before micropython 1.10.
kevinkk525 Jan 29, 2019
ddc23bd
Pulled updates from upstream to support Pyboard D, minor changes
kevinkk525 Jul 2, 2019
842fae3
Added support for unix port of Micropython, small bugfixes
kevinkk525 Jul 2, 2019
66df3da
Rollback renaming the package
kevinkk525 Jul 2, 2019
0745c56
Merge branch 'master' of https://github.com/peterhinch/micropython-mqtt
kevinkk525 Sep 12, 2019
f0a90f2
Removed minimal version as RAM increase is also minimal by now
kevinkk525 Sep 12, 2019
086d39c
Merge branch 'master' of https://github.com/peterhinch/micropython-mqtt
kevinkk525 Sep 28, 2019
2b34c9b
Merge branch 'master' of https://github.com/peterhinch/micropython-mqtt
kevinkk525 Oct 19, 2019
27615df
Possibility to use timeouts with publish coroutine.
kevinkk525 Oct 19, 2019
339e0f5
Updated Readme
kevinkk525 Oct 19, 2019
a6c35b4
Updated Readme
kevinkk525 Oct 19, 2019
3c7e8db
Merge branch 'master' of https://github.com/peterhinch/micropython-mqtt
kevinkk525 Oct 31, 2019
7fc2542
Pulled upstream patches, adapted unsubscribe
kevinkk525 Oct 31, 2019
1e55dd2
Broke the Lock by removing locked() and released()
kevinkk525 Oct 31, 2019
2e39332
Updated README
kevinkk525 Oct 31, 2019
75f8997
Updated README
kevinkk525 Oct 31, 2019
42ae4eb
Updated README
kevinkk525 Oct 31, 2019
87324e7
Merge branch 'master' of https://github.com/peterhinch/micropython-mqtt
kevinkk525 Nov 4, 2019
bd6b80e
Merged upstream patches
kevinkk525 Nov 4, 2019
e0b0646
delete .idea
kevinkk525 Nov 4, 2019
edf1ea0
remove gitignore
kevinkk525 Nov 4, 2019
eb66b97
concurrent timeout subclass for all operations
kevinkk525 Nov 4, 2019
b3e5055
Merge branch 'master' of https://github.com/peterhinch/micropython-mqtt
kevinkk525 Nov 4, 2019
5ea7b82
concurrent timeout subclass for all operations with no queue limitation
kevinkk525 Nov 4, 2019
a5d1404
bugfix, testcase
kevinkk525 Nov 5, 2019
e840a6d
timeout option with wait_for uasyncio extension from
kevinkk525 Nov 6, 2019
2145972
timeout option with wait_for() uasyncio extension from
kevinkk525 Nov 6, 2019
c5b5829
support new uasyncio.
kevinkk525 Mar 26, 2020
6e25ac7
new implementation for concurrent timeout operations
kevinkk525 Apr 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 0 additions & 101 deletions .gitignore

This file was deleted.

41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
# Update: This library can now only be used with the new uasyncio version that got merged into the main micropython repository!

# NOTE:
With the latest commits in the original repository by Peter Hinch, this module is now <b>USEABLE AS A DROP-IN REPLACEMENT!</b> Only change needed is how the MQTTClient is created. Look [here](./README_mqtt_as.md#23-example-usage) for an example, it is simple, the pythonic way.

# Changes to base repo of Peter Hinch

1. Sorted files and made a structure so you know which file belongs where without reading the documentation every time
2. Made repo a module to be used like
*from micropython_mqtt_as.mqtt_as import MQTTClient
from micropython_mqtt_as.config import config*
making it possible to just clone the repo and copy it to `espXXXX/modules` also reducing file clutter in this directory.
3. ~~Removed unnecessary workarounds of official ESP32 port for ESP32 loboris fork (Feel free to report issues).~~ implemented upstream
4. Changed MQTTClient constructor initialization from using a dictionary to using keywords with default parameters. It's still possible to use the dictionary for initialization with almost no changes to existing codebase
5. ~~Made a minimal version of mqtt_as for the ESP8266 to save some RAM~~ Removed again as the removing of workarounds in the main version only got ~150B less RAM usage which is not worth the effort.
6. Added support for "unsubscribe"
7. ~~Added support for recognizing retained publications (makes change in "subs_cb" necessary as it now has to take 3 args [topic,msg,retained])~~ implemented upstream
8. All other files are updated to the new changes and are usable (e.g. tests).
9. Updated documentation to reflect all changes
10. ~~Fixes a reliability problem when having many subscribe/unsubscribe in a short time, resulting endless reconnects (see commit for changes and explanation)~~ fixed upstream, real concurrent operations possible now
11. Added support for the unix port of Micropython

Motivation for the changes:
For my project I had to adapt the library to use it on the ESP32 with loboris fork but also use it on my ESP8266 that is short on RAM all the time.
Therefore I had the following motivation for each of the above mentioned changes:
1. I don't like to walk through a mess of files not knowing which one is important or where it belongs to and I don't want to read all the documentation just to know which files belong where.
2. Like all modules this should be a directory as well, making usage easier.
3. ~~Made it work with loboris fork but did not want to use workarounds that are not needed on this fork. (Peter Hinch made it work with loboris port as well but has the workarounds still in it to be safe)~~
4. I felt that this kind of initialization is the more pythonic way of doing things but apart from that it has an important advantage on the ESP8266, removing the config dict completely uses 100-200 Bytes less, which is important on ESP8266.
5. ~~This version for the ESP8266 has all non related code (workarounds for ESP32) and also some not commonly functions removed, saving another 150-250 Bytes so that after all changes I get 250-450 Bytes more RAM which is about 2% of the available RAM.~~
6. At first I did not need that but later it became important to me so I added it
7. I made a huge workaround in a subclass to recognize retained messages instead of just supporting it directly
8. Although I do not need any other file I felt that it is important to finish the work I started and not leave half the repo unusable.
9. Wouldn't want issues because of wrong documentation or frustrated users. Have fun with it :D
10. ~~was simply needed. Sadly makes the module a little bigger~~
11. Needed that to run my projects on a Pi and it's great for testing code

# Introduction

MQTT is an easily used networking protocol designed for IOT (internet of
Expand Down Expand Up @@ -38,7 +75,7 @@ It has the drawback of increased code size which is an issue on the ESP8266.
Run as frozen bytecode it uses about 50% of the RAM on the ESP8266. On ESP32
and Pyboard D it may be run as a standard Python module.

It is documented [here](./mqtt_as/README.md).
It is documented [here](./README_mqtt_as.md).

## 2. MQTT for generic MicroPython targets

Expand All @@ -50,4 +87,4 @@ five GPIO pins accessible via the `machine` library should suffice.

The driver is non-blocking and is designed for applications using uasyncio.

It is documented [here](./NO_NET.md).
It is documented [here](./remote_mqtt/NO_NET.md).
120 changes: 72 additions & 48 deletions mqtt_as/README.md → README_mqtt_as.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,43 @@ but duplication can occur. Level 2 avoids duplication; it is not supported by
the official driver or by this module. Duplicates can readily be handled at the
application level.

###### [Main README](../README.md)
###### [Main README](./README.md)

# 1. Contents

1. [Contents](./README.md#1-contents)
1.1 [Rationale](./README.md#11-rationale)
1.2 [Overview](./README.md#12-overview)
1.3 [Project Status](./README.md#13-project-status)
1.4 [ESP8266 Limitations](./README.md#14-esp8266-limitations)
1.5 [ESP32 Issues](./README.md#15-esp32-issues)
1.6 [Pyboard D](./README.md#16-pyboard-d)
1.7 [Dependency](./README.md#17-dependency)
2. [Getting started](./README.md#2-getting_started)
2.1 [Program files](./README.md#21-program-files)
2.2 [Installation](./README.md#22-installation)
2.3 [Example Usage](./README.md#23-example-usage)
3. [MQTTClient class](./README.md#3-mqttclient-class)
3.1 [Constructor](./README.md#31-constructor)
3.2 [Methods](./README.md#32-methods)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.1 [connect](./README.md#321-connect)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.2 [publish](./README.md#322-publish)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.3 [subscribe](./README.md#323-subscribe)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.4 [isconnected](./README.md#324-isconnected)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.5 [disconnect](./README.md#325-disconnect)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.6 [close](./README.md#326-close)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.7 [broker_up](./README.md#327-broker_up)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.8 [wan_ok](./README.md#328-wan_ok)
3.3 [Class Variables](./README.md#33-class-variables)
3.4 [Module Attribute](./README.md#34-module-attribute)
4. [Notes](./README.md#4-notes)
4.1 [Connectivity](./README.md#41-connectivity)
4.2 [Client publications with qos == 1](./README.md#42-client-publications-with-qos-1)
4.3 [Client subscriptions with qos == 1](./README.md#43-client-subscriptions-with-qos-1)
4.4 [Application Design](./README.md#44-application-design)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4.4.1 [Publication Timeouts](./README.md#441-publication-timeouts)
5. [Low Power Demo](./README.md#5-low-power-demo) Note: Pyboard D specific and highly experimental.
6. [References](./README.md#6-references)
1. [Contents](./README.md#1-contents)
1.1 [Rationale](./README.md#11-rationale)
1.2 [Overview](./README.md#12-overview)
1.3 [Project Status](./README.md#13-project-status)
1.4 [ESP8266 Limitations](./README.md#14-esp8266-limitations)
1.5 [ESP32 Issues](./README.md#15-esp32-issues)
1.6 [Pyboard D](./README.md#16-pyboard-d)
1.7 [Dependency](./README.md#17-dependency)
2. [Getting started](./README.md#2-getting_started)
2.1 [Program files](./README.md#21-program-files)
2.2 [Installation](./README.md#22-installation)
2.3 [Example Usage](./README.md#23-example-usage)
3. [MQTTClient class](./README.md#3-mqttclient-class)
3.1 [Constructor](./README.md#31-constructor)
3.2 [Methods](./README.md#32-methods)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.1 [connect](./README.md#321-connect)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.2 [publish](./README.md#322-publish)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.3 [subscribe](./README.md#323-subscribe)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.4 [isconnected](./README.md#324-isconnected)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.5 [disconnect](./README.md#325-disconnect)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.6 [close](./README.md#326-close)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.7 [broker_up](./README.md#327-broker_up)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.8 [wan_ok](./README.md#328-wan_ok)
3.3 [Class Variables](./README.md#33-class-variables)
3.4 [Module Attribute](./README.md#34-module-attribute)
4. [Notes](./README.md#4-notes)
4.1 [Connectivity](./README.md#41-connectivity)
4.2 [Client publications with qos == 1](./README.md#42-client-publications-with-qos-1)
4.3 [Client subscriptions with qos == 1](./README.md#43-client-subscriptions-with-qos-1)
4.4 [Application Design](./README.md#44-application-design)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4.4.1 [Publication Timeouts](./README.md#441-publication-timeouts)
5. [Low Power Demo](./README.md#5-low-power-demo) Note: Pyboard D specific and highly experimental.
6. [References](./README.md#6-references)

## 1.1 Rationale

Expand Down Expand Up @@ -97,7 +97,7 @@ modified for resilience and for asynchronous operation.

Hardware support: Pyboard D, ESP8266 and ESP32.
Firmware support: Official firmware. Limited support for ESP32 Loboris port.
Broker support: Mosquitto is preferred for its excellent MQTT compliance.
Broker support: Mosquitto is preferred for its excellent MQTT compliance.
Protocol: Currently the module supports a subset of MQTT revision 3.1.1.

Initial development was by Peter Hinch. Thanks are due to Kevin Köck for
Expand All @@ -109,16 +109,22 @@ providing and testing a number of bugfixes and enhancements.
SSL/TLS now tested successfully on Pyboard D.
Fix bug where ESP8266 could hang attempting to connect.
Can now reconnect after disconnect is issued.
Now supports concurrent qos==1 publications and subscriptions.
Now supports concurrent qos==1 publications and subscriptions.
**API change** The disconnect method is now asynchronous.

24th Sept 2019
24th Sept 2019
**API change:** the subscription callback requires an additional parameter for
the retained message flag.
On ESP8266 the code disables automatic sleep: this reduces reconnects at cost
of increased power consumption.
of increased power consumption.

1st April 2019
2nd July 2019
Added support for the unix port of Micropython. The unique_id must be set manually
as the unix port doesn't have the function *unique_id()* to read a chip's id.
The library assumes that the device is correctly connected to the network as the OS
will take care of the network connection.

1st April 2019
In the light of improved ESP32 firmware and the availability of the Pyboard D
the code has minor changes to support these platforms.

Expand Down Expand Up @@ -178,7 +184,7 @@ fork and its library, but this has not been tested.
### Experimental scripts

1. `lowpower.py` Pybaord D micro-power test. See [Section 5](./README.md#5-low-power-demo).
2. `tls8266.py` SSL/TLS connectionfor ESP8266. Fails with
2. `tls8266.py` SSL/TLS connectionfor ESP8266. Fails with
`ssl_handshake_status: -4`.
3. `tls32.py` SSL/TLS connection for ESP32. Fails with
`mbedtls_ssl_handshake error: -77`.
Expand All @@ -197,7 +203,7 @@ platforms, or to have the capability of running on an ESP8266 which has not
previously connected, `config.py` should be edited to provide them. This is a
sample cross-platform file:
```python
from mqtt_as import config
from micropython_mqtt_as.mqtt_as import config

config['server'] = '192.168.0.10' # Change to suit e.g. 'iot.eclipse.org'

Expand Down Expand Up @@ -230,9 +236,10 @@ with the topic `foo_topic` the topic and message are printed. The code
periodically publishes an incrementing count under the topic `result`.

```python
from mqtt_as import MQTTClient
from config import config
from micropython_mqtt_as.mqtt_as import MQTTClient
from micropython_mqtt_as.config import config
import uasyncio as asyncio
from sys import platform

SERVER = '192.168.0.10' # Change to suit e.g. 'iot.eclipse.org'

Expand All @@ -255,9 +262,13 @@ async def main(client):
config['subs_cb'] = callback
config['connect_coro'] = conn_han
config['server'] = SERVER
if platform == "linux":
config["client_id"]="linux"

MQTTClient.DEBUG = True # Optional: print diagnostic messages
client = MQTTClient(config)
client = MQTTClient(**config) # Using dict to stay compatible to upstream.
# Alternatively initialize MQTTClient the pythonic way using arguments like:
# client = MQTTClient(server=SERVER, port=1883, ...)
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main(client))
Expand Down Expand Up @@ -286,9 +297,12 @@ The module provides a single class: `MQTTClient`.

## 3.1 Constructor

This takes a dictionary as argument. The default is `mqtt_as.config`. Normally
an application imports this and modifies selected entries as required. Entries
are as follows (default values shown in []):
This takes all keywords found in the dictionary in `config.py` as argument.
As a convenience you can also use this dictionary by importing it and changing
the values. You then call the constructor by `MQTTClient(**config)`, this
automatically matches the contents of the dict to the keywords of the constructor.

Entries of config dictionary are:

**WiFi Credentials**

Expand Down Expand Up @@ -446,6 +460,16 @@ to '8.8.8.8' and checks for a valid response.
There is a single arg `packet` which is a bytes object being the DNS query. The
default object queries the Google DNS server.

### 3.2.9 unsubscribe (async)

Unsubscribes a topic, so no messages will be received anymore.

The coro will pause until a `UNSUBACK` has been received from the broker, if
necessary reconnecting to a failed network.

Args:
1. `topic`

## 3.3 Class Variables

1. `DEBUG` If `True` causes diagnostic messages to be printed.
Expand Down Expand Up @@ -597,7 +621,7 @@ not detected.
[mosquitto server](http://mosquitto.org/man/mosquitto-8.html)
[mosquitto client publish](http://mosquitto.org/man/mosquitto_pub-1.html)
[mosquitto client subscribe](http://mosquitto.org/man/mosquitto_sub-1.html)
[MQTT 3.1.1 spec](http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718048)
[MQTT 3.1.1 spec](http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718048)
[python client for PC's](https://www.eclipse.org/paho/clients/python/)
[Unofficial MQTT FAQ](https://forum.micropython.org/viewtopic.php?f=16&t=2239)

Expand Down
Empty file added __init__.py
Empty file.
Loading