---
title: "W5500 pico coap server"
url: "https://maker.wiznet.io/Aimee0/projects/w5500-pico-coap-server/"
markdown_url: "https://maker.wiznet.io/Aimee0/projects/w5500-pico-coap-server/md"
type: "UCC: User Created Content"
author: "Aimee"
author_url: "https://maker.wiznet.io/Aimee0/"
original_author: "Aimee"
original_url: "https://github.com/aimee0000/RP2040-HAT-COAP_C"
published: "2024-05-03"
language: "en"
hardware: ["WIZnet W5500-EVB-Pico"]
likes: 0
views: 492
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# W5500 pico coap server

> This project includes examples of coap server using RP2040.

Original author: Aimee (source: https://github.com/aimee0000/RP2040-HAT-COAP_C)

## Components

- **WIZnet W5500-EVB-Pico** x 1 ([docs](https://docs.wiznet.io/Product/Chip/Ethernet/W5500/w5500-evb-pico))

## Article

This project uses w5500-evb-pico as a coap server and work with the client.

#### Project

Integrate microcoap library to RP2040-HAT-C project.

link : https://github.com/1248/microcoap

Library Folder

![](https://maker.wiznet.io/upload/ckeditor5/446449487%5F1714752171%2Epng)

Example Folder

![](https://maker.wiznet.io/upload/ckeditor5/446449487%5F1714752363%2Epng)

#endpoints.c

![](https://maker.wiznet.io/upload/ckeditor5/446449487%5F1714752425%2Epng)

![](https://maker.wiznet.io/upload/ckeditor5/446449487%5F1714752788%2Epng)

Create a handler function based on the message received from the client and then register it in the structure called endpoint.

# coapServer.c
Create a function called coapServer_run() to act as UDP.

```plaintext
void coapServer_run()
{
    int32_t ret, n;
    coap_packet_t pkt;
    uint16_t size = 0, sentsize=0;
    uint8_t scratch_raw[DATA_BUF_SIZE];
    coap_rw_buffer_t scratch_buf = {scratch_raw, sizeof(scratch_raw)};

    uint8_t  destip[4];
    uint16_t destport;


   switch(getSn_SR(COAPSock_Num))
   {
      case SOCK_UDP :
         if((size = getSn_RX_RSR(COAPSock_Num)) > 0)
         {
            if(size > DATA_BUF_SIZE) 
                size = DATA_BUF_SIZE;
            ret = recvfrom(COAPSock_Num, pCOAP_RX, size, destip, (uint16_t*)&destport);

#ifdef DEBUG
            printf("Receive: ");
            coap_dump(buf, ret, true);
            printf("\n");
#endif

            if (0 != (ret = coap_parse(&pkt, pCOAP_RX, ret)))
                printf("Bad packet rc=%d\n", ret);
            else
            {
                size_t rsplen = DATA_BUF_SIZE * sizeof(uint8_t);
                coap_packet_t rsppkt;
#ifdef DEBUG
                coap_dumpPacket(&pkt);
#endif
                coap_handle_req(&scratch_buf, &pkt, &rsppkt);

                if (0 != (ret = coap_build(pCOAP_TX, &rsplen, &rsppkt)))
                    printf("coap_build failed rc=%d\n", ret);
                else
                {
#ifdef DEBUG
                    printf("Sending: ");
                    coap_dump(buf, rsplen, true);
                    printf("\n");
#endif
#ifdef DEBUG
                    coap_dumpPacket(&rsppkt);
#endif
                    ret = sendto(COAPSock_Num, pCOAP_TX, rsplen, destip, destport);
                }
            }
         }
         break;
      case SOCK_CLOSED:
         if(socket(COAPSock_Num, Sn_MR_UDP, COAP_SERVER_PORT, 0x00) == COAPSock_Num)
        {
            printf("%d:Opened, UDP loopback, port [%d]\r\n", COAPSock_Num, COAP_SERVER_PORT);
        }
         break;
      default :
         break;
   }
}
```

#### How to use

**Download libcoap**

lilnk : <https://github.com/obgm/libcoap>

```plaintext
$ git clone https://github.com/obgm/libcoap.git
$ cd libcoap
$ git checkout main

$ cmake -E remove_directory build
$ cmake -E make_directory build
$ cd build
$ cmake .. -DENABLE_TESTS=ON
$ cmake --build .
$ cd Debug
```

**Load program to Pico Board**

![](https://maker.wiznet.io/upload/ckeditor5/446449487%5F1714750364%2Epng)

**Put message / Get message**

light on

![](https://maker.wiznet.io/upload/ckeditor5/446449487%5F1714755099%2Epng)

light off

![](https://maker.wiznet.io/upload/ckeditor5/446449487%5F1714755130%2Epng)

---

Source: https://maker.wiznet.io/Aimee0/projects/w5500-pico-coap-server/
