在上一个教程中UDP协议的优点over TCP/IP in IoT applications were discussed. The UDP protocol has a small overhead of 8 bytes which makes it more suitable for use in the Internet of Things. In this project, the application of UDP protocol in IoT will be demonstrated. In this project, an ESP8266 Wi-Fi modem will be configured as UDP server and a laptop will be used as UDP Client. Both Client and server will be co-located communicating through same Wi-Fi router so, the ESP board will act as a local server. The UDP Client could send data to the server on a particular port with the help of an application –数据包发送者.
The ESP module working as server checks for the UDP packet received from the client on a particular port. When a valid packet is arrived, an acknowledge packet is sent back to the client to the same port it has been sent out. In response to receiving packet and sending acknowledgement, the ESP modem switches on an LED as visual indication of successful Client-Server Communication. This application is very useful as it demonstrates server/client communication over UDP protocol.
It can be said that the IOT device designed in this project is a simple UDP server with LED indicator. It is designed by interfacing an LED with the ESP-8266 Wi-Fi module. The Wi-Fi module as well as LED light are powered continuously with the help of a USB to Serial Converter. The Wi-Fi module needs to loaded with a firmware that could receive data over UDP protocol and respond with an acknowledgement. The Arduino UNO is used to flash the firmware code on the ESP8266 module. The ESP module can also be flashed with code using a FTDI converter like CP2102. The firmware itself is written in the Arduino IDE.
需要组件 -

图1:UDP协议基于客户端服务器通信所需的组件列表
Software Required –
•Arduino IDE
框图 -

Fig. 2: Block Diagram of ESP8266 UDP Server and Packet Sender UDP Client Communication
电路连接 -
首先,ESP8266板需要加载固件代码。在本教程中,使用Arduino IDE编写固件代码。它使用Arduino Uno加载到ESP8266板上。该项目使用了通用的ESP8266板。该板没有任何引导电阻,没有电压调节器,没有重置电路,也没有USB串行适配器。ESP8266模块在3.3 V电源上运行,电流大于或等于250 mA。因此,使用CP2102 USB到串行适配器可在每种情况下可靠地可靠地运行ESP8266的3.3 V电压。
The ESP8266 Wi-Fi Module is a self contained SOC with integrated TCP/IP protocol stack that can access to a Wi-Fi network. The ESP8266 is capable of either hosting an application or off loading all Wi-Fi networking functions from another application processor. Each ESP8266 module comes pre-programmed with an AT command set firmware. The module comes available in two models – ESP-01 and ESP-12. ESP-12 has 16 pins available for interfacing while ESP-01 has only 8 pins available for use. The ESP-12 has the following pin configuration –

Fig. 3: Table listing pin configuration of ESP8266 ESP-12 Module

Fig. 4: Table listing pin configuration of ESP8266 ESP-12 Module
The ESP-01 model is used in the project. The ESP-01 model has the following pin configuration –

Fig. 5: Table listing pin configuration of ESP8266 ESP-01 Module
The Chip Enable and VCC pins of the module are connected to the 3.3 V DC while Ground pin is connected to the common ground. The chip enable pin is connected to VCC via a 10K pull up resistor. The RESET pin is connected to the ground via a tactile switch where the pin is supplied VCC through a 10K pull up resistor by default. The Tx and Rx pins of the module are connected to the RX and TX pins of the Arduino UNO. The GPIO-0 pin of the module is connected to ground via a tactile switch where the pin is supplied VCC through a 10K pull up resistor by default.
这些上拉电阻充当电压分隔电路,可保护ESP8266板免受高压。Arduino董事会在5V和3.3 V下运行,而ESP8266在3.3 V处运行。尽管Arduino板本身通过其3.3 V电源引脚提供动力,但该电压分隔器电路进一步增加了任何电压激增的保护。上拉电阻的使用增加了电路的稳定性。通过这些电路连接,Arduino板本身充当了串行适配器的USB。可以将Arduino Uno和ESP8266模块之间的电路连接总结如下 -

图6:Arduino和ESP8266模块之间的表列表电路连接
During compilation of the code, the GPIO0 and RESET switches must be pressed. For uploading program, the RESET switch must be released while the GPIO0 programming switch must be left pressed, so that the ESP can enter in programming mode. After uploading the code, the programming switch should also be released as well.
Write the firmware code in the Arduino IDE and connect the Arduino board with the PC via USB cable. Open Arduino IDE and go to Tools->Port and select the Arduino board (Arduino UNO). It may look like /dev/ttyABM0 (Arduino/Genuino Uno). Select the correct port name. The port name can be different in different IDE setups. Then Open Serial monitor in the Arduino IDE by navigating to Tools->Serial Monitor and set the baud rate to 115200 bauds per second. Pass ‘AT’ and ‘AT+GMR’ commands to test the connection between the Arduino and ESP-01 module. Try different settings for the ‘Line ending’ option of the serial monitor like Both NL & CR. Try different combinations until the ESP module starts interacting correctly with the serial monitor.
DownloadPython,pip和ESPtool.Erase the pre-loaded firmware if the ESP module has any. Flash the firmware code to the ESP-01 module by writing the commands in serial monitor as instructed for the ESPtool. Check out the following link to write proper command for flashing the firmware code using ESPtool-
上传程序,必须重新复位开关leased while the GPIO0 programming switch must be left pressed. After loading the code, the programming switch should also be released as well. In this way, the firmware code can be loaded to the ESP8266 using the Arduino board as FTDI converter. It must be noted that every GND needs to be common to complete the circuit. The GPIO2 is alternative TX for bootloader mode.
删除连接。现在,ESP模块准备在项目电路中安装。在项目电路中,ESP8266与CP2102适配器和LED连接。
CP2102是UART桥的单个芯片USB。它用于为ESP模块提供电源。它也可用于引导加载。CP2102 IC具有以下引脚配置 -

Fig. 7: Table listing pin configuration of CP2102 IC

图8:CP2102 IC的表列表销件配置

Fig. 9: Table listing pin configuration of CP2102 IC
ESP板连接到CP2102,以实现引导加载模式和使用用途。对于引导加载,ESP-01 TX引脚连接到CP2102的RX引脚,ESP的RX PIN连接到CP2102的TX,以便它们都可以发送和接收数据。ESP8266-01需要3.3 V电源,因此ESP的VCC连接到CP2102的3V3,并且两者的GND都需要相互连接。ESP8266的重置引脚以及10K上拉电阻(自举电阻)连接到CP2102的RTS引脚。GPIO0是一个编程引脚,用于将ESP-01结束到编程模式中。GPIO0与10K上拉电阻一起连接到CP2102的DTR引脚。ES-01的CHPD引脚用10K电阻拔出。下表汇总了ESP模块与CP2102之间的电路连接 -

Fig. 10: Table listing circuit connections between ESP8266 ESP-01 Module and CP2102 IC
将ESP模块与CP2102适配器连接后,必须将适配器连接到PC,并且必须以与Arduino相似的方式加载固件代码的ESP模块。
加载代码后,ESP模块将使用固件中给出的SSID自动访问Wi-Fi点。LED连接到ESP模块。LED的阳极引脚连接到GPIO2,具有220欧姆串联电阻,阴极引脚连接到GND。ESP模块现在准备通过Wi-Fi路由器通过UDP协议接收数据,并将控制LED指示与UDP客户端的成功通信。

Fig. 11: Prototype of ESP8266 UDP Server
在笔记本电脑上,数据包发送者用作UDP客户端。实际上,任何支持UDP的客户端都可以使用。要将数据包发送器设置为UDP客户端,请首先打开数据包发送器并写下所需字段中的输入参数。在“名称”字段中,可以编写任何名称,以便可以唯一地识别客户端。在ASCII字段中,编写必须发送的实际数据报,并在地址中,编写UDP服务器的IP地址以及服务器正在侦听的端口。这很简单。安装并启动数据包发送者应用。填写所需的字段,客户现在可以将数据报发送到服务器。

图12:数据包发送者UDP客户端的屏幕截图
电路的工作原理 -
ESP调制解调器在此设置中充当UDP服务器。它装有一个固件,该固件可以在UDP协议上接收数据,并在响应中回来了同一端口上的确认。用UDP客户端使用的笔记本电脑使用数据包发送器应用程序发送数据报。与UDP客户端成功通信后,ESP调制解调器上的固件代码点亮了LED。
在基于ESP8266的UDP服务器的固件代码中,使用可用路由器启动Wi-Fi连接,并使用标准库函数设置UDP连接。在ESP代码中导入了一个名为Wifiudp.h的库,用于管理UDP连接并在其上接收数据。成功收到UDP数据包后,代码将在同一端口上发送确认,并将LED连接引脚设置为High。以下流程图演示了ESP8266 UDP服务器上的UDP连接和数据管理 -

图13:ESP8266 UDP服务器连接和数据管理的流程图
在笔记本电脑侧,UDP连接和数据传输仅由数据包发送者应用程序管理。
Programming Guide –
The ESP8266 modem is programmed to operate as UDP server. In the ESP code, WifiUDP.h is imported to manage the UDP connection and receive data over it. A wifiUDP object is instantiated for programming of UDP routines. The UDP port on which the server is listening is specified. For receiving any message, a buffer is set up so that no message is missed. When client sends the message, the server will acknowledge the client by sending another packet.
wifiudp udp;
unsigned int locudpport = 5291;
char incomingpacket [255];
char replyPacekt[] = “Welcome here!”;
After declaration, a Wi-Fi connection is initiated. The ESP8266 Wi-Fi is used to connect the server to the access point (Internet). The following code declares the Wi-Fi SSID and password.
const char* ssid =“检测到病毒”;
const char* password =“ 123456789”;
The following function is used to initiate the Wi-Fi connection.
wifi.begin(SSID,密码);//与网络启动ESP8266连接
建立与Internet的连接后,服务器继续使用以下功能在指定端口上的传入数据包 -
udp.begin(localudpport);
这样,在指定的端口上建立了服务器与Internet的连接。现在,服务器准备收听任何消息。当服务器接收客户端请求时,它会根据接收请求做出响应。服务器将等待接收传入数据包。当服务器接收到数据包时,它将其存储到缓冲区中并用于进一步的过程。在整个过程中,一旦收到数据包,代码将打印出发送者的IP地址和端口以及接收到的数据包的长度。如果数据包没有空,则其内容也将被打印出来。以下代码设法接收数据包 -
//listening for incoming UDP packets
int packetSize = Udp.parsePacket();
//Received Packet? Yes!
如果(Packetsize)
{
serial.printf(“从%s,port%dn接收到%d字节”,Packetsize,
udp.remoteip()。toString()。c_str(),udp.remoteport());
int len = udp.Read(incomingpacket,255);
如果(len> 0)
{
incomingpacket [len] = 0;}
Serial.printf(“UDP packet contents: %sn”, incomingPacket);
Whenever a message is sent from UDP client, the UDP server acknowledges to that client by sending acknowledgement message. For each received packet, an acknowledge packet is sent using the following functions –
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(replyPacekt);
udp.endpacket();
It must be noted that the reply is sent to the IP and port of the sender by using Udp.remoteIP() and Udp.remotePort().
So, the server is waiting to receive the packets, when the client sends the packet, the server identifies it on the basis of IP address and port number then on receiving the packet, it notify the client by sending acknowledgement packet. In this way, server-client communication is established. On successful communication of delivery of data to it, the server set the LED connected pin to HIGH lighting up the LED to visually indicate successful UDP communication.
在下一个教程中,了解COAP协议。
Project Source Code
###//程序到#include#include const char* ssid =“检测到病毒”;const char* password =“ techshlok”;int status_led = 2;wifiudp udp;unsigned int locudpport = 5219;//当地端口以聆听char收到的媒介[255];//用于传入数据包的缓冲区char venterge [] =“收到您的消息:”;//回复字符串以发送void setup(){serial.begin(115200);pinmode(status_led,输出);serial.println(); Serial.printf("Connecting to %s ", ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("Wi-Fi connected!"); Udp.begin(localUdpPort); Serial.printf("I am listening at IP %s, UDP port %dn", WiFi.localIP().toString().c_str(), localUdpPort); } void loop() { int packetSize = Udp.parsePacket(); if (packetSize) { // receive incoming UDP message Serial.printf("Received %d message from %s, port %dn", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort()); int len = Udp.read(ReceivedMessage, 255); if (len > 0) { ReceivedMessage[len] = 0; } Serial.printf("UDP message: %sn", ReceivedMessage); digitalWrite(status_led, HIGH); delay(1000); digitalWrite(status_led, LOW); // send back a reply, to the IP address and port we got the packet from Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); Udp.write(Acknowledge); Udp.endPacket(); } } ###
与本文有关的问题?
Ask and discuss onedaboard.com和Electro-Tech-online.com论坛。
告诉我们你的想法!!
You must be登录发表评论。