http - NodeMCU, a simple GET request is quite the challenge -



have project going on using 2 nodemcus , lua. 1 nodemcu (server) hosting simple web server reacts specific url parameters. example, go to: http://192.168.1.179/?pin=on1 in browser , it's leds light etc. want recreate doing other nodemcu (client) @ push of button. part holding me http request.

for starters, i'd use http module .. module isn't present in latest firmware build available. don't know why is? i'm not build latest - latest. 2 version difference there... have submitted 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 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 work. dns fails full path. is, outputs 0 print portion. mind you, local server , it's address directly specified. i'm getting dhcp router , i've tried static ip. i'm @ loss , don't know else do. it's last step in completing project driving me nuts! appreciated. should note lost script server cannot reference it. works though. x_x'

as per https://github.com/nodemcu/nodemcu-firmware#releases can't download recent binaries anymore (has been >2y). easiest go https://nodemcu-build.com/ , have binary built you. make sure select http module.

however, please note http module http client!

as making simple requests http module suggest take @ 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)       print("http request failed")     else       print(code, data)     end   end) 

this yields 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 server need net module , net.createserver(net.tcp). have examples @ https://nodemcu.readthedocs.io/en/latest/en/modules/net/#example_6.

i advise though not build own server use https://github.com/marcoskirsch/nodemcu-httpserver/ instead beyond trivial projects.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -