HTTP vs TCP/IP, send data to a web server
I'm currently working on a project where I need to use an Arduino Nano (http://arduino.cc/en/Main/arduinoBoardNano) to send data from a temperature sensor to a web server.
At first I thought it would be easy, since there are so many great libraries out there to help with POST/GET etc. However, my professor just told me that I need to send data to the server using TCP/IP, and as I understand it POST and GET are HTTP methods.
Could someone explain to me the difference between HTTP and TCP/IP? Specifically as it relates to sending data to a web server. I'm looking for an answer that isn't too technical (I'm pretty new to all of this).
Finally, if there is anyone out there with experience making an Arduino do what I've described above, I would really appreciate some pointers.
Thanks!
Solution 1:
In Short: TCP is a transport-layer protocol, and HTTP is an application-layer protocol that runs over TCP.
Detail:To understand the difference (and a lot of other networking topics), you need to understand the idea of a layered networking model. Essentially, there are different protocols that let a computer talk at different distances and different layers of abstraction.
At the very bottom of the network stack is the physical layer. This is where electrical signals or light pulses or radio waves actually transmit information from place to place. The physical layer doesn't really have protocols, but instead has standards for voltages, frequencies, and other physical properties. You can transmit information directly this way, but you need a lot of power or a dedicated line, and without higher layers you won't be able to share bandwidth.
The next layer up is the link layer. This layer covers communication with devices that share a physical communications medium. Here, protocols like Ethernet, 802.11a/b/g/n, and Token Ring specify how to handle multiple concurrent accesses to the physical medium and how to direct traffic to one device instead of another. In a typical home network, this is how your computer talks to your home "router."
The third layer is the network layer. In the majority of cases, this is dominated by Internet Protocol (IP). This is where the magic of the Internet happens, and you get to talk to a computer halfway around the world, without needing to know where it is. Routers handle directing your traffic from your local network to the network where the other computer lives, where its own link layer handles getting the packets to the right computer.
Now we are getting somewhere. We can talk to a computer somewhere around the world, but that computer is running lots of different programs. How should it know which one to deliver your message to? The transport layer takes care of this, usually with port numbers. The two most popular transport layer protocols are TCP and UDP. TCP does a lot of interesting things to smooth over the rough spots of network-layer packet-switched communication like reordering packets, retransmitting lost packets, etc. UDP is more unreliable, but has less overhead.
So we've connected your browser to the web server software on the other end, but how does the server know what page you want? How can you post a question or an answer? These are things that application-layer protocols handle. For web traffic, this is the HyperText Transfer Protocol (HTTP). There are thousands of application-layer protocols: SMTP, IMAP, and POP3 for email; XMPP, IRC, ICQ for chat; Telnet, SSH, RDP for remote administration; etc.
These are the five layers of the TCP/IP networking model, but they are really only conceptual. The OSI model has 7 layers. In reality, some protocols shim between various layers, or can work at multiple layers at once. TLS/SSL for instance provides encryption and session information between the network and transport layers. Above the application layer, Application Programming Interfaces (APIs) govern communication with web applications like Quora, Twitter, and Facebook.
- Source: Quora: Difference between HTTP protocol and TCP protocol.
Solution 2:
HTTP is a protocol used mostly for browsing the internet (IE, Firefox, etc). It rides on top of TCP which provides a reliable link between two computers (if packet get lost - it is re-transmitted). TCP itself rides on top of IP, which provides unified addressing to communicate between computers. TCP/IP is a basis for internet and 99% of other networks.
Basically it means if you are communicating HTTP, you are doing it with TCP/IP underneath (but I am sure this is not what your professor meant).
Arduino Nano is not supporting all of those, so you need something in between, which will translate Nano signaling to TCP/HTTP communication.
Some of your options are:
- Communicating with Nano over Serial and making PC translate your Serial protocol to HTTP/TCP.
- Switch Nano with some other Arduino board which supports Ethernet/Wifi shield extension (Uno/Mega), or choosing a custom board which contains Ethernet by itself.
- Using another Arduino (Uno/Mega) with Ethernet shield as an additional board which communicates with Nano over Serial or with the help of RF modules (I personally implemented this option in past).
- Another unusual option is to attach Nano to your Android smartphone using Audio cable and to use soft-modem library. (https://code.google.com/p/arms22/issues/detail?id=2), which contains implementation for Android and write an application for Android
Web server you mentioned supports HTTP only by definition, so if you want to communicate over TCP, you will need to use some TCP server.
One of the existing web services to provide graphs for visualizing Sensor data is https://xively.com/, its API is based on REST which rides on top of HTTP. But it is not the only one.
Solution 3:
IP vs. TCp vs. HTTP
Think of IP
as a sort of high-way that allows other protocols to get on and find their way to other computers. TCP
and UDP
are the "trucks" on the highway, and the "load" they are carrying are protocols such as HTTP
, File Transfer Protocol (FTP
) and more.
IP
is required to connect all networks;
TCP
is a mechanism that allows us to transfer data safely and
HTTP
which utilizes TCP to transfer its data, is a specific protocol used by Web servers and clients.
Solution 4:
@Miro answered the question well, in technical speak, but for the newbies on the topic of networking, I would like to provide a explanation in not-so-technical-speak:
One might regard TCP or UDP as the equivalent of the US Postal Service, while HTTP is the equivalent of one business letter template. If you are writing to an unknown person, as business letter format is a good, generic way to transmit the "WHO,HOW,WHAT,WHEN,AND WHERE" of your message, but it is NOT the only format that is allowed by the USPS. That is, if you are writing to a business associate or loved one, you might forgo the formalities and instead go with a more efficient format, like a billing invoice or love letter or greeting card, but the USPS, like TCP or UDP, will still be there for you, carrying the letters back and forth.
Solution 5:
Offhand, there is no special reason why TCP/IP or HTTP should be preferred, apart from HTTP being easier to code up. There is some overhead associated with HTTP, but it is generally small compared to latency lags. Moreover, a good TCP/IP link will have code about it, for reliability, which HTTP provides for free.
So, unless the professor in question has some special technical requirement (ask!), or unless they want this to be some kind of special learning experience (ask!), it seems to me that a POST/GET-type interaction is superior. Many systems today indeed communicate using JSON objects in this manner.