Git Product home page Git Product logo

coap_server_for_stm32f407_discovery's Introduction

Coap server example for STM32f407 Discovery using LAN8720

I started this example using my example for STM32f407 Discovery with LWIP socket then to create coap server i imported libcoap library to the project with the help of porting used in espressif esp-idf.

Steps to import Libcoap Llibrary to our STM32f407 Discovery

  • Download the libcoap library then unzip the downloaded folder to folder named libcoap and copy that folder into project in path coap_server_example_for_STM32f407_Discovery\Middlewares\Third_Party\libcoap

    Note that according to espressif esp-idf we did not use the latest version of libcoap , we used version 2 for libcoap library with specified commit which can be downloaded from here

  • Download the port folder created by espressif esp-idf from here and save that port folder inside our created libcoap folder sothat it will have the path coap_server_example_for_STM32f407_Discovery\Middlewares\Third_Party\libcoap\port

  • Create new folder called application in the path \coap_server_example_for_STM32f407_Discovery\Middlewares\Third_Party\libcoap\examples\application where it contains source and header files for our coap API task (server.c , server.h and common.h )

  • As IPv6 is included in libcoap so we need to download LWIP IPv6 folder and add it in our project in path coap_server_example_for_STM32f407_Discovery\Middlewares\Third_Party\LwIP\src\core

  • Add the downloaded sources files in STM32Cube IDE

    • in project explorer tab in STM32Cube IDE add new folder named libcoap to Middlewares and also folders named IPv6,application and src constructing that structure
      ├── Middlewares
      │   ├── libcoap
      │       ├── application
      │       └── src
      │   ├── FreeRtos
      │   ├── LwIP
      │       ├── Api
      │       ├── apps
      │       ├── Core
      │       │   ├── IPv4
      │       │   └── Ipv6
      │       └── Neif
      
    • right click on application then choose import to import file server.c from path \coap_server_example_for_STM32f407_Discovery\Middlewares\Third_Party\libcoap\examples\application with refrence to project location
    • repeat previos step to add source files of libcoap library in src sufolder and also for IPv6 source files
  • We need to exclude from build the file coap_io_lwip.c which will be located in STM32Cube IDE in \Middlewares\libcoap\src so in project explorer tab richt click on that file then click on properties then check box for exclude resource from build and click apply

  • For compiling in STM32Cube IDE include the following paths

    • ../../../Middlewares/Third_Party/LwIP/src/include/compat/posix
    • ../../../Middlewares/Third_Party/libcoap/include/coap2
    • ../../../Middlewares/Third_Party/libcoap/port/include
    • ../../../Middlewares/Third_Party/libcoap/port/include/coap
    • ../../../Middlewares/Third_Party/libcoap/examples/application

Modifications to use with STM32

  • In lwipopts.h add the following

    #define LWIP_IPV6                       1
    #define LWIP_DNS                        1
    #define LWIP_TCPIP_CORE_LOCKING         0
    #define LWIP_DNS_SUPPORT_MDNS_QUERIES 1
    #define DNS_SERVER_ADDRESS(ipaddr) ip_addr_set_ip4_u32(ipaddr,ipaddr_addr("8.8.8.8"))
    #define DNS_MAX_SERVERS                 3
    #define DNS_FALLBACK_SERVER_INDEX        (DNS_MAX_SERVERS - 1)
    
  • In coap_mutex.h add the following

    #define PTHREAD_MUTEX_INITIALIZER ((pthread_mutex_t) 0xFFFFFFFF)

  • For files coap_io.c,address.c,libcoap.h and net.c

    replace # include <netinet/in.h> with :

    #include "lwip/inet.h"

  • In file \coap_server_example_for_STM32f407_Discovery\Middlewares\Third_Party\LwIP\src\include\lwip\inet.h add the following

    #define IN6_IS_ADDR_MULTICAST(a) IN_MULTICAST(a)

  • In file coap_server_example_for_STM32f407_Discovery\Middlewares\Third_Party\libcoap\port\include\coap_config_posix.h edit the definitions for the following :

    #define COAP_CONSTRAINED_STACK 0

    #define _POSIX_TIMERS 1

  • Add two files time.c in coap_server_example_for_STM32f407_Discovery\Src and time.h in coap_server_example_for_STM32f407_Discovery\Incto give coap ticks time and import that source file time.c in STM32Cube IDE under coap_server_example_for_STM32f407_Discovery\Application\User subfolder.

Code explanation

I created task for CoAP server which can receive requests / data from CoAP client and transmit data to CoAP client , in our coap server i create only one resource /hello with initial data "Hello World!" and that resource will support coap methods get , put and delete while client can query or change that resource data as well as our created resource is observable to allow coap clients to observe our resource data

How to test the code

We can use coap-cli tool to test all coap client methods supported by our server such as:

* coap get coap://[STM board IP]/hello
* echo 'data to send' | coap put coap://[STM board IP]/hello
* coap delete coap://[STM board IP]/hello 
* coap get -o coap://[STM board IP]/hello 

coap_server_for_stm32f407_discovery's People

Contributors

ahosny333 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.