Library to make HTTP queries with an ESP8266 WiFi module and Arduino

Library to make HTTP queries with an ESP8266 WiFi module and Arduino

Library to make HTTP queries with an ESP8266 WiFi module and Arduino

A simple way to send information to and from a microcontroller is by centralizing it on a web server. Although it is not as efficient a method as, for example, directly accessing a database, it is effective enough, especially if you consider a project based on a microcontroller, and add the advantages of ubiquity (data in the cloud) and simplicity (it can be handled as text information and orders). Sending information using this system can consist of making HTTP POST requests and receiving it by analyzing the content of the response to an HTTP GET request.

Table of Contents

    El ESP8266 WiFi module It is a very economical option, with a performance very suitable for use with microcontrollers and very easy to use working with AT commands.

    To systematize its use with HTTP requests, I have developed a small library that supports the needs of my sleep management device project (which I have called SleepManager) since it bases its infrastructure on a web server that allows it to be extended to an object. IoT as cloud service. It is not difficult to add other services such as UDP requests to the library, for example, to time synchronization over NTP although, as I explain in the linked text, it is not critical for my needs and I can resolve it acceptably with an HTTP request to a web page prepared for that purpose, as I show in an example of use of this library.

    The operation of the library is based on sending AT commands when the device is available and repeating them (with a small delay) in case of error, which is interpreted as non-availability (successfully, by testing) for example from point WiFi access or server being consulted.

    The library takes advantage of the fact that the ESP8266 WiFi module returns a +IPD code as a warning of data reception to fill a small buffer with the information returned by the server. For the sleep management device project I need to analyze very little data returned from the server so, to economize, the buffer and the pointer that run through it are especially small; This will be one of the first things you will have to change to reuse the library to process a larger volume of data.

    The functions that are exposed as public allow (1) to know the status of the module: if it is connected or not, if there is data in the buffer and how much and if the ESP8266 module has completed the operation that has been requested; (2) connect to a WiFi access point on a network with a DHCP server and (3) make HTTP GET and POST requests although, as you will see, the code is designed to make it very easy to add others.

    Regarding the internal functioning, firstly two matrices are built, one with the AT orders and another with the expected responses for success and error; Then, the program that uses the library must call the function from time to time send which will be in charge of sending the orders to the module if they are available and not busy and to the function to receive which will process responses from the ESP8266 module (and from the HTTP server through it if applicable)

    As there is a possibility that the module could be disconnected due to signal loss, the main program can check if the ESP8266 module is connected to the WiFi access point (with the function connected) and try to connect otherwise (with the function connect_wifi)

    To know if the requested operation has finished, the program can use the function operation_finished and in that case send another or use the resulting data for which the function is used read_buffer, which returns the contents of the buffer that has been filled with the data that arrived at the ESP8266 module until the operation is completed, and the function buffer_length which reports the amount of this data that the server has warned that it was going to be sent (so that the information from the module itself can be discounted)

    The example below uses the HTTP query library with the ESP8266 WiFi module to send data to the server (a percentage obtained from reading an analog input) every certain time interval. Since it does not expect to receive any response, it does not use the buffer and it is enough for the operation to complete correctly. This system is what I use in my sleep management device project to store the results of the sensor readings on the server.

    The following example uses an HTTP GET query to query the server's time and synchronize it with the microcontrolled device's time by adding the estimated response time; In fact, it adds another 4 to 6 seconds to ensure that the device's time is greater than the server's time and to easily check if the time on the real-time clock that is part of the device is correct or has been lost due to downloading. battery.

    After many tests I have verified that this system of Time synchronization with ESP8266 WiFi module It is accurate enough for my needs; in the worst case with an error of less than 10 seconds which is somewhat irrelevant in my case.

    As can be seen in the code, it is necessary to first query the length of the buffer since reading resets it so that it is available to store new data.

    Once the content of the buffer has been read, in this example it is processed to obtain the time. First, the text between the braces is selected (the server responds using the command PHP echo “{“.time().”}”) transforms it into a “time object” and obtains the date and time from it in human format.

    The following example program, inspired by a user's query, is used to find the public IP address using the service CheckIP AWS (Amazon Web Services) every five minutes.

    As in the previous examples, a series of assumptions are made that will have to be changed depending on the network configuration used (192.168.1.X, in the example), the SSID and the WiFi key... The way to find the IP address is not very elegant, it assumes that it is the last line of the response to the HTTP GET request although there may be an API to do it in a more orthodox way.


    Here you can download the ESP8266 library for HTTP queries with Arduino Uno (without console) and from here you can download the ESP8266 HTTP query library for Arduino that uses a console, that is, it needs to implement a serial port by software or use as hardware, for example, a motherboard Arduino Mega o Arduino Leonardo.

    Post Comment

    You May Have Missed