As the title says... Chat on...

User avatar
By ac21
#11235 I'm trying to use the code below by dannyvai https://github.com/dannyvai/esp2866_too ... onnect_lua


Code: Select allwifi.setmode(wifi.STATIONAP)
cfg={}
cfg.ssid="myhotspot"
cfg.pwd="myhotspot"
wifi.ap.config(cfg)
print(wifi.ap.getip())
file.remove("connect.lua")
file.open("connect.lua","w")
file.writeline([[print("starting server")]])
file.writeline([[print(wifi.ap.getip())]])
file.writeline([[ssid = ""]])
file.writeline([[pwd = ""]])
file.writeline([[srv=net.createServer(net.TCP) srv:listen(80,function(conn)]])
file.writeline([[conn:on("receive",function(conn,payload)]])
file.writeline([[print(payload)]])
file.writeline([[if string.find(payload,"favicon.ico") == nil then]])
file.writeline([[if (string.find(payload,"ssid") ~= nil) and (string.find(payload,"pwd") ~= nil) then]])
file.writeline([[payload_len = string.len(payload)]])
file.writeline([[ssid_idx = string.find(payload,"ssid")]])
file.writeline([[pwd_idx = string.find(payload,"pwd=")]])
file.writeline([[amp_idx = string.find(payload,"&")]])
file.writeline([[if amp_idx < pwd_idx then]])
file.writeline([[ssid=string.sub(payload,ssid_idx+5,amp_idx-1)]])
file.writeline([[pwd=string.sub(payload,pwd_idx+4,payload_len)]])
file.writeline([[else]])
file.writeline([[pwd=string.sub(payload,pwd_idx+4,amp_idx-1)]])
file.writeline([[ssid=string.sub(payload,ssid_idx+5,payload_len)]])
file.writeline([[end]])
file.writeline([[print(ssid)]])
file.writeline([[print(pwd)]])
file.writeline([[wifi.setmode(wifi.STATION)]])
file.writeline([[wifi.sta.config(ssid,pwd)]])
file.writeline([[print("Connected to " .. ssid)]])
file.writeline([[file.open("connected","w")]])
file.writeline([[file.close()]])
file.writeline([[node.restart()]])
file.writeline([[end]])
file.writeline([[end]])
file.writeline([[html='<html><form method="POST" name="config_wifi"><p>ssid:<input name="ssid" value="" /></p>']])
file.writeline([[html = html .. '<p>pwd:<input name="pwd" value="" /></p>']])
file.writeline([[html = html .. '<p><input type="submit" value="config" /></p>']])
file.writeline([[conn:send( "".. html .." ssid was :" .. ssid .. " pwd was : " .. pwd)]])
file.writeline([[end)]])
file.writeline([[conn:on("sent",function(conn) conn:close() end)]])
file.writeline([[end)]])
file.close()
file.remove("connected.lua")
file.open("connected.lua","w")
file.writeline([[print("starting connected server")]])
file.writeline([[print(wifi.sta.getip())]])
file.writeline([[srv=net.createServer(net.TCP) srv:listen(80,function(conn)]])
file.writeline([[conn:on("receive",function(conn,payload)]])
file.writeline([[print(payload)]])
file.writeline([[if string.find(payload,"favicon.ico") == nil then]])
file.writeline([[end]])
file.writeline([[conn:send("Connected!")]])
file.writeline([[end)]])
file.writeline([[conn:on("sent",function(conn) conn:close() end)]])
file.writeline([[end)]])
file.close()
file.remove("init.lua")
file.open("init.lua","w")
file.writeline([[if file.open("connected","r") == nil then]])
file.writeline([[dofile("connect.lua")]])
file.writeline([[else]])
file.writeline([[dofile("connected.lua")]])
file.writeline([[end]])
file.close()
node.restart()


My goal is to load it and run it at my friends house and get the station ip then run a server on another lua file already setup.
It never returns a IP.
I name the file init.lua and run it, it creates a access point, I connect to it with my phone, and go to its IP "192.168.4.1", it asks for the router SSID and pass which i put in.
I can see it runs "file.writeline([[print("starting connected server")]])". line 68
then it gives me a nil.
and a msg "only one tcp server allowed"
I can manually send "print(wifi.sta.getip())" and get the station IP "192.168.1.79"
Will all station IPs return this IP no matter what router its connected to?
In this code where do I insert myserver.lua file to run after it finds the station IP.
why is it looking for favicon.ico? am i missing that? line 20 and 74 :shock:
User avatar
By marcelstoer
#65539 I believe quite a few things are wrong here. Main reason: that script hasn't been updated since January 2015 which is ages ago. You can't expect it to run smoothly on a recent NodeMCU firmware.

`wifi.sta.getip()` returns the IP but `print(wifi.sta.getip())` doesn't - it prints the IP to console but `print()` itself returns nil.

`srv=net.createServer(net.TCP)` can only be called once or otherwise you get the "only one tcp server allowed" message. So, if `srv` is not nil you need to call `net.server:close()` first before creating a new server.

`wifi.sta.config(ssid,pwd)` now uses a Lua table for arguments rather than var args.

favicon.ico is requested by your user agent (i.e browser) automatically. It's ok if you can't serve that. If you don't include a custom <link> element in your HTML the browsers try to load it off the website's root. Of course you could include <link rel="shortcut icon" href="http://example.com/myicon.ico" /> to load it from somewhere else.

BUT the NodeMCU project provides a module specifically designed for your use case (from what I understand): https://nodemcu.readthedocs.io/en/lates ... ser-setup/