 This video will teach you how to send and receive UDP data packets using the ESP8266. In this tutorial we will use Arduino to write a simple ESP8266 application, which can send and receive UDP data packets. In order to complete this tutorial, you will need to configure the Arduino IDE, so that it supports boards with the ESP8266 microcontroller. If you have not already added ESP8266 boards to Arduino, follow the link on screen for the tutorial that explains how to do this. You can use any board that features the ESP8266 microcontroller. We will be using the Wemos D1 mini board. In order to complete this tutorial, see the link on screen for further details about this board. With that said, let's begin by starting a new project in the Arduino IDE. The first thing we need to do is include the ESP8266 Wi-Fi library, using an include statement. Next, we will create two constant values where we can enter the username and password for our Wi-Fi router. Create a new file and give it the name, Wi-Fi.h. Enter the defined statements for username and password. Now, add an include statement to the main file to include the Wi-Fi credentials file. Inside of the setup function, we will first begin the serial port. This will allow us to use the Arduino serial terminal to communicate with the ESP8266. Then we will begin our Wi-Fi connection, specifying the username and password that we defined earlier. Next, we will print a message to the serial terminal, which confirms that our ESP8266 is beginning an attempt to connect to the specified network. At this point we will halt our application until a Wi-Fi connection is established. We can do this using a while loop, which evaluates whether or not a connection has been made. Once a connection has been established, the while loop exits and allows the program to continue. Now we can print a message to confirm that a connection has been established, followed by the newly assigned IP address that our device has obtained. At this stage we will complete our program with an empty loop function, so that we can test the Wi-Fi connectivity. Connect your ESP8266 to the computer and select the correct board and port from the Arduino tools menu, and then build the project. Once the build has completed successfully, open the serial terminal, set the board rate to 115200, and then reset the board. The connection process should be reported in the serial terminal, with the assigned IP address being printed following successful connection. Now that we have the framework in place for a Wi-Fi connection, we can add UDP capability. First, we need to add the UDP library header file, using an include statement at the beginning of the project. Then we will add a constant value to represent the desired UDP port. Next, we'll create an instance of the UDP class. We will name it UDP, but you can give it a different name if you prefer. Then we will add a char array called packet, in order to buffer the received data. We will also add a char array called reply, in order to buffer the data to be sent back to the sender. The default message, packet received, will be placed into this array as a default reply message. Moving to the setup function, we will begin instance of the UDP class that we defined earlier, after the Wi-Fi connection has been established. We will output confirmation to the terminal once UDP has started. In order to handle received packets, we will use the main function loop to continually check if a packet has been received. We will call the pass packet function, which checks for the presence of a UDP packet, and returns the packet size if a packet is received. We will load the packet size into a new integer variable called packet size, and test it using an if statement. If a UDP packet is received, the packet size integer will be greater than zero. In which case the contents of the if statement will be executed. If no packet has been received, the packet size integer will be equal to zero, and the code within the if statement will be ignored. The code that we wish to use to handle our received packet should therefore be placed within the if statement. On receiving a packet, we will print the details to the serial terminal. First, we will print the size of the packet, which we already have stored in the packet size integer. Next, we will use the read function to read the contents of the packet into the char buffer, which we created earlier. A second argument is given to specify the maximum packet size. The read function also returns the length of the receive packet, and we will store this in a new integer, len. Now we have our receive packet stored in the char array packet. We also have the length of this array stored in the integer, len. We need to add a null terminator to the end of the string in the packet array, in order to mark the end of the string. The index number of the char at the end of the packet array is equal to the array length. Therefore we can use the array length variable, len, as the index position, in order to place the null terminator at the end of the string. Now we can print the string that was received to the terminal window. The final thing that we need to add to our program is the code to send a response. Firstly, we need to use the begin packet function, which requires two arguments. The first argument is the IP address we wish to send data to, and the second argument is the port. Rather than using a specific address and port, we can use the remote IP and remote port functions instead. These functions return the IP address and port of the message sender. This means our program will automatically reply to the sender of the received packets. Next, we will write the contents of the reply buffer, specified earlier in the program. Lastly, we will call the end packet function, in order to send the reply. Now we are ready to build and upload the project to the ESP8266. Next, we will test our project using a tool called packet sender. Packet sender is a free utility for sending and receiving network packets. It supports Mac, Linux and Windows operating systems. Follow the link on screen in order to download it. Open the serial terminal, ensure the board rate is set to 115200, and then reset the ESP8266. Next, go ahead and open packet sender. Make sure it is operating in IPv4 mode using the button in the bottom right hand corner. We will create a test packet to send to our ESP8266. The message that we wish to send should be written in the ASCII box. Enter the message and press enter. Packet sender will automatically convert it to the equivalent array of hex values in the box below. Enter the IP address of the ESP8266 in the address box, and enter the port. Choose UDP in the protocol dropdown box. Press the save button if you wish to save the packet for future use. Now, go ahead and press the send button. The message written in the ASCII box should be received by the ESP8266, and then displayed in the Arduino Serial window. The ESP8266 should automatically send a reply, which will be received by packet sender. Congratulations! You have successfully configured your ESP8266 for sending and receiving UDP packets. Visit sitech.com, forward slash ESP, for more tutorials like this.