Skip to content

Commit 2525274

Browse files
committed
Added installation instructions
1 parent a1c37b1 commit 2525274

File tree

1 file changed

+52
-22
lines changed

1 file changed

+52
-22
lines changed

README.md

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# microCoAPy
2+
23
A mini implementation of CoAP (Constrained Application Protocol) into MicroPython
34

45
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.
56

67
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.
78

89
# Table of contents
10+
911
- [Tested boards](#tested-boards)
1012
- [Documentation](https://github.com/insighio/microCoAPy/wiki)
13+
- [Installation](#installation)
1114
- [Supported operations](#supported-operations)
1215
- [CoAP client](#coap-client)
1316
- [Example of usage](#example-of-usage)
@@ -24,20 +27,37 @@ The first goal of this implementation is to provide basic functionality to send
2427
- [Issues and contributions](#issues-and-contributions)
2528

2629
# 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/).
3049

3150
# Supported operations
3251

3352
## CoAP client
34-
* PUT
35-
* POST
36-
* GET
53+
54+
- PUT
55+
- POST
56+
- GET
3757

3858
### 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))
4059

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))
4161

4262
```python
4363
import microcoapy
@@ -62,6 +82,7 @@ client.stop()
6282
```
6383

6484
#### Code explained
85+
6586
Lets examine the above code and explain its purpose.
6687

6788
```python
@@ -72,9 +93,10 @@ def receivedMessageCallback(packet, sender):
7293
client = microcoapy.Coap()
7394
client.responseCallback = receivedMessageCallback
7495
```
75-
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.__
7696

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)_.
78100

79101
```python
80102
client.start()
@@ -87,7 +109,7 @@ bytesTransferred = client.get(_SERVER_IP, _SERVER_PORT, "current/measure")
87109
print("[GET] Sent bytes: ", bytesTransferred)
88110
```
89111

90-
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.
91113

92114
```python
93115
client.poll(2000)
@@ -101,14 +123,17 @@ If a packet gets received during that period of type that is an _ACK_ to our req
101123
client.stop()
102124
```
103125

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.
105127

106128
To send POST or PUT message replace the call of _get_ function with:
129+
107130
```python
108131
bytesTransferred = client.put(_SERVER_IP, _SERVER_PORT, "led/turnOn", "test",
109132
None, microcoapy.COAP_CONTENT_FORMAT.COAP_TEXT_PLAIN)
110133
```
134+
111135
or
136+
112137
```python
113138
bytesTransferred = client.post(_SERVER_IP, _SERVER_PORT, "led/turnOn", "test",
114139
None, microcoapy.COAP_CONTENT_FORMAT.COAP_TEXT_PLAIN)
@@ -117,6 +142,7 @@ bytesTransferred = client.post(_SERVER_IP, _SERVER_PORT, "led/turnOn", "test",
117142
For details on the arguments please advice the [documentation](https://github.com/insighio/microCoAPy/wiki).
118143

119144
## CoAP server
145+
120146
Starts a server and calls custom callbacks upon receiving an incoming request. The response needs to be defined by the user of the library.
121147

122148
### Example of usage
@@ -149,6 +175,7 @@ client.stop()
149175
```
150176

151177
#### Code explained
178+
152179
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.
153180

154181
```python
@@ -161,7 +188,7 @@ def measureCurrent(packet, senderIp, senderPort):
161188
client.addIncomingRequestCallback('current/measure', measureCurrent)
162189
```
163190

164-
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.
165192

166193
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.
167194

@@ -176,19 +203,21 @@ while time.ticks_diff(time.ticks_ms(), start_time) < timeoutMs:
176203
client.poll(60000)
177204
```
178205

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.
180207

181208
## 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).
183211

184212
If this socket type is not the appropriate for your project, custom socket instances can be used instead.
185213

186214
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.
187215

188216
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
219+
- recvfrom(self, bufsize): returns a byte array
220+
- setblocking(self, flag)
192221

193222
Example:
194223

@@ -228,11 +257,12 @@ client.setCustomSocket(customSocket)
228257

229258
## Pycom custom socket based on AT commands
230259

231-
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.
232261

233262
NOTE: The socket to work without limitations needs one of the following Pycom firmwares:
234-
* [Pygate Firmware Release v1.20.2.rc11](https://github.com/pycom/pycom-micropython-sigfox/releases/tag/v1.20.2.rc11_pygate) (or newer)
235-
* [Firmware Release v1.20.2.r1](https://github.com/pycom/pycom-micropython-sigfox/releases/tag/v1.20.2.r1) (or newer)
263+
264+
- [Pygate Firmware Release v1.20.2.rc11](https://github.com/pycom/pycom-micropython-sigfox/releases/tag/v1.20.2.rc11_pygate) (or newer)
265+
- [Firmware Release v1.20.2.r1](https://github.com/pycom/pycom-micropython-sigfox/releases/tag/v1.20.2.r1) (or newer)
236266

237267
# Beta features under implementation or evaluation
238268

@@ -258,8 +288,8 @@ client.debug = False
258288

259289
# Future work
260290

261-
* Since this library is quite fresh, the next period will be full of testing.
262-
* enhancments on funtionality as needed
291+
- Since this library is quite fresh, the next period will be full of testing.
292+
- enhancments on funtionality as needed
263293

264294
# Issues and contributions
265295

0 commit comments

Comments
 (0)