V tomto článku se podíváme, jak nastavit na PicoW bezestavově IPv6 adresu a používat UDP protokol k posílání dat na server. PicoW bude sloužit jako bezdrátová sonda měření teploty. Data naměřené teploty budeme sbírat na serveru.

Předpoklady

Máme k dispozici Wifi Access Point, který umí přidělovat IPv6 adresy bezestavovou konfigurací (IPv6 Stateless Address Auto-configuration (SLAAC)).

Program

main.c
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "lwip/pbuf.h"
#include "lwip/udp.h"
#include "lwip/ip6_addr.h"
#include "hardware/adc.h"
#include "setupWifi.h"

#define KAM_POSLAT "2a12:b7c0:3:1ff::1aa1"

int connect()
{
    char ssid[]      = "chorche";
    char pass[]      = "xxxxxxxxxxxx";
    uint32_t country = CYW43_COUNTRY_CZECH_REPUBLIC;
    uint32_t auth    = CYW43_AUTH_WPA2_MIXED_PSK;
    return  setup(country, ssid, pass, auth, "PicoW6-teplota", NULL, NULL);
}

int main()
{
int connection_status = 0;
const float conversion_factor = 3.319f / ( 1 << 12 );
uint16_t result;
float teplota;
float adc;


    stdio_init_all();
// nastavení a výběr interního čidla
    adc_init();
    adc_set_temp_sensor_enabled(true);
    adc_select_input(4);

// nastavení měniče na malý šum
    gpio_set_function(23, GPIO_FUNC_SIO);
    gpio_set_dir( 23, true );
    gpio_put( 23, 1 );

    sleep_ms(10000); // pro připojení minicomu
    printf("\nMěření teploty a posílání na UDP server v 0.1-6.\n");
    do {
        printf("Připojuji se k AP.\n");
        connection_status = connect();
    } while ( connection_status != CYW43_LINK_UP);
    
    printf("Připojeno.\n");
    printf("Linková IPv6 adresa:    %s\n", ip6addr_ntoa(netif_ip6_addr(netif_default,0)));
    printf("Globální IPv6 adresa:   %s\n", ip6addr_ntoa(netif_ip6_addr(netif_default,1)));
    printf("Host Name: %s\n", netif_get_hostname(netif_default));
	struct udp_pcb *pcb = udp_new_ip_type(6);

    char moje_ip_buf[64];
    sprintf(moje_ip_buf,"%s", ip6addr_ntoa(netif_ip6_addr(netif_default,1)));
    ip6_addr_t moje_ip;
    ip6addr_aton(moje_ip_buf, &moje_ip);
	IP_SET_TYPE_VAL(&moje_ip, IPADDR_TYPE_V6);
    udp_bind(pcb, &moje_ip, 4848);
	ip6_addr_t ip_kam;
	ip6addr_aton(KAM_POSLAT, &ip_kam);
	IP_SET_TYPE_VAL(&ip_kam, IPADDR_TYPE_V6);
    printf("IPv6 adresa na posílání: %s\n", ip6addr_ntoa(&ip_kam));
    udp_connect(pcb, &ip_kam, 4848);
    static char message[256];
        
    while( true ){
        result = adc_read();
        adc = result * conversion_factor;
        // teplota bez korekce
        teplota = 27.0f - (adc - 0.706f) / 0.001721f;
        memset(message,'\0',256);     
        sprintf(message, "Teplota: %5.1f ˚C\n", teplota);
        struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, 256, PBUF_RAM);
        snprintf(p->payload, strlen(message)+1, "%s", message);
        err_t er = udp_send(pcb,p);
        pbuf_free(p);
        printf("Poslal jsem: 'Teplota: %5.1f ˚C', %s\r", teplota, er==0?"OK":"chyba");
        sleep_ms(1000);
	}
    return 0;
}
setupWifi.h
/* Nastavení Wifi karty */

int setup(uint32_t country, const char *ssid, const char *pass,
          uint32_t auth, const char *hostname, ip_addr_t *iplocal,
          ip_addr_t *ipglobal)
{

    if (cyw43_arch_init_with_country(country))
    {
        return 1;
    }

    cyw43_arch_enable_sta_mode();
    if (hostname != NULL)
    {
        netif_set_hostname(netif_default, hostname);
    }
    if (cyw43_arch_wifi_connect_async(ssid, pass, auth))
    {
        return 2;
    }
    int flashrate = 1000;
    int status = CYW43_LINK_UP + 1;
    while (status >= 0 && status != CYW43_LINK_UP)
    {
        int new_status = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
        if (new_status != status)
        {
            status = new_status;
            flashrate = flashrate / (status + 1);
            printf("Stav připojování: %d %d\n", status, flashrate);
        }
        cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
        sleep_ms(flashrate);
        cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
        sleep_ms(flashrate);
    }
    if (status < 0)
    {
        cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
    }
    else
    {
        cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
        if (iplocal != NULL)
        {
            //iplocal = (ip_addr_t) netif_ip6_addr(netif_default,0);
        }
        if (ipglobal != NULL)
        {
            //ipglobal = (ip_addr_t) (netif_default,1);
        }
        
	}
    return status;
}
lwipopts.h
#ifndef _LWIPOPTS_EXAMPLE_COMMONH_H
#define _LWIPOPTS_EXAMPLE_COMMONH_H

// Common settings used in most of the pico_w examples
// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html for details)

// allow override in some examples
#ifndef NO_SYS
#define NO_SYS 1
#endif
// allow override in some examples
#ifndef LWIP_SOCKET
#define LWIP_SOCKET 0
#endif
#if PICO_CYW43_ARCH_POLL
#define MEM_LIBC_MALLOC 1
#else
// MEM_LIBC_MALLOC is incompatible with non polling versions
#define MEM_LIBC_MALLOC 0
#endif
#define MEM_ALIGNMENT 4
#define MEM_SIZE 4000
#define MEMP_NUM_TCP_SEG 32
#define MEMP_NUM_ARP_QUEUE 10
#define PBUF_POOL_SIZE 24
#define LWIP_ARP 1
#define LWIP_ETHERNET 1
#define LWIP_ICMP 1
#define LWIP_RAW 1
#define TCP_WND (8 * TCP_MSS)
#define TCP_MSS 1460
#define TCP_SND_BUF (8 * TCP_MSS)
#define TCP_SND_QUEUELEN ((4 * (TCP_SND_BUF) + (TCP_MSS - 1)) / (TCP_MSS))
#define LWIP_NETIF_STATUS_CALLBACK 1
#define LWIP_NETIF_LINK_CALLBACK 1
#define LWIP_NETIF_HOSTNAME 1
#define LWIP_NETCONN 0
#define MEM_STATS 0
#define SYS_STATS 0
#define MEMP_STATS 0
#define LINK_STATS 0
// #define ETH_PAD_SIZE 2
#define LWIP_CHKSUM_ALGORITHM 3
#define LWIP_DHCP 0
#define LWIP_IPV4 0
#define LWIP_IPV6 1
#define LWIP_IPV6_AUTOCONFIG 1
#define LWIP_TCP 0
#define LWIP_UDP 1
#define LWIP_DNS 0
#define LWIP_TCP_KEEPALIVE 0
#define LWIP_NETIF_TX_SINGLE_PBUF 1
#define DHCP_DOES_ARP_CHECK 0
#define LWIP_DHCP_DOES_ACD_CHECK 0
#define LWIP_CHECKSUM_ON_COPY 1
#define LWIP_CHECKSUM_GEN_UDP 1
#define LWIP_IPV6_SCOPES 0

#ifndef NDEBUG
#define LWIP_DEBUG 1
#define LWIP_STATS 1
#define LWIP_STATS_DISPLAY 1
#endif

#define ETHARP_DEBUG        LWIP_DBG_OFF
#define NETIF_DEBUG         LWIP_DBG_OFF
#define PBUF_DEBUG          LWIP_DBG_OFF
#define API_LIB_DEBUG       LWIP_DBG_OFF
#define API_MSG_DEBUG       LWIP_DBG_OFF
#define SOCKETS_DEBUG       LWIP_DBG_OFF
#define ICMP_DEBUG          LWIP_DBG_OFF
#define INET_DEBUG          LWIP_DBG_OFF
#define IP_DEBUG            LWIP_DBG_OFF
#define IP_REASS_DEBUG      LWIP_DBG_OFF
#define RAW_DEBUG           LWIP_DBG_OFF
#define MEM_DEBUG           LWIP_DBG_OFF
#define MEMP_DEBUG          LWIP_DBG_OFF
#define SYS_DEBUG           LWIP_DBG_OFF
#define TCP_DEBUG           LWIP_DBG_OFF
#define TCP_INPUT_DEBUG     LWIP_DBG_OFF
#define TCP_OUTPUT_DEBUG    LWIP_DBG_OFF
#define TCP_RTO_DEBUG       LWIP_DBG_OFF
#define TCP_CWND_DEBUG      LWIP_DBG_OFF
#define TCP_WND_DEBUG       LWIP_DBG_OFF
#define TCP_FR_DEBUG        LWIP_DBG_OFF
#define TCP_QLEN_DEBUG      LWIP_DBG_OFF
#define TCP_RST_DEBUG       LWIP_DBG_OFF
#define UDP_DEBUG           LWIP_DBG_OFF
#define TCPIP_DEBUG         LWIP_DBG_OFF
#define PPP_DEBUG           LWIP_DBG_OFF
#define SLIP_DEBUG          LWIP_DBG_OFF
#define DHCP_DEBUG          LWIP_DBG_OFF

#endif /* __LWIPOPTS_H__ */
CMakeLists.txt
cmake_minimum_required(VERSION 3.13)

set(PICO_BOARD pico_w)

set (CMAKE_C_STANDARD 11)
set (CMAKE_CXX_STANDARD 17)

include(pico_sdk_import.cmake)
project(PicoW_UDP C CXX ASM)
pico_sdk_init()

add_executable(main
    main.c
)

target_include_directories(main PRIVATE ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries( main
    pico_stdlib
    pico_cyw43_arch_lwip_threadsafe_background
    hardware_adc
)

pico_enable_stdio_usb(main 1)
pico_enable_stdio_uart(main 0)
pico_add_extra_outputs(main)

Jednoduchý server v Pythonu na zobrazení došlých dat (běží na serveru).

udp_server.py
import asyncio
class ClientDatagramProtocol(asyncio.DatagramProtocol):
    def datagram_received(self, data, addr):
        message = data.decode("utf8")
        print("Received",message,"from", addr)

async def main():
    loop = asyncio.get_running_loop()
    transport, protocol = await loop.create_datagram_endpoint(
          lambda: ClientDatagramProtocol(),
                    local_addr=('::', 4848))
    await asyncio.sleep(100000)
    transport.close()

asyncio.run(main())

Zdroje a odkazy