Using the ESP as a simple microcontroller is great, but the reason why most people use it, is its Wi-Fi capabilities. All these acronyms might intimidate you, but I'll try my best to explain them step-by-step and in an easy way. Some paragraphs are in italic.
These provide some extra information, but are not critical to understanding the ESP's Wi-Fi functions, so don't get frustrated if there are things you don't understand. It's really hard to give a clear explanation, without over-complicating things and while keeping it short enough as well. If you've got any feedback or remarks, be sure to leave a comment to help improve this article.
We'll go over these different layers, because we need to understand how our ESP communicates with other devices on the network.Tech Note 121 - ESP32 Webserver and UDP to control remote Devices
The link layer contains the physical link between two devices, an Ethernet cable, for example, or a Wi-Fi connection. This is the layer that is closest to the hardware. This can happen in two different ways:. The AP can be built-in to your modem or router, for example. In this configuration, the ESP acts like a wireless station. The ESP acts as an access point and wireless stations can connect to it.
These stations could be your laptop, a smartphone, or even another ESP in station mode. All devices on a LAN can communicate with each other.
If the ESP is in access point mode, it can communicate with any station that is connected to it, and two stations e. The link layer is the physical link between devices: in the case of the ESP, this is a WiFi connection. The ESP can act as a station and connect to an access point, or act as an access point and let other devices connect to it. Although the devices are now physically connected either through actual wires Ethernet or through radio waves Wi-Fithey can't actually talk to each other yet, because they have no way of knowing where to send the message to.
That's where the Internet Protocol IP comes in. Every device on the network has a personal IP address. IPv6 is an improved version of IPv4 and has much more addresses than IPv4 because there are much more devices than available IPv4 addresses. The IP address consists of 4 numbers, for example The advantage of the ESP module is that it contains a micro controller on board, so no additional Arduino board is needed.
ESP8266 Community Forum
Of course there are some negative sides. The ESP runs on 3. While searching the internet for examples most of the below code was picked from the net at various places I came across many questions how to implement a direct data transfer without the nice "do it like that" examples.
There was some example code, but mostly with question why it did not work. So after some reading and trying to understand, I created the below examples which allow fast and simple transfer of data between two ESP Did you use this instructable in your classroom? Add a Teacher Note to share how you incorporated it into your lesson. The might be different ones, but most examples use the mentioned on.
When using this, you need to get your communication to WiFi level.
ESP8266 - AT Command Reference
The AP provides the name of the network and the IP addresses and the client will connect to this server. So compared the nRF24l01, where the code at both ends is more or less the same except for the transmission channels the code of the ESP is fundamentally different, as one is configured as AP and the other as client. The next topic is, that instead of just sending some bytes to the nRF24l01, for the ESP transfer protocols need to be observed.
The TCP Transmission Control Protocol is a protocol which allows a loss-free transmission between a server and a client. Additionally, by using all these handshakes the protocol prevents data lost due to many packets send at the same time in the network.
Data packets wait until they can be received. Its overhead is therefore smaller and there is no need for all the handshakes to maintain a connection. UDP incorporates some basic error detection, but no correction the corrupted package is just dropped. Data are send, without the knowledge if the receiving party is free to receive the data.
At the same time, multiple packets can collide, as each party sends the data whenever it is needed.For example, imagine a relay that has to be activated at a certain time or a data logger that has to store values at precise intervals. But these chips are not perfectly accurate so, you need to do manual adjustments over and over again to keep them synchronized.
The protocol can be used to synchronize all networked devices to Coordinated Universal Time UTC within a few milliseconds 50 milliseconds over the public Internet and under 5 milliseconds in a LAN environment. UTC does not vary, it is the same world wide. NTP sets the clocks of computers to UTC, any local time zone offset or day light saving time offset is applied by the client. In this manner clients can synchronize to servers regardless of location and time zone differences.
NTP uses a hierarchical architecture. Each level in the hierarchy is known as a stratum. At the very top are high-precision timekeeping devices, such as atomic clocks, GPS or radio clocks, known as stratum 0 hardware clocks. Stratum 1 servers have a direct connection to a stratum 0 hardware clock and therefore have the most accurate time. Each stratum in the hierarchy synchronizes to the stratum above and act as servers for lower stratum computers. NTP can operate in a number of ways. The most common configuration is to operate in client-server mode.
The basic working principle is as follows:. Follow the next steps to install this library in your Arduino IDE. There should be a couple entries. Click on that entry, and then select Install. The following sketch will give you complete understanding on how to get current day and time from the NTP Server. Before you head for uploading the sketch, you need to make some changes to make it work for you. After uploading the sketch, press the RST button on your NodeMCU, and you should get the current day and time every second as shown below.
First, we include the libraries needed for this project. We also define daysOfTheWeek 2D array.Click the download icon on the right to get release notes in detail and download the SDK of the latest version.
AT Notes:. While using it, users should use. Slow connection problems in some cases have been solved. Its parameter is true. Users should confirm that only when ambient temperature is higher than the normal operating temperature range that crystal oscillator requires does this function need to be enabled.
When this patch is used. Therefore this process can be carried out by any user, including end users of ESP based products.
However, with appropriate build settings based on your application flash configuration, the test can be made to run on any ESP based system.
ESP device will download a new version of firmware from the update server over Wi-Fi and run it. However, with appropriate build settings based on your application flash configuration, the test can be made to run on any ESPbased system. The demonstration initially runs the user1. The user1. When a system restart is triggered, the ESP boots into the updated user2.
This new update is downloaded as user1. This cycle is repeated for every subsequent update. The UART passthrough mode has been provided with the AT firmware to facilitate sending of data blocks of large size and for continuous transmission of small packets of data. In the passthrough mode, the data sent by the main application processor is directly forwarded to the target networking device via a TCP or UDP connection.
In power critical applications, it is important to balance the average or burst data transfer rate as well as overall power consumption of the system. This test demonstration is intended to demonstrate the results of various power consumption tests on the ESP Based on this guide, users can choose the appropriate PHY mode and data transmission scheme for their application.
This guide is intended to help implement and test the available low power modes and observe the resulting current consumption of the ESP easily and quickly. It demonstrates the Modem-sleep mode, Light-sleep mode, and Deep-sleep mode. This guide is intended to help users run ping test on the ESP easily and quickly. The ping test has been demonstrated and run on the ESP-Launcher. However, with appropriate build settings, the test can be made to run on any ESPbased system.
The size of write-buffer is bytes. The tuning may seem like a noise wave. This document introduces the Simple-Pair technology developed by Espressif. The document focuses on Simple-Pair features, their uses and the demo code. This guide introduces the methods of identifying the causes of ESP reset and common Fatal exceptions.
X and earlier versions. This document provides introduction to the interfaces integrated on ESP Functional overview, parameter configuration, function description, application demos and other information is included. This document introduces the specifications of ESPEX hardware, including overview, pin definitions, functional description, peripherals, and electrical characteristics, etc.Add the following snippet to your HTML:.
Read up about this project on. ArduinoIDE 1. IoT is a hot spot in technology right now; however, the " I " stands for I nternet and maybe you do not wish to have your IoT toys logging into your SOHO access point and accessing the Internet router. The AP appears on any WiFi enabled device and can be connected to easily; in this project, the AP is not passworded, so the connection is very simple from notebook, PC, tablet, or smartphone.
The AP of the ESP is configured to be a captive portal from a DNS perspective, which is explained in more detail in my recent hackster project: here. It would be trivial to add the code for the webserver and to define a webpage that will output the time and date when a browser connects. Eventually, the concept is to utilize a second or more ESP as UDP receivers which will be placed throughout the house where traditional clocks are now located.
It may look "funky" but it is very effective! When the Processing Sketch is compiled and run, the console display should start showing the GPRMC sentence being broadcast once-per-second. At this time, I am going to call the circuit stable; I am getting a consistent Bytes of free memory displayed on the serial console. I am concluding the POC today and listing it as a success.
Just double-check that the console is at BAUD. Resetting the ESP will show the following on the console:. Note that Pin 2 is set for Output. In final use, the serial output will be disabled. For the ESP firmware sketch which is compiled by Arduino 1. In addition, you will need the Streaming library and the Utility library the. The sketch code is shown lower down this page, just before the Comment section. The Streaming library is installed in your Arduino Library folder as it is usable for all Arduino models.
The two Utility files are installed in the sketch folder as they are specific to this project. After installing Processing on the PC, you need to copy and run the following sketch. The project runs at 3. Therefore, a very high quality, low noise power supply must be used.
I am using a 5V 1A wall wart from an old Blackberry device and using a 5V to 3. With the ambient temperature of the air at 74F You should see the LED flash on and off once per second.
Now there is very little set up to do here Firstly edit these two lines with the details of the WiFi network that your laptop is connected to.
Once this has been done upload the firmware to your NodeMCU dev board. Once it is uploaded open the serial port at Baud and you should something like this:. Connecting to WiFi Connecting……. Now take the IP address which has been allocated to your ESP and add it to the following line in your processing script.
At this point you may also want to choose a different port you will have to change it on the Arduino as well if this number conflicts with something in your current network. Now you are ready to roll! Run the processing script. When you press and hold the space bar the LED should light up. You can also press f once to turn on flicker mode whereby the led will turn on and off continuously until you press f again.
Woo Hoo so it works… but how do you use this for another application? On both sides you will need the appropriate UDP library. On the Processing side to send data you form an array of bytes which you then send using the one line command below.
It is easy to split integers and longs into bytes and then reassemble them on the other side. To recieve a UDP packet in processing you use the following code.
It utilises a default handler for when a UDP packet is received. Incoming data is stored in an array of byte much like the one used to send the data. To send data from the Arduino it is very similar but requires a few extra steps.
You need to begin the packet, write the data to it and then use end packet to send the data. You also need to specify the size of the byte array.
To receive Data on the Arduino. To avoid error you should check that there is a packet available packet size not 0. It was read into a packetBuffer of the maximum possible packet size. Like Liked by 1 person. Like Like.
Does not look like C, or bash. UDP that the demo shows is only for transmitting the data of the WiFi connection. To communicate over the usb connection you have to use serial. There are plenty of examples of this as it is identical to the standard arduinl serial communication over usb. If it is another one, could you provide it. I am not certain they are the same. The one I used was included with the node MCU board package that you can install using the idea board manager option.
I assume it is the same as the arduino one with the exception of any low level changes due to the different chip architecture. First off all: thousand thanks to you for this tuto!There are many applications where you want to know the time. In a normal Arduino project, you would have to get a RTC module, set the right time, sacrifice some Arduino pins for communication And when the RTC battery runs out, you have to replace it.
On the ESP, all you need is an Internet connection: you can just ask a time server what time it is. There are a couple of differences, but it's really easy to use, thanks to the great libraries that come with the ESP Arduino Core. The main difference between TCP and UDP is that TCP needs a connection to send messages: First a handshake is sent by the client, the server responds, and a connection is established, and the client can send its messages.
After the client has received the response of the server, the connection is closed except when using WebSockets. To send a new message, the client has to open a new connection to the server first. This introduces latency and overhead. UDP doesn't use a connection, a client can just send a message to the server directly, and the server can just send a response message back to the client when it has finished processing.
There is, however, no guarantee that the messages will arrive at their destination, and there's no way to know whether they arrived or not without sending an acknowledgement, of course. This means that we can't halt the program to wait for a response, because the request or response packet could have been lost on the Internet, and the ESP will enter an infinite loop.
Instead of waiting for a response, we just send multiple requests, with a fixed interval between two requests, and just regularly check if a response has been received. We'll also need to allocate memory for a buffer to store the UDP packets. For NTP, we need a buffer of 48 bytes long. Then wait for a connection startUDP ; if! We'll look at the implementation of this function later. There's not much we can do without the IP address of the time server, so if the lookup fails, reboot the ESP.
The first part of the loop sends a new NTP request to the time server every minute. This is based on Blink Without Delay.
Wireless Temperature Monitor with ESP8266 and Raspberry Pi
Then we call the getTime function to check if we've got a new response from the server. If this is the case, we update the timeUNIX variable with the new timestamp from the server. If we don't get any responses for an hour, then there's something wrong, so we reboot the ESP. The last part prints the actual time. Nothing special here, just a function to connect to Wi-Fi, and a new function to start listening for UDP messages on port Then wait for a connection wifiMulti.
In the getTime function, we first try to parse the UDP packet. If there's no packet available, the function just returns 0. If there is a UDP packet available however, read it into the buffer.
The NTP timestamp is 32 bits or 4 bytes wide, so we combine these bytes into one long number. To request the time from the NTP server, you have to send a certain sequence of 48 bytes.
We don't need any fancy features, so just set the first byte to request the time, and leave all other 47 bytes zero. To actually send the packet, you have to start the packet, specifying the IP address of the server, and the NTP port number, port Then just write the buffer to the packet, and send it with endPacket. The last three functions are just some simple math to convert seconds to hours, minutes and seconds.
Enter your Wi-Fi credentials on linesand hit upload. If you have a working Internet connection, you should get an output that looks like this:. You should see the time update every second, and Sending NTP request If you don't have an Internet connection, the DNS lookup of the time server will fail:. If your connection is not reliable, or if there's heavy traffic, you might encounter some dropped packets:.