insighio / microCoAPy

A mini client/server implementation of CoAP (Constrained Application Protocol) into MicroPython
Apache License 2.0
57 stars 10 forks source link

Suggestion for improvement: possibility to use custom methods for socket operations #1

Closed dmartauz closed 4 years ago

dmartauz commented 4 years ago

In case specific network interface is not mapped to internal IP stack and not available through socket module it would be beneficial if custom methods for socket operations could be used. E.g. they could make use of socket API of cellular modem controlled via AT commands.

ftylitak commented 4 years ago

Hello @dmartauz

to my understanding you suggest to decouple the CoAP logic from the socket operations. Correct?

Indeed it is a good idea and future-proof. The initial version was implemented as is because on Pycom devices, the sockets are already encapsulated by the micropython API. So in case of WiFi, it is a typical UDP socket, whereas on NBIoT it is AT commands sent to the modem to handle the UDP socket on the modem.

Do you have any specific example of a user case where the network interface is not mapped to the internal IP stack? If yes, it would be a good input for us to use it as test scenario.

Eitherway, during next week we will proceed to the decoupling and we will keep you updated.

dmartauz commented 4 years ago

@ftylitak yes your understanding is correct.

My setup is ESP32 (WROVER module) with Quectel BC66 NB-IoT modem on a custom board. I already performed some testing of your lib against Thingsboard. First time without any modifications over WiFi connection, second time I passed content of "buffer" bytearray from sendPacket method to the NB-IoT modem via AT command. Worked as expected in both cases.

ftylitak commented 4 years ago

@dmartauz I am very happy it worked as expected.

A proper example code for ESP32 was within our plans, so this change that you suggested is a good opportunity to proceed on doing.

My estimation is that within this week we will have a first version. We will keep you updated.

ftylitak commented 4 years ago

@dmartauz,

in branch IN-209-esp32-example there is a proposed solution to your suggestion through Pull Request #2 .

The branch also contains an example for ESP32 that show how to use a custom socket.

The custom dummy socket that has been used is: https://github.com/insighio/microCoAPy/blob/a88427004cf22d247882a18992d6b865a8c45513/examples/esp32_wifi_coap_client_custom_socket.py#L67-L83

and is set in microCoAPy instance as: https://github.com/insighio/microCoAPy/blob/a88427004cf22d247882a18992d6b865a8c45513/examples/esp32_wifi_coap_client_custom_socket.py#L91-L95

Could you give this branch a try? Does this fullfil your requirements?

ftylitak commented 4 years ago

Closing the issue due to inactivity.

If there is any suggestion of a different approach on this subject, please open an new issue.