Wiznet makers

Aimee0

Published May 03, 2024 ©

17 UCC

10 WCC

8 VAR

0 Contests

0 Followers

0 Following

Original Link

W5500 pico coap server

This project includes examples of coap server using RP2040.

COMPONENTS Hardware components

WIZnet - W5500-EVB-Pico

x 1


PROJECT DESCRIPTION

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

Example Folder

 

#endpoints.c

 

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.


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

$ 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

 

Put message / Get message

light on

 

light off

 

 

 

Documents
Comments Write