esp8266 nodemcu lua wget external file via http

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    esp8266 nodemcu lua wget external file via http

    Here is a simple wget function for esp8266 nodemcu lua, which gets a remote file and saves it locally. Your esp8266 has to have the http and file module included in its firmware.
    https://nodemcu.readthedocs.io/en/ma.../modules/http/
    https://nodemcu.readthedocs.io/en/master/en/modules/file/


    here is the function:
    Code:
    function wget(url,file_name)
    
        http.get(url, nil, function(code, data)
            if (code < 0) then
                print("HTTP request failed")
            else
                -- print(code, data)
                -- open file in 'w' mode
                if file.open(file_name, "w") then
                  -- write text to the file
                  file.write(data)
                  file.close()
    
    
                end
            end
          end)
    
    end
    usage:
    Code:
    wget("http://example.com/file.txt", "local_file_name.lua")
    the usage example shows how to get contents of external file.txt and save the contents to the local_file_name.lua
    Advertise your mobile site for FREE with AdTwirl

Working...
X