Many IoT data hosting providers have multiple options for protocols to send and receive data. Almost all of them support a basic HTTP GET/POST protocol, which is what we explore here!
Please do not request to join the team!
We've all heard of HTTP - it's in the browser bar right now! But what is HTTP, really, and how can we use it to send data to a server? Well, HTTP stands for HyperText Transmission Protocol. It runs on top of TCP, or Transmission Control Protocol. You don't need to fully understand these protocols, just grasp the basics. TCP is responsible for taking data from an application, assembling it into a packet, and passing it to the ethernet or wi-fi card to transmit across the Internet. At the other end, the reverse happens. In the case of HTTP, the data is hypertext, or HTML. But it doesn't have to be HTML. HTTP can be used to transfer files, images, scripts, videos and more.
The two most important requests are called GET and POST. These are commands in the HTTP standard that an HTTP server will understand. When I want to view google.com, my browser sends a GET command to google.com's web server, and it returns the homepage. When I type a search into the search bar and hit enter, my browser sends a POST command to the server with whatever I typed. Think of it like posting a letter!
Alongside the GET and POST commands, HTTP also sends some extra data called headers. These headers are little tidbits of information the server can use to modify what it gives you. For example, a common header is the User-Agent header. This tells the server what kind of browser we are using. Ever wondered how it is you get a different layout when you go to a website on your smartphone? Your smartphone's browser is telling the server via the User-Agent header that it's a mobile device, and so the server sends a different layout so that it fits on your smaller screen!
In our case, all we want to do is get some data from a sensor, and send it to a server. So we will only be using the POST request. We will also be including some data in the HTTP headers, so that the server knows who we are and doesn't reject our requests (they don't want you sending data to someone else's stream!).
There are a huge number of services available that will allow you store and display data for free. Many are open-source, too, which is a bonus! For this tutorial, we will be using Adafruit IO, but once you understand the process you can easily substitute any other service. Popular ones include Freeboard.io, Thingsboard, Node-RED, Thingspeak, and many, many more.
The documentation for the Adafruit IO REST API is located here. I suggest taking at least a cursory look before reading the rest of this tutorial. Make sure to take a look at the createData operation - this is the one we will be using.
Sign up for Adafruit IO
As I mentioned previously, you are free to use any cloud data service you want! However, if you want to be able to use the code as provided, then it's easier just to use Adafruit IO. You can sign up by visiting https://io.adafruit.com/ and clicking the Sign Up button.
Once you've signed up, navigate to the Feeds section in the left-hand toolbar, and then create two feeds. In my image below, I have created a feed called bedroomhumidity, and bedroomtemp. I've also placed these into a group called Bedroom, but this isn't necessary.
Once you've created the two feeds, click on the name of one of the feeds, and then click Feed Info in the right-hand toolbar. This brings up all the info about the feed, including the API endpoint that we will need later. Make sure to also click on View AIO Key -- we will need this key later as well.
That's all we need for now! Later on, feel free to play with creating Dashboards to view your data.
Installing libraries and breadboarding
In order for this project to build and upload successfully, we need to add a few more libraries to our Arduino environment. Fire up Arduino, and then just like way back in part 1, open the Library Manager by pressing Ctrl-Shift-I. In the search box, type DHT, and scroll down until you see Simple DHT. Click install. It should look like this:
After that, enter OneWire into the search box, and install the OneWire library which looks like this:
Now that we've got that out of the way, let's take a look at our circuit, and the sensors we will be using. Both the DHT22 and the DS18B20 are very widely available, and quite inexpensive. They can be found from the usual suspects, as well as auction websites and Chinese importers. The DS18B20 was made by Dallas, who were acquired by Maxim in 2001.
The DHT22 is a low-cost humidity sensor with surprising accuracy for the price. It actually has a built-in sensor similar to the DS18B20 for thermal compensation of the humidity sensor. The reason I am collecting temperature data from a separate DS18B20 is because I have one on a longer wire, encased in a metal probe. This allows me to place the probe away from the DHT sensor, meaning I can sample temperature from two places at once if needed. However, if you only have one or the other (or a different sensor altogether) this project will still work with some modifications.
Grab a breadboard, a couple 1K resistors, your sensors and your ESP8266 and let's put it all together!
The above diagram uses an Adafruit Feather HUZZAH but you can use any ESP8266 board. Just make sure you connect the sensors to the same pins, or edit the code to match the pins you picked. Also, be sure to power the sensors from 3.3V as the ESP8266's pins cannot handle 5V!