2


I have a project going on using two NodeMCUs and LUA. One NodeMCU (server) is hosting a simple web server that reacts to specific URL parameters. For example, I go to: http://192.168.1.179/?pin=ON1 in my browser and it's LEDs light up etc. I want to recreate doing this with the other NodeMCU (client) at the push of a button. The only part that is holding me up is this HTTP GET request.

For starters, I'd like to simply use the http module .. but the module isn't present in the latest firmware build available. I don't know why this is? I'm not up to build the latest - latest. 2 version difference there... I have submitted for a custom build.

require('http')
stdin:1: module 'http' not found:
no field package.preload['http']
no file 'http.lc'
no file 'http.lua'

Apparently there are other, older, means of doing so:

    conn=net.createConnection(net.TCP, false) 
    conn:on("receive", function(conn, pl) print(pl) end)
    conn:connect(80,"192.168.1.179")
    conn:send("GET /?pin=ON1 HTTP/1.1\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")

I've been playing around trying to get it to work. The DNS fails with the full path. As is, it just outputs 0 for the print portion. Mind you, this is a local server and it's address is directly specified. I'm getting a DHCP from the router and I've tried a static IP. I'm at a loss and don't know what else to do. It's the last step in completing this project as well so it is driving me nuts! Any help would be appreciated. I should note I lost my script for the server so I cannot reference it. It works though. x_X'

datguy.dev
  • 31
  • 1
  • 5

2 Answers2

1

As per https://github.com/nodemcu/nodemcu-firmware#releases you can't download recent binaries anymore (has been like that for >2y). The easiest is to go to https://nodemcu-build.com/ and have a binary built for you. Make sure you select the HTTP module.

However, please note that the HTTP module is an HTTP client!

As for making simple GET requests with this HTTP module I suggest take a look at https://nodemcu.readthedocs.io/en/latest/en/modules/http/#httpget:

http.get("http://httpbin.org/get?paul=muller", nil, function(code, data)
    if (code < 0) then
      print("HTTP request failed")
    else
      print(code, data)
    end
  end)

This yields something like this:

200 {
  "args": {
    "paul": "muller"
  }, 
  "headers": {
    "Connection": "close", 
    "Host": "httpbin.org", 
    "User-Agent": "ESP8266"
  }, 
  "origin": "xxx.xxx.xxx.xxx", 
  "url": "http://httpbin.org/get?paul=muller"
}

To build a server you need the net module and net.createServer(net.TCP). We have some examples at https://nodemcu.readthedocs.io/en/latest/en/modules/net/#example_6.

I advise though to NOT build your own server but use https://github.com/marcoskirsch/nodemcu-httpserver/ instead for anything beyond trivial projects.

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
  • Many of those aspects were addressed in the original post. Just back story. I just need to make a GET request with URL PARAMETERS. I've received my build with http. Now I'm getting:HTTP client: DNS failed for httpbin.org code: -1 data: nil HTTP request failed. I cannot ping the client NodeMCU, like I can the server NodeMCU. – datguy.dev Jul 28 '17 at 15:45
  • Thanks for updating your answer. I did try that simple method early on as it is simple. Just for question information, I was getting a -1 error with LUA options for the request. The request would fail and I haven't a clue about the error at the time. In Arduino, I also get -1 error, but the request is successful. Perhaps it was a poor server implementation or the server wasn't responding with the http status code before timeout. – datguy.dev Jul 29 '17 at 11:16
-1

The system is working fine using the Arduino ESP8266WiFi & ESP8266HTTPClient libraries. I'm going to mark this solved simply because I refuse to deal with the LUA http options anymore.

datguy.dev
  • 31
  • 1
  • 5