echo '' ;

ESP8266 NodeMCU Module – HTTP


This tutorial “ESP8266 NodeMCU HTTP Module” will discuss. The ESP8266 NodeMCU module is a popular microcontroller board based on the ESP8266 Wi-Fi chip. It’s widely used for IoT (Internet of Things) projects due to its low cost, built-in Wi-Fi capabilities, and ease of programming. One common use case of the ESP8266 NodeMCU is handling HTTP requests and responses, allowing it to communicate with web servers, APIs, and other devices over the internet.

Read more: ESP8266 NodeMCU Module – HTTP

HTTP (Hypertext Transfer Protocol) is the foundation of data communication on the World Wide Web. With the ESP8266 NodeMCU, you can leverage HTTP to send and receive data, control devices remotely, and interact with cloud services.

In this example, we’ll explore how to configure the ESP8266 NodeMCU to connect to a Wi-Fi network, monitor Wi-Fi events, and perform HTTP operations such as sending POST requests to a web server. Additionally, we’ll set up a GPIO pin to trigger an action when its state changes, showcasing how the ESP8266 NodeMCU can interact with external devices.

ESP8266 NodeMCU HTTP Module functions

http.delete()Executes a HTTP DELETE request.
http.get()Executes a HTTP GET request.
http.post()Executes a HTTP POST request.
http.put()Executes a HTTP PUT request.
http.request()Execute a custom HTTP request for any HTTP method.

http.delete()

  • Executes a HTTP DELETE request. Note that concurrent requests are not supported.
  • Syntax : http.delete(url, headers, body, callback
  • Parameters
    • url The URL to fetch, including the http:// or https:// prefix
    • headers Optional additional headers to append, including \r\n; may be nil
    • body The body to post; must already be encoded in the appropriate format, but may be empty
    • callback The callback function to be invoked when the response has been received or an error occurred; it is invoked with the arguments status_code, body and headers. In case of an error status_code is set to -1.
  • Returns : nil

HTTP Get Example

  • Read your thing-speak text file from the below code for ESP8266 NodeMCU Module HTTP.

Code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
station_cfg={}
station_cfg.ssid="ArunEworld" -- Enter SSID here
station_cfg.pwd="8300026060INDIA" --Enter password here
server_link = "http://iot.aruneworld.com/httt-get.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
--Wifi Eent Monitoring file
wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T)
print("\n\tSTA - CONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: "..
T.BSSID.."\n\tChannel: "..T.channel)
end)
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
print("\n\tSTA - DISCONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: "..
T.BSSID.."\n\treason: "..T.reason)
tmr.stop(0)
end)
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
print("\n\tSTA - GOT IP".."\n\tStation IP: "..T.IP.."\n\tSubnet mask: "..
T.netmask.."\n\tGateway IP: "..T.gateway)
tmr.start(0)
end)
function GetFromArunEworld()-- 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 5 second
tmr.alarm(1, 5000, 1, function() GetFromArunEworld() end)
station_cfg={} station_cfg.ssid="ArunEworld" -- Enter SSID here station_cfg.pwd="8300026060INDIA" --Enter password here server_link = "http://iot.aruneworld.com/httt-get.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 --Wifi Eent Monitoring file wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T) print("\n\tSTA - CONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: ".. T.BSSID.."\n\tChannel: "..T.channel) end) wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) print("\n\tSTA - DISCONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: ".. T.BSSID.."\n\treason: "..T.reason) tmr.stop(0) end) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T) print("\n\tSTA - GOT IP".."\n\tStation IP: "..T.IP.."\n\tSubnet mask: ".. T.netmask.."\n\tGateway IP: "..T.gateway) tmr.start(0) end) function GetFromArunEworld()-- 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 5 second tmr.alarm(1, 5000, 1, function() GetFromArunEworld() end)
station_cfg={}
station_cfg.ssid="ArunEworld"     -- Enter SSID here
station_cfg.pwd="8300026060INDIA"  --Enter password here
server_link = "http://iot.aruneworld.com/httt-get.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

--Wifi Eent Monitoring file
wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T)
 print("\n\tSTA - CONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: "..
 T.BSSID.."\n\tChannel: "..T.channel)
 
 end)

 wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
 print("\n\tSTA - DISCONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: "..
 T.BSSID.."\n\treason: "..T.reason)
	tmr.stop(0)
 end)

 wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
 print("\n\tSTA - GOT IP".."\n\tStation IP: "..T.IP.."\n\tSubnet mask: "..
 T.netmask.."\n\tGateway IP: "..T.gateway)
 tmr.start(0)
 end)

 
function GetFromArunEworld()-- 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 5 second
tmr.alarm(1, 5000, 1, function() GetFromArunEworld() end)

Code Explanation

This code sets up a NodeMCU or similar device to connect to a Wi-Fi network, monitor Wi-Fi events, and periodically fetch data from a server over HTTP. It’s a good example of IoT device interaction with both Wi-Fi and web servers.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
station_cfg={}
station_cfg.ssid="ArunEworld" -- Enter SSID here
station_cfg.pwd="8300026060INDIA" -- Enter password here
station_cfg={} station_cfg.ssid="ArunEworld" -- Enter SSID here station_cfg.pwd="8300026060INDIA" -- Enter password here
station_cfg={}
station_cfg.ssid="ArunEworld"     -- Enter SSID here
station_cfg.pwd="8300026060INDIA"  -- Enter password here
  • Here, a Lua table named station_cfg is defined, which contains the SSID and password for connecting to the Wi-Fi network.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
server_link = "http://iot.aruneworld.com/httt-get.txt" -- set server URL
server_link = "http://iot.aruneworld.com/httt-get.txt" -- set server URL
server_link = "http://iot.aruneworld.com/httt-get.txt" -- set server URL
  • This line sets the URL of the server from which data will be fetched. It seems to be a text file containing data.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
wifi.setmode(wifi.STATION) -- set Wi-Fi mode to station
wifi.sta.config(station_cfg)-- set SSID and password to config
wifi.sta.connect() -- connect to router
wifi.setmode(wifi.STATION) -- set Wi-Fi mode to station wifi.sta.config(station_cfg)-- set SSID and password to config wifi.sta.connect() -- connect to router
wifi.setmode(wifi.STATION)  -- set Wi-Fi mode to station
wifi.sta.config(station_cfg)-- set SSID and password to config
wifi.sta.connect()          -- connect to router
  • These lines configure the Wi-Fi module to operate in station mode, set the Wi-Fi station configuration to the values provided in station_cfg, and then initiate a connection to the specified router.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
-- Wi-Fi Event Monitoring
wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T)
print("\n\tSTA - CONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: "..
T.BSSID.."\n\tChannel: "..T.channel)
end)
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
print("\n\tSTA - DISCONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: "..
T.BSSID.."\n\treason: "..T.reason)
tmr.stop(0) -- Stop timer upon disconnection
end)
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
print("\n\tSTA - GOT IP".."\n\tStation IP: "..T.IP.."\n\tSubnet mask: "..
T.netmask.."\n\tGateway IP: "..T.gateway)
tmr.start(0) -- Start timer upon obtaining IP address
end)
-- Wi-Fi Event Monitoring wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T) print("\n\tSTA - CONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: ".. T.BSSID.."\n\tChannel: "..T.channel) end) wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) print("\n\tSTA - DISCONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: ".. T.BSSID.."\n\treason: "..T.reason) tmr.stop(0) -- Stop timer upon disconnection end) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T) print("\n\tSTA - GOT IP".."\n\tStation IP: "..T.IP.."\n\tSubnet mask: ".. T.netmask.."\n\tGateway IP: "..T.gateway) tmr.start(0) -- Start timer upon obtaining IP address end)
-- Wi-Fi Event Monitoring
wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T)
    print("\n\tSTA - CONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: "..
    T.BSSID.."\n\tChannel: "..T.channel)
end)

wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
    print("\n\tSTA - DISCONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: "..
    T.BSSID.."\n\treason: "..T.reason)
    tmr.stop(0) -- Stop timer upon disconnection
end)

wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
    print("\n\tSTA - GOT IP".."\n\tStation IP: "..T.IP.."\n\tSubnet mask: "..
    T.netmask.."\n\tGateway IP: "..T.gateway)
    tmr.start(0) -- Start timer upon obtaining IP address
end)
  • These functions register event handlers for various Wi-Fi events like connection, disconnection, and obtaining an IP address. When any of these events occur, the code prints a message containing relevant information. Additionally, it starts or stops the timer depending on the event.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function GetFromArunEworld()-- callback function for fetching data
http.get(server_link,'',
function(code, data)
if (code < 0) then
print("HTTP request failed")
else
print(code, data)
end
end)
end
function GetFromArunEworld()-- callback function for fetching data http.get(server_link,'', function(code, data) if (code < 0) then print("HTTP request failed") else print(code, data) end end) end
function GetFromArunEworld()-- callback function for fetching data
    http.get(server_link,'',
    function(code, data)
        if (code < 0) then
            print("HTTP request failed")
        else
            print(code, data)
        end
    end)
end
  • This function GetFromArunEworld() is defined as a callback function to fetch data from the specified server. It makes an HTTP GET request to server_link and prints the response code and data.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
-- call the fetch function every 5 seconds
tmr.alarm(1, 5000, 1, function() GetFromArunEworld() end)
-- call the fetch function every 5 seconds tmr.alarm(1, 5000, 1, function() GetFromArunEworld() end)
-- call the fetch function every 5 seconds
tmr.alarm(1, 5000, 1, function() GetFromArunEworld() end)
  • Finally, a timer is set up to call the GetFromArunEworld() function every 5 seconds to fetch data from the server.

HTTP Post Example

Post Data to thinkspeak website

Code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
local 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 successful post
end
end)
end
-- call post function after each 20 seconds for ThingSpeak server update
tmr.alarm(1, 20000, tmr.ALARM_AUTO, function() PostToThingSpeak() end)
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 local 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 successful post end end) end -- call post function after each 20 seconds for ThingSpeak server update tmr.alarm(1, 20000, tmr.ALARM_AUTO, function() PostToThingSpeak() end)
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
    local 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 successful post
        end
    end)
end

-- call post function after each 20 seconds for ThingSpeak server update
tmr.alarm(1, 20000, tmr.ALARM_AUTO, function() PostToThingSpeak() end)

Code Explanation

This script connects to the specified Wi-Fi network, sends data to a ThingSpeak channel at regular intervals, and increments the count each time data is successfully posted.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
station_cfg={}
station_cfg.ssid="ssid" -- Enter SSID here
station_cfg.pwd="password" -- Enter password here
station_cfg={} station_cfg.ssid="ssid" -- Enter SSID here station_cfg.pwd="password" -- Enter password here
station_cfg={}
station_cfg.ssid="ssid" -- Enter SSID here
station_cfg.pwd="password" -- Enter password here
  • These lines define a Lua table station_cfg with keys ssid and pwd, representing the SSID and password of the Wi-Fi network you want to connect to.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
server = "http://api.thingspeak.com/update" -- set server URL
server = "http://api.thingspeak.com/update" -- set server URL
server = "http://api.thingspeak.com/update" -- set server URL
  • This line sets the URL of the ThingSpeak server where you’ll send the data.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
count = 0 -- set initial count to 0
count = 0 -- set initial count to 0
count = 0 -- set initial count to 0
  • Initializes a variable count to keep track of the number of data posts.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
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
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
  • These lines configure the Wi-Fi module to work in station mode, set the Wi-Fi station configuration, and connect to the router using the provided SSID and password.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function PostToThingSpeak()
-- callback function for posting data
local 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 successful post
end
end)
end
function PostToThingSpeak() -- callback function for posting data local 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 successful post end end) end
function PostToThingSpeak()
    -- callback function for posting data
    local 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 successful post
        end
    end)
end
  • Defines a function PostToThingSpeak() to send data to ThingSpeak.
  • Constructs a string containing the API key and the current value of the count variable.
  • Sends an HTTP POST request to the ThingSpeak server with the constructed string.
  • Prints the response code and data received.
  • Increments the count after each successful post.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
-- call post function every 20 seconds for ThingSpeak server update
tmr.alarm(1, 20000, tmr.ALARM_AUTO, function() PostToThingSpeak() end)
-- call post function every 20 seconds for ThingSpeak server update tmr.alarm(1, 20000, tmr.ALARM_AUTO, function() PostToThingSpeak() end)
-- call post function every 20 seconds for ThingSpeak server update
tmr.alarm(1, 20000, tmr.ALARM_AUTO, function() PostToThingSpeak() end)
  • Sets up a timer to call the PostToThingSpeak() function every 20 seconds automatically.

Post Data to Aruneworld website

code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
station_cfg = {}
station_cfg.ssid = "ArunEworld"
station_cfg.pwd = "8300026060BLR"
wifi.sta.config(station_cfg)
wifi.sta.connect(1)
wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T)
print("\n\tSTA - CONNECTED" ..
"\n\tSSID: " .. T.SSID ..
"\n\tBSSID: " .. T.BSSID ..
"\n\tChannel: " .. T.channel)
end)
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
print("\n\tSTA - DISCONNECTED" ..
"\n\tSSID: " .. T.SSID ..
"\n\tBSSID: " .. T.BSSID ..
"\n\treason: " .. T.reason)
end)
wifi.eventmon.register(wifi.eventmon.STA_AUTHMODE_CHANGE, function(T)
print("\n\tSTA - AUTHMODE CHANGE" ..
"\n\told_auth_mode: " .. T.old_auth_mode ..
"\n\tnew_auth_mode: " .. T.new_auth_mode)
end)
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
print("\n\tSTA - GOT IP" ..
"\n\tStation IP: " .. T.IP ..
"\n\tSubnet mask: " .. T.netmask ..
"\n\tGateway IP: " .. T.gateway)
end)
wifi.eventmon.register(wifi.eventmon.STA_DHCP_TIMEOUT, function()
print("\n\tSTA - DHCP TIMEOUT")
end)
wifi.eventmon.register(wifi.eventmon.AP_STACONNECTED, function(T)
print("\n\tAP - STATION CONNECTED" ..
"\n\tMAC: " .. T.MAC ..
"\n\tAID: " .. T.AID)
end)
wifi.eventmon.register(wifi.eventmon.AP_STADISCONNECTED, function(T)
print("\n\tAP - STATION DISCONNECTED" ..
"\n\tMAC: " .. T.MAC ..
"\n\tAID: " .. T.AID)
end)
wifi.eventmon.register(wifi.eventmon.AP_PROBEREQRECVED, function(T)
print("\n\tAP - PROBE REQUEST RECEIVED" ..
"\n\tMAC: " .. T.MAC ..
"\n\tRSSI: " .. T.RSSI)
end)
wifi.eventmon.register(wifi.eventmon.WIFI_MODE_CHANGED, function(T)
print("\n\tSTA - WIFI MODE CHANGED" ..
"\n\told_mode: " .. T.old_mode ..
"\n\tnew_mode: " .. T.new_mode)
end)
local function D_Send()
tmr.wdclr()
http.post('https://aruneworld.com.com/post', 'Content-Type: text/plain\r\n', 'Hum=23&temp=24', function(code, data)
if (code < 0) then
print("HTTP request failed")
else
print(code, data)
end
end)
end
local function pin1cb(level)
D_Send()
end
gpio.trig(3, "down", pin1cb) -- GPIO 0 pin
station_cfg = {} station_cfg.ssid = "ArunEworld" station_cfg.pwd = "8300026060BLR" wifi.sta.config(station_cfg) wifi.sta.connect(1) wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T) print("\n\tSTA - CONNECTED" .. "\n\tSSID: " .. T.SSID .. "\n\tBSSID: " .. T.BSSID .. "\n\tChannel: " .. T.channel) end) wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) print("\n\tSTA - DISCONNECTED" .. "\n\tSSID: " .. T.SSID .. "\n\tBSSID: " .. T.BSSID .. "\n\treason: " .. T.reason) end) wifi.eventmon.register(wifi.eventmon.STA_AUTHMODE_CHANGE, function(T) print("\n\tSTA - AUTHMODE CHANGE" .. "\n\told_auth_mode: " .. T.old_auth_mode .. "\n\tnew_auth_mode: " .. T.new_auth_mode) end) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T) print("\n\tSTA - GOT IP" .. "\n\tStation IP: " .. T.IP .. "\n\tSubnet mask: " .. T.netmask .. "\n\tGateway IP: " .. T.gateway) end) wifi.eventmon.register(wifi.eventmon.STA_DHCP_TIMEOUT, function() print("\n\tSTA - DHCP TIMEOUT") end) wifi.eventmon.register(wifi.eventmon.AP_STACONNECTED, function(T) print("\n\tAP - STATION CONNECTED" .. "\n\tMAC: " .. T.MAC .. "\n\tAID: " .. T.AID) end) wifi.eventmon.register(wifi.eventmon.AP_STADISCONNECTED, function(T) print("\n\tAP - STATION DISCONNECTED" .. "\n\tMAC: " .. T.MAC .. "\n\tAID: " .. T.AID) end) wifi.eventmon.register(wifi.eventmon.AP_PROBEREQRECVED, function(T) print("\n\tAP - PROBE REQUEST RECEIVED" .. "\n\tMAC: " .. T.MAC .. "\n\tRSSI: " .. T.RSSI) end) wifi.eventmon.register(wifi.eventmon.WIFI_MODE_CHANGED, function(T) print("\n\tSTA - WIFI MODE CHANGED" .. "\n\told_mode: " .. T.old_mode .. "\n\tnew_mode: " .. T.new_mode) end) local function D_Send() tmr.wdclr() http.post('https://aruneworld.com.com/post', 'Content-Type: text/plain\r\n', 'Hum=23&temp=24', function(code, data) if (code < 0) then print("HTTP request failed") else print(code, data) end end) end local function pin1cb(level) D_Send() end gpio.trig(3, "down", pin1cb) -- GPIO 0 pin
station_cfg = {}
station_cfg.ssid = "ArunEworld"
station_cfg.pwd = "8300026060BLR"
wifi.sta.config(station_cfg)
wifi.sta.connect(1)

wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T)
    print("\n\tSTA - CONNECTED" ..
          "\n\tSSID: " .. T.SSID ..
          "\n\tBSSID: " .. T.BSSID ..
          "\n\tChannel: " .. T.channel)
end)

wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
    print("\n\tSTA - DISCONNECTED" ..
          "\n\tSSID: " .. T.SSID ..
          "\n\tBSSID: " .. T.BSSID ..
          "\n\treason: " .. T.reason)
end)

wifi.eventmon.register(wifi.eventmon.STA_AUTHMODE_CHANGE, function(T)
    print("\n\tSTA - AUTHMODE CHANGE" ..
          "\n\told_auth_mode: " .. T.old_auth_mode ..
          "\n\tnew_auth_mode: " .. T.new_auth_mode)
end)

wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
    print("\n\tSTA - GOT IP" ..
          "\n\tStation IP: " .. T.IP ..
          "\n\tSubnet mask: " .. T.netmask ..
          "\n\tGateway IP: " .. T.gateway)
end)

wifi.eventmon.register(wifi.eventmon.STA_DHCP_TIMEOUT, function()
    print("\n\tSTA - DHCP TIMEOUT")
end)

wifi.eventmon.register(wifi.eventmon.AP_STACONNECTED, function(T)
    print("\n\tAP - STATION CONNECTED" ..
          "\n\tMAC: " .. T.MAC ..
          "\n\tAID: " .. T.AID)
end)

wifi.eventmon.register(wifi.eventmon.AP_STADISCONNECTED, function(T)
    print("\n\tAP - STATION DISCONNECTED" ..
          "\n\tMAC: " .. T.MAC ..
          "\n\tAID: " .. T.AID)
end)

wifi.eventmon.register(wifi.eventmon.AP_PROBEREQRECVED, function(T)
    print("\n\tAP - PROBE REQUEST RECEIVED" ..
          "\n\tMAC: " .. T.MAC ..
          "\n\tRSSI: " .. T.RSSI)
end)

wifi.eventmon.register(wifi.eventmon.WIFI_MODE_CHANGED, function(T)
    print("\n\tSTA - WIFI MODE CHANGED" ..
          "\n\told_mode: " .. T.old_mode ..
          "\n\tnew_mode: " .. T.new_mode)
end)

local function D_Send()
    tmr.wdclr()
    http.post('https://aruneworld.com.com/post', 'Content-Type: text/plain\r\n', 'Hum=23&temp=24', function(code, data)
        if (code < 0) then
            print("HTTP request failed")
        else
            print(code, data)
        end
    end)
end

local function pin1cb(level)
    D_Send()
end

gpio.trig(3, "down", pin1cb) -- GPIO 0 pin

Code Explanation

This code sets up a NodeMCU or similar device to connect to a Wi-Fi network, monitor various Wi-Fi events, and perform an action when a specific GPIO pin state changes. Let’s break it down:

Overall, this script configures the Wi-Fi connection, sets up event handlers for various Wi-Fi events, defines a function to send data via HTTP POST, and sets up a GPIO pin to trigger an action when its state changes.

  1. Wi-Fi Configuration and Connection Setup:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
station_cfg={}
station_cfg.ssid= "ArunEworld"
station_cfg.pwd= "8300026060BLR"
wifi.sta.config(station_cfg)
wifi.sta.connect(1)
station_cfg={} station_cfg.ssid= "ArunEworld" station_cfg.pwd= "8300026060BLR" wifi.sta.config(station_cfg) wifi.sta.connect(1)
   station_cfg={}
   station_cfg.ssid= "ArunEworld"
   station_cfg.pwd= "8300026060BLR"
   wifi.sta.config(station_cfg)
   wifi.sta.connect(1)
  • This section sets up the Wi-Fi station configuration with the SSID and password provided in station_cfg and then connects to the Wi-Fi network.
  1. Wi-Fi Event Monitoring:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T)
print("\n\tSTA - CONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: ".. T.BSSID.."\n\tChannel: "..T.channel)
end)
wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T) print("\n\tSTA - CONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: ".. T.BSSID.."\n\tChannel: "..T.channel) end)
   wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T)
       print("\n\tSTA - CONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: ".. T.BSSID.."\n\tChannel: "..T.channel)
   end)
  • It registers a callback function to handle the event when the device connects to a Wi-Fi network.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
print("\n\tSTA - DISCONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: ".. T.BSSID.."\n\treason: "..T.reason)
end)
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) print("\n\tSTA - DISCONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: ".. T.BSSID.."\n\treason: "..T.reason) end)
   wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
       print("\n\tSTA - DISCONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: ".. T.BSSID.."\n\treason: "..T.reason)
   end)
  • It registers a callback function to handle the event when the device disconnects from the Wi-Fi network. (Similar registration for other Wi-Fi events like authentication mode change, obtaining IP address, DHCP timeout, station/AP connection/disconnection, etc.)
  1. HTTP Post Functionality:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
local function D_Send()
tmr.wdclr()
http.post('https://aruneworld.com.com/post', 'Content-Type: text/plain\r\n', 'Hum=23&temp=24', function(code, data)
if (code < 0) then
print("HTTP request failed")
else
print(code, data)
end
end)
end
local function D_Send() tmr.wdclr() http.post('https://aruneworld.com.com/post', 'Content-Type: text/plain\r\n', 'Hum=23&temp=24', function(code, data) if (code < 0) then print("HTTP request failed") else print(code, data) end end) end
   local function D_Send()
       tmr.wdclr()
       http.post('https://aruneworld.com.com/post', 'Content-Type: text/plain\r\n', 'Hum=23&temp=24', function(code, data)
           if (code < 0) then
               print("HTTP request failed")
           else
               print(code, data)
           end
       end)
   end
  • Defines a function D_Send() to perform an HTTP POST request to a server when called. In this case, it posts humidity and temperature data.
  1. GPIO Pin Interrupt Setup:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
local function pin1cb(level)
D_Send()
end
gpio.trig(3,"down", pin1cb) --gpio-0 pin
local function pin1cb(level) D_Send() end gpio.trig(3,"down", pin1cb) --gpio-0 pin
   local function pin1cb(level)
       D_Send()
   end
   gpio.trig(3,"down", pin1cb) --gpio-0 pin
  • Defines a callback function pin1cb to be triggered when the GPIO pin 3 (GPIO 0) goes from high to low.
  • Registers this callback function with gpio.trig() to handle the GPIO interrupt.

NEXT

NodeMCU Get Start
NodeMCU Build Firmware
NodeMCU Flash Firmware
NodeMCU IDE
ESP8266 NodeMCU Modules
NodeMCU Module–Firmware Info
NodeMCU Module – GPIO
NodeMCU Module – Node
NodeMCU Module – WiFi
NodeMCU Module – Timer
NodeMCU Module – I2C
NodeMCU Module – File
NodeMCU Module – NET
NodeMCU Module – HTTP
NodeMCU Module – MQTT
ESP8266 NodeMCU Interface
NodeMCU Interface LED
NodeMCU Interface Button
NodeMCU Interface 7 Seg
NodeMCU Interface LCD
NodeMCU Interface ADC
NodeMCU Interface DHT11
NodeMCU Interface MCP23008
NodeMCU Interface MCP23017
NodeMCU Interface ADXL345
NodeMCU Interface DS18B20
ESP8266 NodeMCU Tutorials
NodeMCU Tutorials Google Time
NodeMCU Tutorials WebServer
ESP8266 NodeMCU Projects
Imperial March Ringtone Play
WiFi Router Status Indicator
ESP8266 NodeMCU Project – Home Automation
Others
NodeMCU All Post
Sitemap

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading