I have been working on using an Arduino and an ESP8266 wifi module to log data on an online platform for the past few weeks and after many hurdles, I have something that works reliably.
The general progress can be seen in the previous blog posts, so I will not discuss that.
This code hinges around sending HTTP GET requests. GET is an HTTP method that "asks" for a resource (a web page) from a server. In this case, we are requesting to send data to the IO platform. A GET request follows the general format:
Connection: close\r\n\ (connection can also be kept alive with keep-alive)
When sending these over the ESP, \r\n (carriage return and newline characters) must be appended to each line (the last line has an extra \r\n). Without them, the command may not be properly understood by the server.
Once this command is sent, the server will respond by returning html. This is essentially how an internet browser functions: a URL is entered, the browser receives html from a server, and then displays the html. In this case, however, the response is going to the ESP, which will display "+IPD" (which indicates the ESP has received data) to the serial monitor and then the HTTP code and some html. My information of networking and HTTP is very limited, so much more information can be found online. A resource I skimmed can be found here.
First, I created an Adafruit IO account and familiarized myself with the platform. Adafruit IO is a platform for the internet of things that makes it easy to record and view data. They have a lot of documentation, tutorials, and support, so it is a good choice. Note that their recommended route for using the ESP8266 involves the Huzzah breakout and uploads code directly to the ESP8266. This was not quite what we wanted to do, so I worked on interfacing with the ESP through the Arduino by sending AT commands.
Next, wire up the ESP and Arduino and get familiar with the ESP and its AT commands. All of this information can be found in this previous blog post. The code there is for a different purpose, so that can be ignored.
After that, upload this code and fill in your own WiFi and Adafruit credentials.
This should be enough to get a bare bones method of logging data up and running.
Additionally, here are a couple of posts and websites that helped me troubleshoot and understand what was going on:
- Arduino forum on an ESP GET request issue
- The Adafruit page that explains how to send and receive data using HTTP requests
- A post with more detail on some AT commands
- A video that goes over the process, which helped me break through my issues