In this project, we will learn the basics of Transmission Control Protocol (TCP) and how to communicate to a webserver over TCP. We will request and receive packets from the Google web server using the above communication protocol. The 4Duino display is used to print the status of the connection and the received information for debugging purposes.
Transfer of Data over Internet
The 2 most common ways to transfer chunks of data (also known as packets) over the internet are Transmission Control Protocol (TCP) and User Datagram Protocol (UDP). This is built on top of the IP layer. This means whether you are sending a packet via TCP or UDP, that packet is sent to an IP address.
An Internet Protocol address (IP address) is a numerical label assigned to each device in a network. IP addresses are usually written and displayed in human-readable notation, such as 18.104.22.168. When you type the name of the website (www.google.com) you are actually referring to the IP address of the server associated with that website name.
When a service is initially started it is required to bind to a designated port number. This is requested by the client. Higher level application that uses TCP/IP such as Web protocol, Hypertext Transfer Protocol, have ports preassigned numbers. These are called “well-known ports”. For the HTTP service, port 80 is defined as default.
More on TCP
TCP is the most commonly used protocol on the Internet and it is connection oriented. It guarantees the recipient will receive the data in order by numbering them. Upon receiving the packets, the receiver must send a message back to the sender to acknowledge it received the message. If the sender doesn’t receive this acknowledge message it will assume the data was lost and resend the packets to ensure the recipient received them. This is one of the ways TCP ensures reliability of the communication. TCP is all about reliability – information sent with TCP are tracked so no data is lost or corrupt in transfer. However, this has a negative impact on the speed of data transfer.
HOW IT WORKS
When you type a website on the browser, your computer sends packets to the server’s IP address over TCP requesting for the web page. Your computer simultaneously requests the service to bind to a designated port, i.e. 80 for HTTP. The web server in return sends a stream of TCP packets, which your web browser puts together as a web page and display it for you.
In this project we are going to request and receive time stamp from the Google web server over TCP and print it on the 4Dunino display.
In the skeleton code, there is a section commented to initialise the ESP8266 Wi-Fi Module. All you need to do is uncomment these lines of code and the initialisation is done for you. Now the Wi-Fi modules is ready to be controlled using AT Commands.
The default baud rate is set to 115200 bps, however the maximum baud rate at which Software serial actually works is 19200 bps. So in the code it requires changing the baud rate and this is done for you in the skeleton code.
ESP8266 is set to station mode and joins the AP, which can be your home router. SSID and PASSWORD are defined in the code respectively. Also, the program sets the modules to single connection mode. In single connection mode, only one TCP or UDP communication can be created.
Simply change the SSID and the PASSWORD to suit your router. Also, webserver IP address (GOOGLE) and the port number (HOST_PORT) is defined.
4Duino initiates communications with the Google webserver over TCP and binds the service to the port 80 which is dedicated to HTTP. If successful it prints “create tcp ok” on the display.
4Duino sends a GET request of the web page.
Webserver responds by sending the requested data over TCP. The time stamp is in the http header. So it is extracted from the header and printed on to the display.
Close the TCP connection.
This projects only utilizes the internal circuitry of the 4Duino. Hence you do not require any components for this project. However, a micro USB cable is required to program the 4Duino.
Step 1: Build
4Duino has an inbuilt ESP8266 Wi-Fi module. Hence, there is no external circuitry required for this project.
Step 2: Program
Workshop 4 – 4Duino Basic Graphics environment is used to program this project. (The same could be implemented in the latest Arduino IDE)
This project requires the Arduino IDE to be installed as Workshop calls the Arduino IDE for compiling the Arduino sketches. The Arduino IDE however is not required to be opened or modified to program the 4Duino.
Open this file using Workshop 4.
Change the SSID and the PASSWORD to suit your router, in the program.
Note: Download the project here.
Step 3: Comms Port
Connect the 4Duino to the PC using µUSB cable. Then navigate to the Comms tab and select the Comms port to which the 4Duino connected.
Step 4: Compile and Upload
Finally, go back to “Home” tab and now click on the “Comp’nLoad” button.
After uploading 4Duino communicates to the web server over TCP and prints status and time stamp on the 4Duino display.