【NodeMCU-ESP8266】Arduino环境下建立UDP服务器及WiFiUDP库常用函数详解

目录

一、UDP协议概述

        1、UDP基本概念

        2、UDP的主要特点

        3、UDP的基本报文格式

​二、ESP8266官方UDP库相关函数详解

三、建立UDP服务器的一般流程

四、ESP8266官方库 UDP 示例程序演示

五、ESP8266官方库 UDP 程序运行说明

        1、将程序编译并上传到开发板。

        2、运行串口网络数据调试器 对UDP服务器进行测试。


一、UDP协议概述

        1、UDP基本概念

        UDP(User Datagram Protocol)——  用户数据报协议,是互联网传输层的一个重要协议。
        UDP为应用程序提供了一种无需建立连接就可以发送封装的 IP 数据包的方法,允许应用程序在所需的层次上架构自己的协议之外,几乎没有做什么特别的事情。
        UDP使用底层的互联网协议来传送报文,同IP一样提供不可靠的无连接数据包传输服务。它不提供报文到达确认、排序、及流量控制等功能。
        由于使用UDP协议消耗资源小,通信效率高,所以通常都会用于音频、视频和普通数据的传输例如视频会议都使用UDP协议,因为这种情况即使偶尔丢失一两个数据包,也不会对接收结果产生太大影响。

        2、UDP的主要特点

        (1)UDP是无连接的协议。即在发送数据前不需要建立连接,发送数据结束也没有连接释放,因此减少了开销和发送数据前的时延。
        (2)UDP使用尽最大努力交付。即不保证可靠交付,因此不需要维护复杂的连接状态表。
        (3)UDP是面向数据报的。对于应用层交付的报文,发送方的UDP在添加首部后直接交付给IP层。UDP对应用层交付的报文,既不合并,也不拆分,而是保留这些报文的边界。也就是说,应用层给UDP多长报文,UDP就发多长,一次发送一个报文。
        (4)在接收方的UDP,对于IP层交付的UDP用户数据报,在去除UDP首部后也直接交付应用层,不作任何处理,一次交付一个完整的报文。因此,控制报文长度的工作由应用层负责,应用层要选择合适大小的报文。若报文太长,UDP封装后交给IP层,IP层在传输时可能要进行分片处理,减低了IP层的效率。反之,应用层报文太短,效率也低。
        (5)UDP没有拥塞控制机制。因此,网络出现的拥塞不会使源主机的发送速率降低。这对某些实时应用很重要,如IP电话,实时视频会议等,要求源主机以恒定的速率发送数据,并且允许在网络拥塞时丢失一部分数据,但不允许数据有太大的时延。
        (6)UDP支持一对一,一对多,多对一和多对多的交互通信。
        (7)UDP的首部开销小,只有8字节,比TCP的20字节的首部短。

        3、UDP的基本报文格式

        UDP报头由4部分组成:
        源端口号:发送方的端口号。
        目的端口号:目的主机的端口号。
        UDP长度:UDP报文的长度,单位字节,包括报头和有效载荷。
        UDP校验和:保证报文完整性和正确性,出错就丢弃。

二、ESP8266官方UDP库相关函数详解

        1. begin(uint16_t port)
        功能:初始化 WiFiUDP库和网络设置,启动WiFiUDP套接字,侦听指定的本地端口
        参数:port,指定侦听的本地端口号
        返回值:1,成功;0,失败,表示没有可用的套接字

// initialize, start listening on specified port. 
// Returns 1 if successful, 0 if there are no sockets available to use
uint8_t begin(uint16_t port) override;

        2.  available()
        功能:返回当前数据包中,可从缓冲区读取的字节数。这是已经到达的数据。
        参数:无
        返回值:当前数据包中可用的字节数
        注意:仅在调用解析数据包函数WiFiUDP.parsePacket()之后,才能成功调用此函数。

// Number of bytes remaining in the current packet
int available() override;

        3. beginPacket(hostName, port)  或  beginPacket(hostIp, port)
        功能:开始构建数据包,准备写入数据,以发送到指定ip和端口的远程主机。
        参数:远程主机的IP地址(或域名)和端口号
        返回值:如果成功,则返回1;如果提供的IP地址或端口有问题,则返回0。       

// Start building up a packet to send to the remote host specific in ip and port
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
int beginPacket(IPAddress ip, uint16_t port) override;
// Start building up a packet to send to the remote host specific in host and port
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
int beginPacket(const char *host, uint16_t port) override;

        4. endPacket()
        功能:写入UDP数据后,完成数据找包并发送。
        参数:无
        返回值:报文发送成功,返回1;如有错误,返回0

// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
int endPacket() override;

        5. write(byte)  或  write(buffer, size)
        功能:前者,将单个字节内容,写入UDP数据包;后者,将缓冲区中指定数目的字节数,写入UDP数据包。
        参数:byte,发送的字节;buffer:数据缓冲区指针;size,缓冲区大小。
        返回值:写入UDP数据包的字节数。
        注意:此函数必须包装在beginPacket()和endPacket()之间。其中,beginPacket()初始化数据包,endPacket()完成数据包的发送。

// Write a single byte into the packet
size_t write(uint8_t) override;
// Write size bytes from buffer into the packet
size_t write(const uint8_t *buffer, size_t size) override;

        6. parsePacket ()
        功能:开始处理下一个可用的传入数据包
        参数:无
        返回值:返回数据包的大小(以字节为单位),如无可用的数据包,则返回0
        注意:在使用UDP.read()读取缓冲区之前,必须先行调用parsePacket ()。

// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
int parsePacket() override;

        7. peek()
        功能:从当前数据包中读取并返回下一个字节,但并不向前移动到下一个字节。也就是说,对peek()的连续调用将返回相同的值,与对下一个read()的调用相同。
        返回值:下一个字节或字符;如果没有可供读取的字节则返回-1

// Return the next byte from the current packet without moving on to the next byte
int peek() override;

        8. read()  或  read(buffer, len)
        功能:前者,从当前数据包中读取一个字节;后者,从当前数据包中读取 len 个字节的数据,并将其存入buffer指定的缓冲区。
        参数:buffer,将要保存数据的缓冲区指针;len,要求要读取的字节数。
        返回值:前者,读取到的字节数据;后者,读取到的字节数。如无可供读取的字节则返回0
        注意:在函数 parsePacket() 执行成功之后,才能调用此函数

// Read a single byte from the current packet
int read() override;
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
int read(unsigned char* buffer, size_t len) override;

        9. flush()
        功能:等待发送所有传出字符,此调用后输出缓冲区将被清空

// wait for all outgoing characters to be sent, output buffer is empty after this call
void flush() override;	

        10. stop()
        功能:断开 UDP 连接,释放 UDP 会话期间所使用的资源。

  // Finish with the UDP connection
  void stop() override;

        11. remotelP()
        功能:获取发送当前数据包的远程主机的IP地址
        返回值:发送当前数据包的远程主机IP地址(4个字节的IPAddress结构)
        注意:必须在WiFiUDP.parsePacket()之后,才能调用此函数

// Return the IP address of the host who sent the current incoming packet
IPAddress remoteIP() override;

        12. remotePort()
        功能:获取远程连接主机的端口号
        返回值:发送当前数据包的远程主机的端口号
        注意:必须在WiFiUDP.parsePacket()之后,才能调用此函数

// Return the port of the host who sent the current incoming packet
uint16_t remotePort() override;

三、建立UDP服务器的一般流程

        1、将ESP8266模组或开发板设置在station工作模式:WiFi.mode(WIFI_STA)。
        2、将ESP8266模组或开发板接入到WIFI 网络:WiFi.begin(SSID,PSSD)。
        3、实例化 WiFiUDP对象,开始在指定侦听端口号建立UDP连接,启动自动侦听。
        4、等待输入的UDP包,处理收到的数据,并回复客户端请求的信息。

四、ESP8266官方库 UDP 示例程序演示

        启动 ArduinoIDE,从菜单栏依次点选【文件】——【示例】——【ESP8266WiFi】——【UDP】,在IDE窗口主界面打开官方 UDP 示例程序。 
       将程序编译\上传到NodeMCU-ESP8266开发板。       

/*
  UDPSendReceive.pde:
  This sketch receives UDP message strings, prints them to the serial port
  and sends an "acknowledge" string back to the sender

  A Processing sketch is included at the end of file that can be used to send
  and received messages for testing with a computer.

  created 21 Aug 2010
  by Michael Margolis

  This code is in the public domain.

  adapted from Ethernet library examples
*/


#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

#ifndef STASSID
#define STASSID "xcb940"        //将SSID更改成可用的名称
#define STAPSK "87589940abc"    //更改成可用的密码
#endif

unsigned int localPort = 8888;  // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE + 1];  // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged\r\n";        // a string to send back

WiFiUDP Udp;

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(STASSID, STAPSK);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(500);
  }
  Serial.print("Connected! IP address: ");
  Serial.println(WiFi.localIP());
  Serial.printf("UDP server on port %d\n", localPort);
  Udp.begin(localPort);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.printf("Received packet of size %d from %s:%d\n    (to %s:%d, free heap = %d B)\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort(), Udp.destinationIP().toString().c_str(), Udp.localPort(), ESP.getFreeHeap());

    // read the packet into packetBufffer
    int n = Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    packetBuffer[n] = 0;
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
}

五、ESP8266官方库 UDP 程序运行说明

        1、将程序编译并上传到开发板。

        如在串口监视器可以看到如下信息,说明程序已成功运行。
        信息提示:(1)开发板成功连接到本地WiFi,获取到IP地址为:192.168.0.103。
                          (2)建立UDP服务器成功,并在8888端口启动侦听。

        2、运行串口网络数据调试器 对UDP服务器进行测试。

        (1)调试工具:SSCOM V5.13.1 百度网盘点击下载,  提取码:b89u。


        (2)运行调试工具,按照开发板串口输出信息中的UDP服务器IP地址和端口号,设置调试工具窗口左下角的远程主机IP地址和端口号,本地IP地址和端口号由软件自动填充。
        (3)点击调试工具窗口左下角【连接】按钮,在右下角空白窗口输入待发送的信息,再点击【发送】按钮,就可以向UDP服务器发送信息了。


        (4)如果信息发送成功,开发板串口监视器和调试工具上方的信息窗口,就可以看到有关信息了。

  • 33
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是将ESP8266 NodeMCU开发板连接到Arduino Uno板并传输数据的示例代码: 在NodeMCU上运行的代码: ``` #include <ESP8266WiFi.h> const char* ssid = "your_SSID"; // 你的WiFi名称 const char* password = "your_PASSWORD"; // 你的WiFi密码 WiFiServer server(80); // 设置一个本地服务器 void setup() { Serial.begin(9600); // 与串口通信 delay(10); // 连接到WiFi网络 Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // 开启服务器 server.begin(); Serial.println("Server started"); } void loop() { // 等待客户端连接 WiFiClient client = server.available(); if (!client) { return; } // 读取客户端发送的数据 Serial.println("new client"); while(!client.available()){ delay(1); } String request = client.readStringUntil('\r'); Serial.println(request); client.flush(); // 发送数据到Arduino Uno Serial.println("Sending data to Arduino..."); Serial1.println(request); delay(100); // 发送响应到客户端 client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(""); // 空行必须添加 client.println("<!DOCTYPE HTML>"); client.println("<html>"); client.println("<body>"); client.println("<h1>ESP8266 NodeMCU</h1>"); client.println("<p>Data sent to Arduino Uno: " + request + "</p>"); client.println("</body>"); client.println("</html>"); delay(1); Serial.println("Client disconnected"); Serial.println(""); } ``` 在Arduino Uno上运行的代码: ``` void setup() { Serial.begin(9600); // 与串口通信 Serial1.begin(9600); // 与NodeMCU通信 } void loop() { // 读取NodeMCU发送的数据 if (Serial1.available()) { String data = Serial1.readStringUntil('\r'); Serial.println("Data received: " + data); } } ``` 这个例子中,NodeMCU运行一个本地服务器,等待客户端连接并发送数据。当有客户端连接时,NodeMCU会读取客户端发送的数据并将其发送到Arduino Uno。然后,NodeMCU会向客户端发送一个HTML响应,其中包含已发送到Arduino Uno的数据。Arduino Uno只是读取NodeMCU发送的数据并将其打印到串口监视器中。 注意:确保将NodeMCUArduino Uno正确连接。将NodeMCU的TX引脚连接到Arduino Uno的RX引脚,将NodeMCU的RX引脚连接到Arduino Uno的TX引脚。如果您使用的是NodeMCU V1.0板,您需要将板上的开关设置为“UART”模式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值