三十二、W5100S/W5500+RP2040树莓派Pico<UPnP示例>

本文介绍了W5100S/W5500以太网芯片如何应用于UPnP技术,包括UPnP的基本原理、优点、数据交互流程,以及在智能家居、物联网中的应用场景。教程详细展示了如何使用这些芯片进行UPnP应用的示例和注意事项。

1 前言

  随着智能家居、物联网等技术的快速发展,UPnP技术的应用前景将更加广阔。未来,随着技术的不断进步和应用场景的不断扩展,UPnP技术有望成为连接各种设备的核心协议之一,从而为用户带来更加智能、更加便捷的网络体验。

  W5100S/W5500是一款集成全硬件 TCP/IP 协议栈的嵌入式以太网控制器,同时也是一颗工业级以太网控制芯片。本教程将介绍W5100S/W5500以太网UPnP应用的基本原理、使用步骤、应用实例以及注意事项,帮助读者更好地掌握这一技术。

2 简介

2 .1 什么是UPnP?

  UPnP(Universal Plug and Play)是一种网络协议,它由“通用即插即用论坛”(UPnP™ Forum)推广,旨在使各种设备(如家庭网络、公司网络中的设备)能够相互无缝连接,并简化相关网络的实现。

UPnP的目标是让各种各样的智能设备、无线设备和个人电脑等实现遍布全球的对等网络连接。它支持TCP/IP、UDP和HTTP等协议,是一种分布式的、开放的网络架构。

2.2 UPnP的优点

  UPnP的优点主要包括:

  1. 自动配置:UPnP能够自动配置网络设备,使其能够在网络中进行通信,无需手动设置端口映射等繁琐操作。这大大简化了设备的安装和使用过程,降低了网络配置的复杂性。
  2. 简化用户体验:UPnP可以让用户更方便地使用网络设备和应用程序,提供更好的用户体验。由于UPnP无需用户进行任何配置,用户只需简单地将设备连接到网络中,设备即可自动获取IP地址并与其他设备进行通信。
  3. 多设备互联:UPnP支持多个设备之间的互联,使得设备之间的通信和共享资源变得更加简单和便捷。这使得用户可以方便地将多个设备连接在一起,实现更丰富的应用场景。
  4. 远程访问:UPnP支持远程访问,使用户能够从任何地方访问他们的设备和资源。无论用户身处何地,只要能够连接到网络,即可对家中的设备进行控制和操作。

2.3 UPnP数据交互原理

在这里插入图片描述

UPnP的交互过程包括以下环节:

  1. 地址分配:设备接入网络后,通过自动或手动方式获取IP地址。
  2. 发现阶段:设备在网络中广播自己的存在,并寻找其他设备和服务。
  3. 描述阶段:设备向控制点提供详细的服务和功能描述信息。
  4. 控制阶段:设备通过发送请求消息来控制其他设备或获取其状态信息。
  5. 事件阶段:设备向控制点发送事件消息,以报告自己的状态变化或其他重要事件。
  6. 表达阶段:设备向控制点提供详细的设备和服务信息,以便其他设备了解和利用这些信息。

2.4 UPnP应用场景

UPnP的应用场景包括但不限于以下几种:

  1. 智能家居:UPnP可以用于智能家居设备的互联互通,实现自动化控制和远程控制。例如,通过UPnP协议,用户可以在家中使用手机或电脑控制家中的灯光、电视、空调等设备,也可以设置设备的定时任务。
  2. 物联网:在物联网领域,UPnP可以用于实现设备的互联互通和智能化管理。例如,在智能农业中,通过UPnP协议可以将农田监测设备、温室大棚监测设备等连接起来,实现农业信息的实时监测和远程控制。
  3. 多媒体家庭网关:UPnP可以用于将各种传输媒介连接到家庭局域网中,实现互联和控制。例如,用户可以在家中观看高清电影或聆听无损音乐,同时享受快速网络连接带来的便利。
  4. 智能家居设备:UPnP可以用于智能家居设备的互联和通讯,例如智能音箱、智能灯泡等。用户可以通过语音控制或手机APP控制家中的设备,享受更加便捷的生活体验。
  5. 安全设备串接:UPnP可以实现数字签名和认证功能,保障设备数据的安全性。例如,可由支持UPnP的IP摄像头自动配置局域网环境,也可以在UPnP上实现数字签名和认证功能,保障设备数据的安全性。

3 WIZnet以太网芯片

WIZnet 主流硬件协议栈以太网芯片参数对比

ModelEmbedded CoreHost I/FTX/RX BufferHW SocketNetwork Performance
W5100STCP/IPv4, MAC & PHY8bit BUS, SPI16KB4Max.25Mbps
W6100TCP/IPv4/IPv6, MAC & PHY8bit BUS, Fast SPI32KB8Max.25Mbps
W5500TCP/IPv4, MAC & PHYFast SPI32KB8Max 15Mbps
  1. W5100S/W6100 支持 8bit数据总线接口,网络传输速度会优于W5500。
  2. W6100 支持IPv6,与W5100S 硬件兼容,若已使用W5100S的用户需要支持IPv6,可以Pin to Pin兼容。
  3. W5500 拥有比 W5100S更多的 Socket数量以及发送与接收缓存。

4 UPnP示例概述以及使用

4.1 流程图

  程序的运行框图如下所示:

在这里插入图片描述

4.2 准备工作核心

软件

  • Visual Studio Code
  • WIZnet UartTool
  • Socket Tester

硬件

  • W5100SIO模块 + RP2040 树莓派Pico开发板 或者 WIZnet W5100S-EVB-Pico开发板
  • Micro USB 接口的数据线
  • TTL 转 USB
  • 网线
  • 支持UPNP的路由器

4.3 连接方式

  • 通过数据线连接PC的USB口(主要用于烧录程序,也可以虚拟出串口使用)

  • 通过TTL串口转USB,连接UART0 的默认引脚:

    • RP2040 GPIO 0(UART0 TX) <----> USB_TTL_RX
    • RP2040 GPIO 1(UART0 RX) <----> USB_TTL_TX
  • 使用模块连接RP2040 进行接线时

    • RP2040 GPIO 16 <----> W5100S MISO
    • RP2040 GPIO 17 <----> W5100S CS
    • RP2040 GPIO 18 <----> W5100S SCK
    • RP2040 GPIO 19 <----> W5100S MOSI
    • RP2040 GPIO 20 <----> W5100S RST
  • 通过PC和设备都通过网线连接路由器LAN口

4.4 主要代码概述

  我们使用的是WIZnet官方的ioLibrary_Driver库。该库支持的协议丰富,操作简单,芯片在硬件上集成了TCP/IP协议栈,该库又封装好了TCP/IP层之上的协议,我们只需简单调用相应函数即可完成协议的应用。

第一步:upnp_run.c文件中加入对应的头文件。

第二步:定义相关宏,包括socket端口号、收发缓存发小、DHCP失败后重试次数、LED灯引脚号。

第三步:声明了相关函数,包括定时器回调函数,用于 DHCP 和UPNP 1s 计时处理;网络信息初始化,通过DHCP获取网络信息,失败则静态配置网络信息;LED初始化和LED灯状态控制;还初始化了相关变量等

第四步:进入主函数首先定义变量并初始化串口和spi接口,然后进行socket收发缓存的分片并写入配置信息,接着初始化LED和DHCP,并开启 1s 定时器,优先通过DHCP配置网络信息,失败则用静态网络信息;配置完成后通过 SSDP 发现设备(IGD),接着获取设备的描述并订阅事件消息,成功后进入菜单界面选择操作事件;如下所示:

#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/spi.h"

#include "wizchip_conf.h"
#include "w5100s.h"
#include "bsp_spi.h"
#include "dhcp.h"   // Use dhcp
#include "socket.h" // Use socket
#include "UPnP.h"   // Use upnp

#define SOCKET_ID 0                      // Socket number
#define ETHERNET_BUF_MAX_SIZE (1024 * 4) // Send and receive cache size
#define DHCP_RETRY_COUNT 5               // DHCP retry times
#define USER_LED_PIN 25                  // Onboard led pin

/**
 * @brief   Timer callback processing function, used for dhcp timing processing
 * @param   repeating :Timer structure
 * @return  bool
 */
bool repeating_timer_callback(struct repeating_timer *t);

/**
 * @brief   Initialization of chip network information
 * @param   conf_info :Static configuration information
 * @return  none
 */
void network_init(wiz_NetInfo *conf_info);

/**--
 * @brief   Initiallization led and Registration function
 * @param   none
 * @return  none
 */
void UserLED_Init(void);

/**
 * @brief   set led status, in order to adapt data format, see details the file: snmp_custom.c 's snmpData[]
 * @param   val: 0 -> led off, 1 -> led on
 * @return  none
 */
void setUserLEDStatus(uint8_t val);

/* Network information to be configured. */
wiz_NetInfo net_info = {
    .mac = {0x00, 0x08, 0xdc, 0x1e, 0xed, 0x2e}, // Configured MAC address
    .ip = {192, 168, 1, 10},                     // Configured IP address
    .sn = {255, 255, 255, 0},                    // Configured subnet mask
    .gw = {192, 168, 1, 1},                      // Configured gateway
    .dns = {8, 8, 8, 8},                         // Configured domain address
    .dhcp = NETINFO_DHCP};                       // Configured dhcp model,NETINFO_DHCP:use dhcp; NETINFO_STATIC: use static ip.

static uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {
    0,
}; // Send and receive cache

static uint8_t breakout_flag = 0; // Define the DHCP acquisition flag
static uint16_t tcps_port = 8000;
static uint16_t udps_port = 5000;

#if (_WIZCHIP_ == W5100S)
static uint8_t tx_size[_WIZCHIP_SOCK_NUM_] = {4, 2, 2, 0};
static uint8_t rx_size[_WIZCHIP_SOCK_NUM_] = {4, 2, 2, 0};
#elif (_WIZCHIP_ == W5500)
static uint8_t tx_size[_WIZCHIP_SOCK_NUM_] = {4, 4, 2, 1, 1, 1, 1, 2};
static uint8_t rx_size[_WIZCHIP_SOCK_NUM_] = {4, 4, 2, 1, 1, 1, 1, 2};
#endif

int main()
{
    struct repeating_timer timer; // Define the timer structure
    wiz_NetInfo get_info;         // Stores the read configuration information

    /* MCU init */
    stdio_init_all();     // Initialize the main control peripheral
    wizchip_initialize(); // Initialize the chip interface

    /* socket rx and tx buff init */
    wizchip_init(tx_size, rx_size);
    wizchip_setnetinfo(&net_info); // Configure once first

    /* Onboard LED init*/
    UserLED_Init();

    /*dhcp init*/
    DHCP_init(SOCKET_ID, ethernet_buf);                                   // DHCP initialization
    add_repeating_timer_ms(1000, repeating_timer_callback, NULL, &timer); // Add DHCP 1s Tick Timer handler

    printf("wiznet chip upnp example.\r\n");
    network_init(&net_info);              // Configuring Network Information
    print_network_information(&get_info); // Read back the configuration information and print it

    do
    {
        printf("Send SSDP.. \r\n");
    } while (SSDPProcess(SOCKET_ID) != 0); // SSDP Search discovery

    if (GetDescriptionProcess(SOCKET_ID) == 0) // GET IGD description
    {
        printf("GetDescription Success!!\r\n");
    }
    else
    {
        printf("GetDescription Fail!!\r\n");
    }

    if (SetEventing(SOCKET_ID) == 0) // Subscribes IGD event messageS
    {
        printf("SetEventing Success!!\r\n");
    }
    else
    {
        printf("SetEventing Fail!!\r\n");
    }

    Main_Menu(SOCKET_ID, SOCKET_ID + 1, SOCKET_ID + 2, ethernet_buf, tcps_port, udps_port); // Main menu
}

4.5 结果演示

  1. DHCP获取ip成功:

    在这里插入图片描述

  2. 添加端口映射成功:

    在这里插入图片描述

  3. 运行TCP Server回环测试成功:

    在这里插入图片描述

5 注意事项

  • 注意socket缓存分片要足够大,避免过小导致接收数据不全,数据包解析时致错;
  • 定义用于收发的暂存(缓存)数组大小要大于或等于所对应的socket缓存大小;
  • 路由器要具备upnp功能,并且调试时要开启;
  • 如果想用WIZnet的W5500来实现本章的示例,我们只需修改两个地方即可:

​ (1)在library/ioLibrary_Driver/Ethernet/下找到wizchip_conf.h这个头文件,将_WIZCHIP_ 宏定义修改为W5500。

​ (2)在library下找到CMakeLists.txt文件,将COMPILE_SEL设置为ON即可,OFF为W5100S,ON为W5500。

6 相关链接

WIZnet官网

WIZnet官方库链接

本章例程链接

想了解更多,评论留言哦!

If you are experiencing problems with the Universal Plug and Play service, your computer might not be able to automatically detect the presence of other networked devices, such as PCs, printers, Internet access points and so on. That is where the UPnP Test application comes in. This simple program is designed to help you identify the issues that prevent the UPnP protocol from functioning correctly. Before you get your hopes up, you should know that this tool does not solve the detected problems, but only performs a series of tests to identify the possible causes. One advantage is that the application does not require installation, so your system registry is not affected in any way. The interface is compact and simple, comprising only two panels: one that displays the test type and its short description and the other for viewing which of the tests passed and which failed. The program can verify whether the operating system provides support for the UPnP service and allows you to check if the Simple Service Discovery Protocol (SSDP) and the UPnPHost services are running. It also verifies the connection between your network adapter and your router and the system's capacity to receive UPnP messages, as well as the router's capability to report an external IP address. One of the tests is designed to check if the Windows firewall service is blocking the traffic between your router and the system, thus preventing UPnP from working. The results can be copied to your clipboard by simply pressing a button and the tests can be redone easily. If you want to fix the detected issues, the link in the main interface can prove useful. In conclusion, UPnP Test is a simple tool for detecting problems related to device-to-device networking. However, it can only suggest possible reasons why the UPnP is not working, fixing the detected issues is totally up to you.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值