You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+52-22Lines changed: 52 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,16 @@
1
1
# microCoAPy
2
+
2
3
A mini implementation of CoAP (Constrained Application Protocol) into MicroPython
3
4
4
5
The main difference compared to the established Python implementations [aiocoap](https://github.com/chrysn/aiocoap) and [CoAPthon](https://github.com/Tanganelli/CoAPthon) is its size and complexity since this library will be used on microcontrollers that support MicroPython such as: Pycom devices, ESP32, ESP8266.
5
6
6
7
The first goal of this implementation is to provide basic functionality to send and receive data. DTLS and/or any special features of CoAP as defined in the RFC's, will be examined and implemented in the future.
@@ -24,20 +27,37 @@ The first goal of this implementation is to provide basic functionality to send
24
27
-[Issues and contributions](#issues-and-contributions)
25
28
26
29
# Tested boards
27
-
* Pycom: all Pycom boards
28
-
* ESP32
29
-
* ESP8266
30
+
31
+
- Pycom: all Pycom boards
32
+
- ESP32
33
+
- ESP8266
34
+
35
+
# Installation
36
+
37
+
## Using `mip`
38
+
39
+
For network connected devices, call:
40
+
41
+
```
42
+
import mip
43
+
mip.install("github:insighio/microCoAPy")
44
+
```
45
+
46
+
## File Transfer
47
+
48
+
Download and transfer files in the board through [ampy](https://pypi.org/project/adafruit-ampy/).
30
49
31
50
# Supported operations
32
51
33
52
## CoAP client
34
-
* PUT
35
-
* POST
36
-
* GET
53
+
54
+
- PUT
55
+
- POST
56
+
- GET
37
57
38
58
### Example of usage
39
-
Here is an example using the CoAP client functionality to send requests and receive responses. (this example is part of [examples/pycom_wifi_coap_client.py](https://github.com/insighiot/microCoAPy/blob/master/examples/pycom_wifi_coap_client.py))
40
59
60
+
Here is an example using the CoAP client functionality to send requests and receive responses. (this example is part of [examples/pycom_wifi_coap_client.py](https://github.com/insighiot/microCoAPy/blob/master/examples/pycom_wifi_coap_client.py))
41
61
42
62
```python
43
63
import microcoapy
@@ -62,6 +82,7 @@ client.stop()
62
82
```
63
83
64
84
#### Code explained
85
+
65
86
Lets examine the above code and explain its purpose.
During this step, the CoAP object get initialized. A callback handler is also created to get notifications from the server regarding our requests. __It is not used for incoming requests.__
76
96
77
-
When instantiating new Coap object, a custom port can be optionally configured: *client = microcoapy.Coap(5683)*.
97
+
During this step, the CoAP object get initialized. A callback handler is also created to get notifications from the server regarding our requests. **It is not used for incoming requests.**
98
+
99
+
When instantiating new Coap object, a custom port can be optionally configured: _client = microcoapy.Coap(5683)_.
Having the socket ready, it is time to send our request. In this case we send a simple GET request to the specific address (ex. 192.168.1.2:5683). The [_get_](https://github.com/insighio/microCoAPy/wiki#getip-port-url) function returns the number of bytes that have been sent. So in case of error, 0 will be returned.
112
+
Having the socket ready, it is time to send our request. In this case we send a simple GET request to the specific address (ex. 192.168.1.2:5683). The [_get_](https://github.com/insighio/microCoAPy/wiki#getip-port-url) function returns the number of bytes that have been sent. So in case of error, 0 will be returned.
91
113
92
114
```python
93
115
client.poll(2000)
@@ -101,14 +123,17 @@ If a packet gets received during that period of type that is an _ACK_ to our req
101
123
client.stop()
102
124
```
103
125
104
-
Finally, stop is called to gracefully close the socket. It is preferable to have a corresponding call of [_stop_](https://github.com/insighio/microCoAPy/wiki#stop) to each call of [_start_](https://github.com/insighio/microCoAPy/wiki#startport) function because in special cases such as when using mobile modems, the modem might stuck when running out of available sockets.
126
+
Finally, stop is called to gracefully close the socket. It is preferable to have a corresponding call of [_stop_](https://github.com/insighio/microCoAPy/wiki#stop) to each call of [_start_](https://github.com/insighio/microCoAPy/wiki#startport) function because in special cases such as when using mobile modems, the modem might stuck when running out of available sockets.
105
127
106
128
To send POST or PUT message replace the call of _get_ function with:
For details on the arguments please advice the [documentation](https://github.com/insighio/microCoAPy/wiki).
118
143
119
144
## CoAP server
145
+
120
146
Starts a server and calls custom callbacks upon receiving an incoming request. The response needs to be defined by the user of the library.
121
147
122
148
### Example of usage
@@ -149,6 +175,7 @@ client.stop()
149
175
```
150
176
151
177
#### Code explained
178
+
152
179
Lets examine the above code and explain its purpose. For details on [_start_](https://github.com/insighio/microCoAPy/wiki#startport) and [_stop_](https://github.com/insighio/microCoAPy/wiki#stop) functions advice the previous paragraph of the client example.
This is the main step to prepare the CoAP instance to behave as a server: receive and handle requests. First we create a function _measureCurrent_ that takes as arguments the incoming packet, the sender IP and Port. This function will be used as a callback and will be triggered every time a specific URI path is provided in the incoming request.
191
+
This is the main step to prepare the CoAP instance to behave as a server: receive and handle requests. First we create a function _measureCurrent_ that takes as arguments the incoming packet, the sender IP and Port. This function will be used as a callback and will be triggered every time a specific URI path is provided in the incoming request.
165
192
166
193
This URL is defined upon registering the callback to the CoAP instance by calling [_addIncomingRequestCallback_](https://github.com/insighio/microCoAPy/wiki#addincomingrequestcallbackrequesturl-callback) function. After this call, if a CoAP GET/PUT/POST packet is received with URI path: coap://<IP>/current/measure , the callback will be triggered.
167
194
@@ -176,19 +203,21 @@ while time.ticks_diff(time.ticks_ms(), start_time) < timeoutMs:
176
203
client.poll(60000)
177
204
```
178
205
179
-
Finally, since the functions [_loop_](https://github.com/insighio/microCoAPy/wiki#loopblocking) and [_poll_](https://github.com/insighio/microCoAPy/wiki#polltimeoutms-pollperiodms)__can handle a since packet per run__, we wrap its call to a while loop and wait for incoming messages.
206
+
Finally, since the functions [_loop_](https://github.com/insighio/microCoAPy/wiki#loopblocking) and [_poll_](https://github.com/insighio/microCoAPy/wiki#polltimeoutms-pollperiodms)**can handle a since packet per run**, we wrap its call to a while loop and wait for incoming messages.
180
207
181
208
## Custom sockets
182
-
By using default functions __microcoapy.Coap().start()__ and __microcoapy.Coap().stop()__ the Coap library handles the creation of a UDP socket from **usocket module** at the default port 5683 (if no other is defined when Coap object gets instantiated).
209
+
210
+
By using default functions **microcoapy.Coap().start()** and **microcoapy.Coap().stop()** the Coap library handles the creation of a UDP socket from **usocket module** at the default port 5683 (if no other is defined when Coap object gets instantiated).
183
211
184
212
If this socket type is not the appropriate for your project, custom socket instances can be used instead.
185
213
186
214
Lets consider the case of supporting an external GSM modem connected via Serial on the board and that there is no direct support of this modem from default modules like **network.LTE**. In this case there is no guarranty that a typical UDP socket from usocket module will be functional. Thus, a custom socket instance needs to be created.
187
215
188
216
The custom socket needs to implement the functions:
189
-
* sendto(self, bytes, address) : returns the number of bytes transmitted
190
-
* recvfrom(self, bufsize): returns a byte array
191
-
* setblocking(self, flag)
217
+
218
+
- sendto(self, bytes, address) : returns the number of bytes transmitted
Since most of the implementations of NBIoT networks are based on IPv6, it was essential to move to a custom implementation of UDP socket, as Pycom do not yet support natively IPv6 sockets. Thus, in [examples/pycom/nbiot/pycom_at_socket.py](https://github.com/insighio/microCoAPy/blob/master/examples/pycom/nbiot/pycom_at_socket.py) you can find a complete implementation of a sample socket that directly uses Sequans AT commands.
260
+
Since most of the implementations of NBIoT networks are based on IPv6, it was essential to move to a custom implementation of UDP socket, as Pycom do not yet support natively IPv6 sockets. Thus, in [examples/pycom/nbiot/pycom_at_socket.py](https://github.com/insighio/microCoAPy/blob/master/examples/pycom/nbiot/pycom_at_socket.py) you can find a complete implementation of a sample socket that directly uses Sequans AT commands.
232
261
233
262
NOTE: The socket to work without limitations needs one of the following Pycom firmwares:
0 commit comments