HTTP Client on NodeMCU with ESPlorer IDE

Introduction

HTTP (Hypertext Transfer Protocol) is a standard Application protocol that functions as a request-response protocol between client and server.

HTTP client helps to send HTTP requests and receiving HTTP responses from the HTTP server.

It is widely used in IoT based embedded applications like Home Automation, vehicle engine parameter monitoring remotely for analysis, etc.

NodeMCU is an open-source IoT platform. It is a firmware which runs on ESP8266 Wi-Fi SoC from Espressif Systems. It has onboard wi-fi available through which IoT applications become easy to build.

NodeMCU HTTP Client

The basic HTTP module for NodeMCU will provide GET/POST/PUT/DELETE requests over HTTP. Let’s see the functions that are used to call HTTP requests.

NodeMCU HTTP functions

http.get()

This function executes a HTTP GET request.

Syntax: http.get(url, headers, callback)

Parameters:

url: The URL to fetch, including the http:// or https:// prefix

headers: This is the optional additional headers to append, including \r\n; or it can be nil.

callback: The callback function is called when the response has been received. it has the input arguments as status_code (HTTP status code), body, and headers.

Returns: null

An example would be helpful to visualize how the function is used. Cannot get a clear idea of exactly what is given as an argument for headers.

 

http.post()

This function executes an HTTP POST request.

Syntax: http.post(url, headers, body, callback)

Parameters:

url: The URL for POST, including the http:// or https:// prefix

headers: This is the optional additional headers to append, including \r\n; or it can be nil.

e.g. 'Content-Type: application/json\r\n' or 'Content-Type: text/plain\r\n'

body: This is the body (application data) to post.It must be within appropriate format (like json or text), or it may be empty.

callback: The callback function is called when the response has been received. it has the input arguments as status_code (HTTP status code), body, and headers.

Returns: null

An example would be helpful to visualize how the function is used.

 

http.put()

This function executes an HTTP PUT request.

Syntax: http.put(url, headers, body, callback)

Parameters:

url: The URL to fetch, including the http:// or https:// prefix

headers: This is the optional additional headers to append, including \r\n; or it can be nil.

e.g. 'Content-Type: application/json\r\n' or 'Content-Type: text/plain\r\n'

body: This is the body (application data) to post.It must be within appropriate format (like json or text), or it may be empty.

callback: The callback function is called when the response has been received. it has the input arguments as status_code (HTTP status code), body, and headers.

Returns: null

An example would be helpful to visualize how the function is used.

 

http.delete()

This function executes an HTTP DELETE request.

Syntax: http.delete(url, headers, body, callback)

Parameters:

url: The URL to fetch, including the http:// or https:// prefix

headersThis is the optional additional headers to append, including \r\n; or it can be nil.

body: This is the body (application data) to post. It must be within an appropriate format (like JSON or text), or it may be empty.

callback: The callback function is called when the response has been received. it has the input arguments as status_code (HTTP status code), body, and headers.

Returns: null

An example would be helpful to visualize how the function is used.

 

Example

Let’s write a Lua script for NodeMCU as an HTTP Client and GET/POST the data from/to the Thingspeak server.

Here, we are using the Thingspeak server for HTTP Client demo purposes.

Thingspeak is an open IOT platform where anyone can visualize and analyze live data from their sensor devices. Also, we can perform data analysis on data posted by remote devices with Matlab code in Thingspeak. To learn more about Thingspeak refer link https://thingspeak.com/pages/learn_more

Just sign up and create a channel. We have below the channel and write key on Thingspeak for data send and receive.

  • Channel ID is = 309236
  • Write Key is = 1EYZIS5OCRJSKZHG

Note:  Do not forget to tick the Make Public field in the channel setting option on your Thingspeak channel.

For this example, we are using some NodeMCU Wi-Fi functions to connect with the available wifi access point. Refer to NodeMCU Wi-Fi with ESPlorer IDE to know the wifi functions of NodeMCU.

Lua Script for HTTP Client Get

station_cfg={}
station_cfg.ssid="ssid"     -- Enter SSID here
station_cfg.pwd="password"  --Enter password here
server_link = "http://api.thingspeak.com/channels/309236/feeds/last.txt" -- set server URL

wifi.setmode(wifi.STATION)  -- set wi-fi mode to station
wifi.sta.config(station_cfg)-- set ssid&pwd to config
wifi.sta.connect()          -- connect to router

function GetFromThingSpeak()-- callback function for get data
http.get(server_link,'',
function(code, data)
    if (code < 0) then
     print("HTTP request failed")
    else
     print(code, data)
    end
  end)
end
-- call get function after each 10 second
tmr.alarm(1, 10000, 1, function() GetFromThingSpeak() end)

Output Window for HTTP Get

Below is the output window of the ESPlorer serial monitor which shows the last updated data received from Thingspeak server.

thingspeak http get response

 

Lua Script for HTTP Client Post

station_cfg={}
station_cfg.ssid="ssid"     -- Enter SSID here
station_cfg.pwd="password"  --Enter password here
server = "http://api.thingspeak.com/update" -- set server URL
count = 0   -- set initial count to 0


wifi.setmode(wifi.STATION)  -- set wi-fi mode to station
wifi.sta.config(station_cfg)-- set ssid&pwd to config
wifi.sta.connect()          -- connect to router

function PostToThingSpeak() -- callback function for post data
string = "api_key=1EYZIS5OCRJSKZHG&field1="..count
http.post(server,'', string,
function(code, data)
    if (code < 0) then
      print("HTTP request failed")
    else
      print(code, data)
      count = count+1       -- increment count after each successfull post
    end
  end)
end
-- call post function after each 20 second for thingspeak server update
tmr.alarm(1, 20000, 1, function() PostToThingSpeak() end)

Output Window for HTTP Post

Below is the output window of counts at Thingspeak server.

thingspeak http post count

 


Components Used

ESP12F
ESP12E
1
NodeMCU
NodeMCUNodeMCU
1

Downloads

NodeMCU HTTP Client Lua scripts Download
Ad